自编码器(antoencoder)学习总结
自编码器通常包括两部分:encoder(识别网络或者编码器)将输入转化为内部表示(就是图中的压缩过程),decoder(生成网络或者解码器),将内部表示转换为输出,就是图中的解压过程。自编码器属于无监督学习,压缩之后的数据维度要比刚开始的数据少很多,达到数据降维的目的,然后进行解码,恢复到原来大概的形状。
用MNIST数据集实现autoencoder的代码如下:
tensorflow 版本1.14.0
numpy版本1.16.0
IDE:PyCharm
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#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.001
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)
#tf Graph input(only pictures)
X=tf.compat.v1.placeholder('float',[None,n_input])
#hidden layer setting
n_hidden_1=256 #1 first layer num features
n_hidden_2=64
n_hidden_3=10
n_hidden_4=2 #2 second layer numfeatures
weights={
#不同的tensorflow版本语法不一样,1.0版本以前应该是tf.Variable(下同)
#就是把tf.compat.v1.Variable换成tf.Variable
'encoder_h1':tf.compat.v1.Variable(tf.truncated_normal([n_input,n_hidden_1])),#784*256的形状
'encoder_h2':tf.compat.v1.Variable(tf.truncated_normal([n_hidden_1,n_hidden_2])),
'encoder_h3':tf.compat.v1.Variable(tf.truncated_normal([n_hidden_2,n_hidden_3])),
'encoder_h4':tf.compat.v1.Variable(tf.truncated_normal([n_hidden_3,n_hidden_4])),
'decoder_h1':tf.compat.v1.Variable(tf.truncated_normal([n_hidden_4,n_hidden_3])),
'decoder_h2':tf.compat.v1.Variable(tf.truncated_normal([n_hidden_3,n_hidden_2])),
'decoder_h3':tf.compat.v1.Variable(tf.truncated_normal([n_hidden_2,n_hidden_1])),
'decoder_h4':tf.compat.v1.Variable(tf.truncated_normal([n_hidden_1,n_input])),
}
biases={
'encoder_b1':tf.compat.v1.Variable(tf.random_normal([n_hidden_1])),
'encoder_b2':tf.compat.v1.Variable(tf.random_normal([n_hidden_2])),
'encoder_b3':tf.compat.v1.Variable(tf.random_normal([n_hidden_3])),
'encoder_b4':tf.compat.v1.Variable(tf.random_normal([n_hidden_4])),
'decoder_b1':tf.compat.v1.Variable(tf.random_normal([n_hidden_3])),
'decoder_b2':tf.compat.v1.Variable(tf.random_normal([n_hidden_2])),
'decoder_b3':tf.compat.v1.Variable(tf.random_normal([n_hidden_1])),
'decoder_b4':tf.compat.v1.Variable(tf.random_normal([n_input])),
}
#buliding the encoder
def encoder(x):
#encoder hidden layer with sigmoid activation#1
layer_1=tf.nn.sigmoid(tf.add(tf.matmul(x,weights['encoder_h1']),biases['encoder_b1']))
#decoder hidden layer with sigmoid activation#2
layer_2=tf.nn.sigmoid(tf.add(tf.matmul(layer_1,weights['encoder_h2']),
biases['encoder_b2']))
layer_3=tf.nn.sigmoid(tf.add(tf.matmul(layer_2,weights['encoder_h3']),biases['encoder_b3']))
layer_4=tf.add(tf.matmul(layer_3,weights['encoder_h4']),biases['encoder_b4'])
return layer_4
#buliding the decoder
def decoder(x):
#encoder hidden layer with sigmoid activation#1
layer_1=tf.nn.sigmoid(tf.add(tf.matmul(x,weights['decoder_h1']),
biases['decoder_b1']))
#decoder hidden layer with sigmoid activation#2
layer_2=tf.nn.sigmoid(tf.add(tf.matmul(layer_1,weights['decoder_h2']),
biases['decoder_b2']))
layer_3=tf.nn.sigmoid(tf.add(tf.matmul(layer_2,weights['decoder_h3']),biases['decoder_b3']))
layer_4=tf.sigmoid(tf.add(tf.matmul(layer_3,weights['decoder_h4']),biases['decoder_b4']))
return layer_4
#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,minize the squared error
cost=tf.reduce_mean(tf.pow(y_true-y_pred,2))
optimizer=tf.train.AdamOptimizer(learning_rate).minimize(cost)
#lauch the graph
with tf.compat.v1.Session() as sess:
init=tf.global_variables_initializer()
sess.run(init)
total_batch=int(mnist.train.num_examples/batch_size)
#Training cycle
for epoch in range(training_epochs):
#Loop over all batches
for i in range(total_batch):
batch_xs,batch_ys=mnist.train.next_batch(batch_size)#max(x)=1,min(x) =0
#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 theri reconstructions
f,a=plt.subplots(2,10,figsize=(10,2))
for i in range(examples_to_show):
a[0][i].imshow(np.reshape(mnist.test.images[i],(28,28)))
a[1][i].imshow(np.reshape(encode_decode[i],(28,28)))
plt.show()
#解压前的结果
# encoder_result=sess.run(encoder_op,feed_dict={X:mnist.test.images})
# plt.scatter(encoder_result[:,0],encoder_result[:,1],c=mnist.test.labels)
# plt.show()
有的时候编译器可能会出现警告如下图所示:
这是因为tensorflow版本和numpy的版本不符合,不同的tensorflow版本对应的numpy版本不一样,所以尽量找对应的版本。我之前的版本是1.19.0,这种情况可以先用
pip uninstall numpy 卸载当前的numpy版本,然后再用
pip install numpy==1.16.0(numpy的版本号)我用的是16.0,大家可以根据自己的需要安装对应的版本。
输出结果如下:
把encode_decode=sess.run(y_pred,feed_dict={X:mnist.test.images[:examples_to_show]}) #Compare original images with theri reconstructions f,a=plt.subplots(2,10,figsize=(10,2)) for i in range(examples_to_show): a[0][i].imshow(np.reshape(mnist.test.images[i],(28,28))) a[1][i].imshow(np.reshape(encode_decode[i],(28,28))) plt.show()
给注释掉,把最后三行代码取消注释得到的结果如下