前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[TensorFlow深度学习入门]实战十二·使用DNN网络实现自动编码器

[TensorFlow深度学习入门]实战十二·使用DNN网络实现自动编码器

作者头像
小宋是呢
发布2019-06-27 11:38:25
4900
发布2019-06-27 11:38:25
举报
文章被收录于专栏:深度应用深度应用

[TensorFlow深度学习入门]实战十二·使用DNN网络实现自动编码器

  • 测试代码
代码语言:javascript
复制
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images / 255.0
test_images = test_images / 255.0
"""plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()"""

print(train_images.shape)

x = tf.placeholder(tf.float32,[None,28,28])
y_ = tf.placeholder(tf.float32,[None,28,28])
xs = tf.reshape(x,shape=[-1,28*28])

y_shape = tf.reshape(y_,shape=[-1,28*28])
with tf.variable_scope("encoder") as scope1:
    w1 = tf.get_variable("w1",initializer=tf.random_normal([784,128],stddev=1))
    w2 = tf.get_variable("w2",initializer=tf.random_normal([128,16],stddev=1))
    b1 = tf.get_variable("b1",initializer=tf.zeros([1,128])+0.01)
    b2 = tf.get_variable("b2",initializer=tf.zeros([1,16])+0.01)
    l1 = tf.nn.sigmoid(tf.matmul(xs,w1)+b1)
    l2 = tf.nn.sigmoid(tf.matmul(l1,w2)+b2)


with tf.variable_scope("decoder") as scope2:
    w3 = tf.get_variable("w3",initializer=tf.random_normal([16,128],stddev=1))
    w4 = tf.get_variable("w4",initializer=tf.random_normal([128,784],stddev=1))
    b3 = tf.get_variable("b3",initializer=tf.zeros([1,128])+0.01)
    b4 = tf.get_variable("b4",initializer=tf.zeros([1,784])+0.01)
    l3 = tf.nn.sigmoid(tf.matmul(l2,w3)+b3)
    y = tf.nn.sigmoid(tf.matmul(l3,w4)+b4)

loss = tf.reduce_mean(tf.square(y-y_shape))
opt = tf.train.AdamOptimizer(0.05).minimize(loss)

with tf.Session() as sess:
    
    init = tf.global_variables_initializer()
    srun = sess.run
    srun(init)
    for e in range(3001):
        ts = e*100%60000
        loss_val,_ = srun([loss,opt],{x:train_images[ts:ts+100],y_:train_images[ts:ts+100]})
        if(e%100==0):
            print(e,loss_val)
    y_out = srun(y,{x:test_images[:1]})
    y_show = tf.reshape(y_out,shape=[-1,28,28])
    y_show = y_show.eval()
    print(type(y_show))
    print((y_show.shape))
    print((train_images[0].shape))
    plt.figure()
    plt.imshow(y_show[0])
    #plt.imshow(test_images[0])
    plt.colorbar()
    plt.grid(False)
    plt.show()


plt.figure()
plt.imshow(test_images[0])
plt.colorbar()
plt.grid(False)
plt.show()
  • 输出结果
代码语言:javascript
复制
0 0.3734877
100 0.07903126
200 0.069011234
300 0.06310683
400 0.05900423
500 0.05826289
600 0.057095125
700 0.05598061
800 0.053362366
900 0.049294725
1000 0.05573253
1100 0.049589165
1200 0.052580144
1300 0.050453667
1400 0.052007377
1500 0.04682982
1600 0.052490402
1700 0.047567524
1800 0.05900064
1900 0.051574014
2000 0.049422733
2100 0.046142038
2200 0.05289932
2300 0.049340945
2400 0.051445786
2500 0.0516909
2600 0.049265135
2700 0.04827474
2800 0.04997604
2900 0.04606307
3000 0.050183807
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年12月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • [TensorFlow深度学习入门]实战十二·使用DNN网络实现自动编码器
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档