Tesorflow-自动编码器(AutoEncoder)

Posted Run_For_Love一朝一夕一点一滴

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tesorflow-自动编码器(AutoEncoder)相关的知识,希望对你有一定的参考价值。

直接附上代码:

技术分享图片
 1 import numpy as np
 2 import sklearn.preprocessing as prep
 3 import tensorflow as tf
 4 from tensorflow.examples.tutorials.mnist import input_data
 5 
 6 def xavier_init(fan_in,fan_out,constant=1):
 7     low=-constant*np.sqrt(6.0/(fan_in+fan_out))
 8     high=constant*np.sqrt(6.0/(fan_in+fan_out))
 9     return tf.random_uniform((fan_in,fan_out),minval=low,maxval=high,dtype=tf.float32)
10 
11 class AdditiveGaussianNoiseAutoencoder(object):
12     def __init__(self,n_input,n_hidden,transfer_function=tf.nn.softplus,optimizer=tf.train.AdamOptimizer(),scale=0.1):
13         self.n_input=n_input
14         self.n_hidden=n_hidden
15         self.transfer=transfer_function
16         self.scale=tf.placeholder(tf.float32)
17         self.training_scale=scale
18         network_weights=self._initialize_weights()
19         self.weights=network_weights
20 
21         self.x=tf.placeholder(tf.float32,[None,self.n_input])
22         self.hidden=self.transfer(tf.add(tf.matmul(self.x+scale*tf.random_normal((n_input,)),self.weights[w1]),self.weights[b1]))
23         self.reconstruction=tf.add(tf.matmul(self.hidden,self.weights[w2]),self.weights[b2])
24         self.cost=0.5*tf.reduce_sum(tf.pow(tf.sub(self.reconstruction,self.x),2.0))
25         self.optimizer=optimizer.minimize(self.cost)
26 
27         init=tf.initialize_all_variables()
28         self.sess=tf.Session()
29         self.sess.run(init)
30 
31     def _initialize_weights(self):
32         all_weights=dict()
33         all_weights[w1]=tf.Variable(xavier_init(self.n_input,self.n_hidden))
34         all_weights[b1]=tf.Variable(tf.zeros([self.n_hidden],dtype=tf.float32))
35         all_weights[w2]=tf.Variable(tf.zeros([self.n_hidden,self.n_input],dtype=tf.float32))
36         all_weights[b2]=tf.Variable(tf.zeros([self.n_input],dtype=tf.float32))
37 
38         return all_weights
39 
40     def partial_fit(self,X):
41 
42         cost,opt=self.sess.run((self.cost,self.optimizer),feed_dict={self.x:X,self.scale:self.training_scale})
43 
44         return cost
45 
46     def calc_total_cost(self,X):
47         return self.sess.run(self.cost,feed_dict={self.x:X,self.scale:self.training_scale})
48 
49     def transform(self,X):
50         return self.sess.run(self.hidden,feed_dict={self.x:X,self.scale:self.training_scale})
51 
52     def generate(self,hidden=None):
53         if hidden is None:
54             hidden=np.random.normal(size=self.weights["b1"])
55         return self.sess.run(self.reconstruction,feed_dict={self.hidden:hidden})
56 
57     def reconstruct(self,X):
58         return self.sess.run(self.reconstruction,feed_dict={self.x:X,self.scale:self.training_scale})
59 
60     def getWeights(self):
61         return self.sess.run(self.weights[w1])
62 
63     def getBiases(self):
64         return self.sess.run(self.weights[b1])
65 
66 mnist=input_data.read_data_sets(MNIST_data,one_hot=True)
67 
68 def standard_scale(X_train,X_test):
69     preprocessor=prep.StandardScaler().fit(X_train)
70     X_train=preprocessor.transform(X_train)
71     X_test=preprocessor.transform(X_test)
72     return X_train,X_test
73 
74 def get_random_block_from_data(data,batch_size):
75     start_index=np.random.randint(0,len(data)-batch_size)
76     return data[start_index:(start_index+batch_size)]
77 
78 X_train,X_test=standard_scale(mnist.train.images,mnist.test.images)
79 n_samples=int(mnist.train.num_examples)
80 training_epochs=20
81 batch_size=128
82 diaplay_step=1
83 autoencoder=AdditiveGaussianNoiseAutoencoder(n_input=784,n_hidden=200,transfer_function=tf.nn.softplus,optimizer=tf.train.AdamOptimizer(learning_rate=0.001),scale=0.01)
84 for epoch in range(training_epochs):
85     avg_cost=0
86     total_batch=int(n_samples/batch_size)
87     for i in range(total_batch):
88         batch_xs=get_random_block_from_data(X_train,batch_size)
89 
90         cost=autoencoder.partial_fit(batch_xs)
91         avg_cost+=cost/n_samples*batch_size
92 
93     if epoch%diaplay_step==0:
94         print("Epoch:",%04d%(epoch+1),"cost=","{:.9f}".format(avg_cost))
95 
96 print("Total cost: "+str(autoencoder.calc_total_cost(X_test)))
View Code

 

以上是关于Tesorflow-自动编码器(AutoEncoder)的主要内容,如果未能解决你的问题,请参考以下文章

Tesorflow源代码安装方式以及错误的解决方法

Ubuntu 16.04 + tesorflow环境配置

假期续命充电——简单上手tesorflow2 框架

OpenCV的dnn模块调用TesorFlow训练的MoblieNet模型

OpenCV的dnn模块调用TesorFlow训练的MoblieNet模型

对抗性自动编码器系列--自动编码器AutoEncoder的原理及实现-手写数字的重建