tensorflow实现简单的自编码器

  1 # 自编码器
  2 #导入相关库
  3 import numpy as np
  4 import sklearn.preprocessing as prep
  5 import tensorflow as tf
  6 from tensorflow.examples.tutorials.mnist import input_data
  7 
  8 #定义初始化函数
  9 def xavier_init(fan_in,fan_out,constant=1):
 10     low  = -constant * np.sqrt(6.0 / (fan_in + fan_out))
 11     high  = constant * np.sqrt(6.0 / (fan_in + fan_out))
 12     return tf.random_uniform((fan_in,fan_out),
 13                              minval = low, maxval = high,
 14                              dtype = tf.float32)
 15 
 16 #定义去噪自编码器类
 17 class AdditiveGaussianNoiseAutoencoder(object):
 18     def __init__(self, n_input, n_hidden, transfer_function=tf.nn.softplus,optimizer = tf.train.AdamOptimizer(),scale=0.1):
 19         self.n_input = n_input
 20         self.n_hidden = n_hidden
 21         self.transfer = transfer_function
 22         self.scale = tf.placeholder(tf.float32)
 23         self.training_scale = scale
 24         network_weights = self._initialize_weights()
 25         self.weights = network_weights
 26         self.x = tf.placeholder(tf.float32,[None,self.n_input])
 27         self.hidden = self.transfer(tf.add(tf.matmul(self.x + scale * tf.random_normal((n_input,)),
self.weights['w1']),self.weights['b1'])) 28 self.reconstruction = tf.add(tf.matmul(self.hidden,self.weights['w2']),self.weights['b2']) 29 self.cost = 0.5 * tf.reduce_sum(tf.pow(tf.subtract(self.reconstruction,self.x),2.0)) 30 self.optimizer = optimizer.minimize(self.cost) 31 init = tf.global_variables_initializer() 32 self.sess = tf.Session() 33 self.sess.run(init) 34 35 #权重初始化函数 36 def _initialize_weights(self): 37 all_weights = dict() 38 all_weights['w1'] = tf.Variable(xavier_init(self.n_input,self.n_hidden)) 39 all_weights['b1'] = tf.Variable(tf.zeros([self.n_hidden],dtype = tf.float32)) 40 all_weights['w2'] = tf.Variable(tf.zeros([self.n_hidden,self.n_input],dtype = tf.float32)) 41 all_weights['b2'] = tf.Variable(tf.zeros([self.n_input],dtype = tf.float32)) 42 return all_weights 43 44 #计算损失及执行训练的函数 45 def partial_fit(self,X): 46 cost, opt = self.sess.run((self.cost,self.optimizer),feed_dict = {self.x:X,self.scale:self.training_scale}) 47 return cost 48 49 #只求损失的函数 50 def calc_total_cost(self,X): 51 return self.sess.run(self.cost,feed_dict = {self.x:X,self.scale:self.training_scale}) 52 #返回隐藏层的输出结果 53 def transform(self,X): 54 return self.sess.run(self.hidden,feed_dict = {self.x:X,self.scale:self.training_scale}) 55 #将重建层提取到的高阶特征复原为原始函数 56 def generate(self,hidden = None): 57 if hidden is None: 58 hidden = np.random.normal(size = self.weights["b1"]) 59 return self.sess.run(self.reconstruction,feed_dict = {self.hidden:hidden}) 60 #提取高阶特征及通过高阶特征复原原始数据 61 def reconstruct(self,X): 62 return self.sess.run(self.reconstruction,feed_dict = {self.x:X,self.scale:self.training_scale}) 63 #获取隐藏层的权重 64 def getWeights(self): 65 return self.sess.run(self.weights['w1']) 66 #获取隐藏层的偏置 67 def getBiases(self): 68 return self.ssess.run(self.weights['b1']) 69 70 71 #载入mnist数据集 72 mnist = input_data.read_data_sets('MNIST_data/',one_hot = True) 73 74 #对训练、测试进行标准化处理的函数 75 def standard_scale(X_train,X_test): 76 preprocessor = prep.StandardScaler().fit(X_train) 77 X_train = preprocessor.transform(X_train) 78 X_test = preprocessor.transform(X_test) 79 return X_train, X_test 80 81 82 #获取随机block的函数 83 def get_random_block_data(data, batch_size): 84 start_index = np.random.randint(0, len(data) - batch_size) 85 return data[start_index:(start_index + batch_size)] 86 87 88 #对数据集进行标准化变换 89 X_train,X_test = standard_scale(mnist.train.images,mnist.test.images) 90 91 #定义常用参数 92 n_samples = int(mnist.train.num_examples) 93 training_epochs = 200 94 batch_size = 128 95 display_step = 1 96 97 #创建自编码器的一个实例 98 autoencoder = AdditiveGaussianNoiseAutoencoder(n_input = 784,n_hidden = 200,transfer_function = tf.nn.softplus,optimizer = tf.train.AdamOptimizer(learning_rate = 0.001),scale = 0.01) 99 #开始训练 100 for epoch in range(training_epochs): 101 avg_cost = 0 102 total_batch = int(n_samples / batch_size) 103 for i in range(total_batch): 104 batch_xs = get_random_block_from_data(X_train, batch_size) 105 106 107 cost = autoencoder.partial_fit(batch_xs) 108 avg_cost += cost / n_samples * batch_size 109 110 if epoch % display_step == 0: 111 print("Epoch:", '%04d' % (epoch+1),"cost=", "{:.9f}".format(avg_cost)) 112 113 #性能测试 114 print("Total cost:" + str(autoencoder.calc_total_cost(X_test)))

参考书籍:

1.《Tensorflow实战》黄文坚  唐源 著

作者:舟华520

出处:https://www.cnblogs.com/xfzh193/

本文以学习,分享,研究交流为主,欢迎转载,请标明作者出处!

posted on 2020-10-28 12:08  舟华  阅读(172)  评论(0编辑  收藏  举报

导航