def test_salt_and_pepper_noise_with_min_max(self):
        """ test the salt and pepper function with specified min and max values.
        """
        x_sp = utils.salt_and_pepper_noise(self.x, self.v, 0, 1)

        for sample in x_sp:
            salted_elems = sum([i == 0 or i == 1 for i in sample])
            self.assertEqual(salted_elems, self.v)
    def test_salt_and_pepper_noise_with_min_max(self):
        """ test the salt and pepper function with specified min and max values.
        """
        x_sp = utils.salt_and_pepper_noise(self.x, self.v, 0, 1)

        for sample in x_sp:
            salted_elems = sum([i == 0 or i == 1 for i in sample])
            self.assertEqual(salted_elems, self.v)
    def test_salt_and_pepper_noise_without_min_max(self):
        """ test the salt and pepper function without specified min and max values.
        """
        x_sp = utils.salt_and_pepper_noise(self.x, self.v)

        mn = self.x.min()
        mx = self.x.max()

        for sample in x_sp:
            salted_elements = sum([i == mn or i == mx for i in sample])
            self.assertAlmostEqual(salted_elements, self.v, delta=2)
    def test_salt_and_pepper_noise_without_min_max(self):
        """ test the salt and pepper function without specified min and max values.
        """
        x_sp = utils.salt_and_pepper_noise(self.x, self.v)

        mn = self.x.min()
        mx = self.x.max()

        for sample in x_sp:
            salted_elements = sum([i == mn or i == mx for i in sample])
            self.assertAlmostEqual(salted_elements, self.v, delta=2)
示例#5
0
    def corrupt_input(self, data, v):

        if self.corruption_type == 'masking':
            x_corrupted = utils.masking_noise(data, v)

        elif self.corruption_type == 'salt_and_pepper':
            x_corrupted = utils.salt_and_pepper_noise(data, v)

        elif self.corruption_type == 'gaussian':
            x_corrupted = utils.gaussian_noise(data, v)

        elif self.corruption_type == 'none':
            x_corrupted = data

        else:
            x_corrupted = None

        return x_corrupted
	def _corrupt_input(self, data, v):
		''' Corrupt a fraction 'v' of 'data' according to the noise method of this autoencoder.

		Returns
		-------
		corrupted data
		'''

		if self.corr_type == 'none':
			return np.copy(data)

		if v > 0.0:
			if self.corr_type == 'masking':
				return utils.masking_noise(data, v)
			elif self.corr_type == 'salt_and_pepper':
				return utils.salt_and_pepper_noise(data, v)
		else:
			return np.copy(data)
示例#7
0
    def _corrupt_input(self, data, v):
        """ Corrupt a fraction 'v' of 'data' according to the
        noise method of this autoencoder.
        :return: corrupted data
        """

        if self.corr_type == 'masking':
            x_corrupted = utils.masking_noise(data, v)

        elif self.corr_type == 'salt_and_pepper':
            x_corrupted = utils.salt_and_pepper_noise(data, v)

        elif self.corr_type == 'none':
            x_corrupted = data

        else:
            x_corrupted = None

        return x_corrupted
示例#8
0
    def fit(self, trX, vlX=None, restore_previous_model=False):
        """ Fit the model to the data.

        :type trX: array_like, shape (n_samples, n_features).
        :param trX: Training data.

        :type vlX: array_like, shape (n_validation_samples, n_features).
        :param vlX: optional, default None. Validation data.

        :return: self
        """
        n_features = trX.shape[1]

        self._create_graph(n_features)

        # Merge all the summaries
        merged = tf.merge_all_summaries()
        # Initialize variables
        init_op = tf.initialize_all_variables()
        # Add ops to save and restore all the variables
        self.saver = tf.train.Saver()

        with tf.Session() as self.sess:

            self.sess.run(init_op)

            if restore_previous_model:
                # Restore previous model
                self.saver.restore(self.sess,
                                   self.models_dir + self.model_name)
                # Change model name
                self.model_name += '-restored{}'.format(self.n_iter)

            # ################## #
            #   Training phase   #
            # ################## #

            v = np.round(self.corr_frac * n_features).astype(np.int)

            # Write the summaries to summary_dir
            writer = tf.train.SummaryWriter(self.summary_dir,
                                            self.sess.graph_def)

            for i in range(self.n_iter):

                # #################### #
                #   Input Corruption   #
                # #################### #

                if self.corr_type == 'masking':
                    x_corrupted = utils.masking_noise(trX, v)

                elif self.corr_type == 'salt_and_pepper':
                    x_corrupted = utils.salt_and_pepper_noise(trX, v)

                else:  # none, normal autoencoder
                    x_corrupted = trX

                # Randomly shuffle the input
                shuff = zip(trX, x_corrupted)
                np.random.shuffle(shuff)

                # # Divide dataset into mini-batches
                batches = [
                    _ for _ in utils.gen_batches(shuff, self.batch_size)
                ]

                # All the batches for each epoch
                for batch in batches:
                    x_batch, x_corr_batch = zip(*batch)
                    tr_feed = {
                        self.x: x_batch,
                        self.x_corr: x_corr_batch,
                        self.keep_prob: self.dropout
                    }
                    self.sess.run(self.train_step, feed_dict=tr_feed)

                # Record summary data
                if vlX is not None:
                    vl_feed = {
                        self.x: vlX,
                        self.x_corr: vlX,
                        self.keep_prob: 1.
                    }
                    result = self.sess.run([merged, self.cost],
                                           feed_dict=vl_feed)
                    summary_str = result[0]
                    err = result[1]

                    writer.add_summary(summary_str, i)

                    if self.verbose == 1:
                        print("Validation cost at step %s: %s" % (i, err))

            # Save trained model
            self.saver.save(self.sess, self.models_dir + self.model_name)