tensorflow1.1/autoencoder2

来源:互联网 发布:我知女人心南宫寒txt 编辑:程序博客网 时间:2024/06/01 09:54

环境:tensorflow1.1 , python 3, matplotlib 2.02

encoder编码器将编码得到的低维特征在空间中可视化出来,直观显示数据的聚类效果。实验过程中采用过激活函数 relu ,tanh,sigmoid,发现采用激活函数relu时聚类效果最好

#coding:utf-8"""tensorflow 1.1python 3matplotlib 2.02"""import tensorflow as tfimport numpy as npimport matplotlib.pyplot as pltimport input_datamnist = input_data.read_data_sets('mnist/',one_hot=False)learning_rate = 0.01batch_size = 256#非监督学习没有labelxs = tf.placeholder(tf.float32,[None,28*28])#构建autoencoder网络tf.nn.tanh(-1,1),tf.nn.sigmoid(0,1),autoencoder输入图片没有进行reshapeencoder1 = tf.layers.dense(xs,128,tf.nn.relu)encoder2 = tf.layers.dense(encoder1,64,tf.nn.relu)encoder3 = tf.layers.dense(encoder2,10,tf.nn.relu)#为了便于编码层的输出,encoder4不适用激活函数encoder4 = tf.layers.dense(encoder3,2)decoder0 = tf.layers.dense(encoder4,10,tf.nn.relu)decoder1 = tf.layers.dense(decoder0,64,tf.nn.relu)decoder2 = tf.layers.dense(decoder1,128,tf.nn.relu)decoder3 = tf.layers.dense(decoder2,28*28,tf.nn.relu)#计算loss,输出值和输入值作比较loss = tf.losses.mean_squared_error(labels=xs,predictions=decoder3)train = tf.train.AdamOptimizer(learning_rate).minimize(loss)with tf.Session() as sess:    init = tf.global_variables_initializer()    sess.run(init)    for step in range(5000):        batch_x,batch_y = mnist.train.next_batch(batch_size)        _,c = sess.run([train,loss],feed_dict={xs:batch_x})        if step % 500 ==0:            print('= = = = = = > > > > > >','epoch: ',int(step/500),'train loss %.4f' % c)    #压缩的数据可视化    encoder_result = sess.run(encoder4,feed_dict={xs:mnist.train.images})    plt.scatter(encoder_result[:,0],encoder_result[:,1],c=mnist.train.labels,label='mnist distribution')    plt.legend(loc='best')    plt.title('different mnist digits shows in figure')    plt.colorbar()    plt.show()

采用relu作为激活函数聚类效果

这里写图片描述

采用tanh作为激活函数聚类效果

这里写图片描述

采用sigmoid作为激活函数聚类效果

这里写图片描述

原创粉丝点击