Fork me on GitHub

Autoencoder自编码

Autoencoder自编码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#author:victor
#什么是自编码(Autoencoder)
#What is an Autoencoder
#神经网络的非监督学习
#因为有时候训练的样本数据很大,直接训练会很耗时的,所以把数据的feature压缩一下,然后再解压一下
#Autoencoder是一种数据的压缩算法,其中数据的压缩和解压函数
#数据相关的,有损的,从样本中自动学习的,压缩和解压缩的函数是通过神经网络实现的
#因为自编码不用到训练样本的分类标签,所以是非监督学习的
#比如PCA(principal Component Analysis):主成分析方法。一种使用最广发的数据压缩算法。
#PCA一种常用的数据降维方法。通过线性变换将原始数据变换成为一组各维度线性无关的表示来提取数据的主要线性分量
#比如分类学习,也是非监督学习的

#import module
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

#import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets('MNIST_data',one_hot=False)

#Visualize decoder setting
#Parameters
learning_rate=0.01
training_epochs=20
batch_size=256
display_step=1
examples_to_show=10

#Network Parameters
n_input=784#MNIST data input(img shape:28*28),也即是784个features

#tf.Graph input(only pictures)
X=tf.placeholder('float',[None,n_input])

#hidden layer settings
n_hidden_1=256#first num features(2^8),先经过一个隐藏层压缩成256个features
n_hidden_2=128#second num features(2^7),在经过一个隐藏层压缩成128个features
#define the weights
weights={
'encoder_h1':tf.Variable(tf.random_normal([n_input,n_hidden_1])),
'encoder_h2':tf.Variable(tf.random_normal([n_hidden_1,n_hidden_2])),
#经过一个隐藏层解压缩把128个features解压成256个features
'decoder_h1':tf.Variable(tf.random_normal([n_hidden_2,n_hidden_1])),
#经过一个隐藏层解压缩把256个features解压成原来784个features
'decoder_h2':tf.Variable(tf.random_normal([n_hidden_1,n_input])),
}
#define the biases
biases={
'encoder_b1':tf.Variable(tf.random_normal([n_hidden_1])),
'encoder_b2':tf.Variable(tf.random_normal([n_hidden_2])),
'decoder_b1':tf.Variable(tf.random_normal([n_hidden_1])),
'decoder_b2':tf.Variable(tf.random_normal([n_input])),
}

#building the encoder
def encoder(x):
layer_1=tf.nn.sigmoid(tf.add(tf.matmul(x,weights['encoder_h1']),
biases['encoder_b1'] ))
#Decoder hidden layer with sigmoid activation function
layer_2=tf.nn.sigmoid(tf.add(tf.matmul(layer_1,weights['encoder_h2']),
biases['encoder_b2']))
return layer_2

#building the decoder
def decoder(x):
#Encoder hidden layer with sigmoid activation
layer_1=tf.nn.sigmoid(tf.add(tf.matmul(x,weights['decoder_h1']),
biases['decoder_b1'] ))
#Decoder hidden layer with sigmoid activation function
layer_2=tf.nn.sigmoid(tf.add(tf.matmul(layer_1,weights['decoder_h2']),
biases['decoder_b2']))
return layer_2

#Construct model
encoder_op=encoder(X)
decoder_op=decoder(encoder_op)

#Prediction
y_pred=decoder_op
#Targets(Labels) are the input data
y_true=X

#Define loss and optimizer,minimize the squre error
cost=tf.reduce_mean(tf.pow(y_true-y_pred,2))
optimizer=tf.train.AdamOptimizer(learning_rate).minimize(cost)

#Initializing the variables
init=tf.initialize_all_variables()

#Launch the graph
with tf.Session() as sess:
sess.run(init)
total_batch=int(mnist.train.num_examples/batch_size)
#Train cycle
for epoch in range(training_epochs):
#Loop overall batches
for i in range(total_batch):
batch_xs,batch_ys=mnist.train.next_batch(batch_size)#max(x)=1,min(x)=0,batch_xs已经被normalize正规化过了,最大值是1
#Run optimization op (backprop) and cost op (to get loss value)
_,c=sess.run([optimizer,cost],feed_dict={X:batch_xs})
#Display logs per epoch step
if epoch% display_step==0:
print("Epoch",'%04d'%(epoch+1),
"cost=","{:9f}".format(c))

print("Optimization Finished!")

#Applying encode and decode over test set
encode_decode=sess.run(
y_pred,feed_dict={X:mnist.test.images[:examples_to_show]})
#Compare original images with their reconstructions
f,a=plt.subplots(2,10,figsize=(10,2))
for i in range(examples_to_show):
#real data
a[0][i].imshow(np.reshape(mnist.test.images[i],(28,28)))
#predict data
a[1][i].imshow(np.reshape(encode_decode[i],(28,28)))
plt.show()

运行结果:

autoencoder

总结:发现经过压缩过后的MNIST data,在训练的时候明显速度加快了。说明在进行大量数据训练的时候,使用自编码进行encoder-decoder不失为一个好办法。