tensorflow1.1/非监督学习autoencoder1

环境:tensorflow 1.1, python 3,matplotlib 2.02

AutoEncoder是一种非监督学习,将数据的高维特征进行压缩降维编码,再经过相反的解码过程的一种学习方法。学习过程中通过解码得到的最终结果与原数据进行比较,通过梯度下降,不断提高对原数据的复原能力,原理与PCA相似。
这里写图片描述
在MNIST数据集上,实现特征压缩和特征解压并可视化比较解压后的数据与原数据的对照,实验中对比了5张手写数字图片

#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)
#设置随机种子
tf.set_random_seed(100)
np.random.seed(100)
learning_rate = 0.01
N_pictures = 5
batch_size = 64

#非监督学习没有label
xs = tf.placeholder(tf.float32,[None,28*28])

#构建autoencoder网络,tf.nn.tanh(-1,1),tf.nn.sigmoid(0,1)
encoder0 = tf.layers.dense(xs,128,tf.nn.tanh)
encoder1 = tf.layers.dense(encoder0,64,tf.nn.tanh)
encoder2 = tf.layers.dense(encoder1,12,tf.nn.tanh)
encoder3 = tf.layers.dense(encoder2,3)
decoder0 = tf.layers.dense(encoder3,12,tf.nn.tanh)
decoder1 = tf.layers.dense(decoder0,64,tf.nn.tanh)
decoder2 = tf.layers.dense(decoder1,128,tf.nn.tanh)
decoder3 = tf.layers.dense(decoder2,28*28,tf.nn.sigmoid)

#计算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)
    #画图,2行5列返回图和子图
    figure_,a = plt.subplots(2,N_pictures,figsize=(5,2))
    #开始交互模式
    plt.ion()
    #测试的图
    view_figures = mnist.test.images[:N_pictures]
    for i in range(N_pictures):
        #将图片reshape为28行28列显示
        a[0][i].imshow(np.reshape(view_figures[i],(28,28)))
        #清空x轴,y轴坐标
        a[0][i].set_xticks(())
        a[0][i].set_yticks(())
    for step in range(10000):
        batch_x,batch_y = mnist.train.next_batch(batch_size)
        #encoder3和decoder3需要进行run
        _,encoded,decoded,c = sess.run([train,encoder3,decoder3,loss],feed_dict={xs:batch_x})
        if step % 1000 ==0:
            print('= = = = = = > > > > > >','train loss:% .4f' % c)
            #将真实的图片和autoencoder后的图片对比
            decoder_figures = sess.run(decoder3,feed_dict={xs:view_figures})
            for i in range(N_pictures):
                #清除第一行图片
                a[1][i].clear()
                a[1][i].imshow(np.reshape(decoder_figures[i],(28,28)))
                a[1][i].set_xticks(())
                a[1][i].set_yticks(())
            plt.draw()
            plt.pause(1)
    plt.ioff() #关闭交互模式

结果:

这里写图片描述

这里写图片描述

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值