Note: Code from [1] [2]
# Here, for example with the most representative noise removal from the encoder. # Import MNIST datasets Import numpy AS NP Import sklearn.preprocessing AS Prep Import tensorflow AS TF from tensorflow.examples.tutorials.mnist Import input_data # here uses a parameter initialization method xavier initialization, the definition needs to do this work. # Action Xaiver initializer is important to make the right small just right. # Implemented here is a standard uniformly distributed Xaiver initializer. DEF xavier_init (fan_in, fan_out, Constant =. 1 ): "" " is reasonably initialization weight. Parameters: fan_in - number of rows; fan_out - the number of columns; . Constant - constant-weight, wide-multiple condition initialization return initialized weight Tensor. "" " Low = -constant * np.sqrt (6.0 / (+ fan_in fan_out)) High = Constant * np.sqrt (6.0 / (+ fan_in fan_out)) return tf.random_uniform ((fan_in, fan_out), MINVAL = Low, = MAXVAL High, DTYPE = tf.float32) # define a self-encoded class denoising class AdditiveGaussianNoiseAutoencoder (Object): "" " the __init __ (): Construction function; n_input: number of input variables; n_hidden: hidden layer nodes; transfer_function: implicit containing layer activation function, default softplus; optimizer: optimizer, default Adam; Scale: Gaussian noise factor, the default is 0.1; "" " DEF the __init__ (Self, n_input, n_hidden, transfer_function = tf.nn.softplus, Optimizer = tf.train.AdamOptimizer (), Scale = 0.1 ): self.n_input = n_input self.n_hidden = n_hidden self.transfer = transfer_function self.scale = tf.placeholder (tf.float32) self.training_scale = Scale network_weights = self._initialize_weights () self.weights = network_weights # define the network structure, creating a placeholder n_input dimension of the input x, and #The establishment of a hidden layer can extract feature. = self.x tf.placeholder (tf.float32, [None, self.n_input]) self.hidden = self.transfer (tf.add (tf.matmul (self.x Scale * + tf.random_normal ((n_input,) ), self.weights [ ' W1 ' ]), self.weights [ ' B1 ' ])) self.reconstruction = tf.add (tf.matmul (self.hidden, self.weights [ ' w2 of ' ]), Self. weights [ ' B2 ' ]) # first, define custom encoder loss function is directly used herein squared error (SquaredError) as cost. #Then, the training operation is defined as a loss of self.cost self.optimizer optimizer to optimize. # Finally, create Session, and initialize all of the model parameters from the encoder. = 0.5 * tf.reduce_sum self.cost (tf.pow (tf.subtract (self.reconstruction, self.x), 2.0 )) self.optimizer = optimizer.minimize (self.cost) the init = tf.global_variables_initializer () Self .sess = tf.Session () self.sess.run (the init) DEF _initialize_weights (Self): all_weights = dict () all_weights [ ' W1 ' ] = tf.Variable (xavier_init (self.n_input, self.n_hidden)) all_weights ['b1'] = tf.Variable(tf.zeros([self.n_hidden], dtype=tf.float32)) all_weights['w2'] = tf.Variable(tf.zeros([self.n_hidden, self.n_input], dtype=tf.float32)) all_weights['b2'] = tf.Variable(tf.zeros([self.n_input], dtype=tf.float32)) return all_weights def partial_fit(self, X): cost, opt = self.sess.run((self.cost, self.optimizer), feed_dict={self.x: X, self.scale: self.training_scale}) return cost DEF calc_total_cost (Self, X-): return self.sess.run (self.cost, feed_dict = {self.x: X-, self.scale: self.training_scale}) # define a transform function, to return from the encoder implied output layer, object feature provides an interface to obtain the abstract. DEF Transform (Self, X-): return self.sess.run (self.hidden, feed_dict = {self.x: X-, self.scale: self.training_scale}) DEF Generate (Self, hidden = None): IF hidden IS none: hidden = np.random.normal (size = self.weights [ " B1 "]) Return self.sess.run (self.reconstruction, feed_dict = {self.hidden: hidden}) DEF the reconstruct (Self, X-): return self.sess.run (self.reconstruction, feed_dict = {self.x: X- , self.scale: self.training_scale}) DEF getWeights (Self): # Get hidden layer weights W1. return self.sess.run (self.weights [ ' W1 ' ]) DEF getBiases (Self): # Get implicit paranoid factor-containing layer B1. return self.sess.run (self.weights [ ' B1 ' ]) #Using the function to read the sample data provided TensorFlow MNIST loading data set. MNIST = input_data.read_data_sets ( ' MNIST_data ' , one_hot = True) # define a training, standardized test data processing function. DEF standard_scale (X_train, X_test): Preprocessor = prep.StandardScaler () Fit (X_train). X_train = preprocessor.transform (X_train) X_test = preprocessor.transform (X_test) return X_train, X_test DEF get_random_block_from_data (Data, the batch_size): start_index = np.random.randint (0, len (Data) - the batch_size) return data[start_index:(start_index + batch_size)] X_train, X_test = standard_scale(mnist.train.images, mnist.test.images) n_samples = int(mnist.train.num_examples) training_epochs = 20 batch_size = 128 display_step = 1 autoencoder = AdditiveGaussianNoiseAutoencoder(n_input=784, n_hidden=200, transfer_function=tf.nn.softplus, optimizer=tf.train.AdamOptimizer(learning_rate=0.001), scale=0.01) for epoch in range(training_epochs): avg_cost = 0. total_batch = int(n_samples / batch_size) # Loop over all batches for i in range(total_batch): batch_xs = get_random_block_from_data(X_train, batch_size) # Fit training using batch data cost = autoencoder.partial_fit(batch_xs) # Compute average loss avg_cost += cost / n_samples * batch_size # Display logs per epoch step IF Epoch% display_step == 0: Print ( " Epoch: " , ' % 04D ' % (+ Epoch. 1), " cost = " , " {:} .9f " .format (avg_cost)) # finally finished training model performance testing. Print ( " Total cost: " + str (autoencoder.calc_total_cost (X_test)))
[1] Huangwen Jian .TensorFlow combat Beijing: Electronic Industry Press
[2] https://blog.csdn.net/qq_37608890/article/details/79352212