tensorflow1.1/autoencoder2

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

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

#coding:utf-8
"""
tensorflow 1.1
python 3
matplotlib 2.02

"""
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import input_data

mnist = input_data.read_data_sets('mnist/',one_hot=False)

learning_rate = 0.01
batch_size = 256

#非监督学习没有label
xs = tf.placeholder(tf.float32,[None,28*28])
#构建autoencoder网络tf.nn.tanh(-1,1),tf.nn.sigmoid(0,1),autoencoder输入图片没有进行reshape
encoder1 = 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作为激活函数聚类效果

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值