tensoflow--代码学习7(autoencoder)

代码段1:

#Autoencoder
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',one_hot=False)

#Visualize decoder setting
#parameters
learning_rate = 0.01
training_epochs = 5
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.placeholder('float',[None,n_input])

#hidden layer settings
n_hidden_1 = 256    #1st layer num features
n_hidden_2 = 128    #2rd layer num features
weights = {
    'encoder_h1':tf.Variable(tf.random_normal([n_input,n_hidden_1])),
    'encoder_h2':tf.Variable(tf.random_normal([n_hidden_1,n_hidden_2])),
    
    'decoder_h1':tf.Variable(tf.random_normal([n_hidden_2,n_hidden_1])),
    'decoder_h2':tf.Variable(tf.random_normal([n_hidden_1,n_input]))
}
biases = {
    'encoder_b1':tf.Variable(tf.random_normal([n_hidden_1])),
    'encoder_b2':tf.Variable(tf.random_normal([n_hidden_2])),
    
    'decoder_b1':tf.Variable(tf.random_normal([n_hidden_1])),
    'decoder_b2':tf.Variable(tf.random_normal([n_input]))
}

#building the encoder
def encoder(x):
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x,weights['encoder_h1']),biases['encoder_b1']))
    #encoder hidden layer with sigmoid activation #2
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1,weights['encoder_h2']),biases['encoder_b2']))
    return layer_2

def decoder(x):
    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']))
    return layer_2

#construct model
encoder_op = encoder(X)
decoder_op = decoder(encoder_op)

#prediction
y_pred = decoder_op
#targets(Labels)are the input data
y_ture = X

#define loss and optimizer ,minimize the squared error
cost = tf.reduce_mean(tf.pow(y_ture - y_pred,2))
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

#initializing the variable
init = tf.global_variables_initializer()

#launch the graph
with tf.Session() as sess:
    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 optimizer 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("optimizer 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 thier reconstruction
    %matplotlib qt5
    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()

代码段2:

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',one_hot=False)

#Visualize decoder setting
#parameters
learning_rate = 0.001
training_epochs = 20
batch_size = 256
display_step = 1

#Network parameters
n_input = 784    #MNIST data input (img shape :28*28)

#tf.graph input (only pictures)
X = tf.placeholder('float',[None,n_input])

#hidden layer settings
n_hidden_1 = 256    #1st layer num features
n_hidden_2 = 64    #2rd layer num features
n_hidden_3 = 10
n_hidden_4 = 2

weights = {
    'encoder_h1':tf.Variable(tf.truncated_normal([n_input,n_hidden_1])),
    'encoder_h2':tf.Variable(tf.truncated_normal([n_hidden_1,n_hidden_2])),
    'encoder_h3':tf.Variable(tf.truncated_normal([n_hidden_2,n_hidden_3])),
    'encoder_h4':tf.Variable(tf.truncated_normal([n_hidden_3,n_hidden_4])),
    
    'decoder_h1':tf.Variable(tf.truncated_normal([n_hidden_4,n_hidden_3])),
    'decoder_h2':tf.Variable(tf.truncated_normal([n_hidden_3,n_hidden_2])),
    'decoder_h3':tf.Variable(tf.truncated_normal([n_hidden_2,n_hidden_1])),
    'decoder_h4':tf.Variable(tf.truncated_normal([n_hidden_1,n_input]))
}
biases = {
    'encoder_b1':tf.Variable(tf.random_normal([n_hidden_1],)),
    'encoder_b2':tf.Variable(tf.random_normal([n_hidden_2],)),
    'encoder_b3':tf.Variable(tf.random_normal([n_hidden_3],)),
    'encoder_b4':tf.Variable(tf.random_normal([n_hidden_4],)),
    
    'decoder_b1':tf.Variable(tf.random_normal([n_hidden_3],)),
    'decoder_b2':tf.Variable(tf.random_normal([n_hidden_2],)),
    'decoder_b3':tf.Variable(tf.random_normal([n_hidden_1],)),
    'decoder_b4':tf.Variable(tf.random_normal([n_input],))
}

#building the encoder
def encoder(x):
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x,weights['encoder_h1']),biases['encoder_b1']))
    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']))
    #在第四层时,输出量不再是 [0,1] 范围内的数,而是将数据通过默认的 Linear activation function 调整为 (-∞,∞) 
    layer_4 = tf.add(tf.matmul(layer_3,weights['encoder_h4']),biases['encoder_b4'])
    return layer_4

def decoder(x):
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x,weights['decoder_h1']),biases['decoder_b1']))
    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.nn.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_ture = X

#define loss and optimizer ,minimize the squared error
cost = tf.reduce_mean(tf.pow(y_ture - y_pred,2))
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

#initializing the variable
init = tf.global_variables_initializer()

#launch the graph
with tf.Session() as sess:
    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 optimizer 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("optimizer finished")
    
    encoder_result = sess.run(encoder_op,feed_dict={X:mnist.test.images})
    %matplotlib qt5
    plt.scatter(encoder_result[:,0],encoder_result[:,1],c=mnist.test.labels)
    plt.show()

区别在于是否输出为2D效果图。由于使用的是jupyter notebook,所以在展示图形画面时需使用%matplotlib qt5
代码可直接运行,故不再粘贴结果及其figure。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值