TensorFlow实战 1 自编码器(Autoencoder)

       自编码器(Autoencoder):使用无监督方法,通过自身高阶特征编码自己,是一种输入和输出“基本一致”的神经网络。

自编码器使用少量稀疏的高阶特征重构输出,同时附加限制:

       1)如果限制中间隐藏层节点数量,使得中间层节点数量小于输入\输出节点数量,则为降维过程;如果给中间层加L1正则,则可根据惩罚系数控制中间层节点稀疏程度,惩罚系数越大,特征组合越稀疏,特征数量越少。

       2)如果给数据加入噪声,则为去噪自编码器(Denoizing Autoencoder),模型将从噪声中学习数据特征。

       自编码器的用途(function):

      1)去噪

       2)降维

      

代码实现:

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

#import mnist data sets
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot = False)

#define parameters
learning_rate = 0.001
training_epochs = 10
batch_size = 256
display_step = 1
examples_to_show = 10
n_input = 784

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

#number of parameters in hidden layer
n_hidden_1 = 256
n_hidden_2 = 128

#create the weights and biases dict for encoder and decoder
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])),
}

#define 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']))
	return layer_2

#define decoder
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']))
	return layer_2

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

#define prediction
y_pred = decoder_op
y_true = X

#define cost function and optimizer
cost = tf.reduce_mean(tf.pow((y_true - y_pred),2))
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

#optimization and display results
with tf.Session() as sess:
	#initialize variables
	init = tf.global_variables_initializer()
	sess.run(init)
	total_batch = int(mnist.train.num_examples / batch_size)
	for eoch in range(training_epochs):
		for i in range(total_batch):
			batch_xs, batch_ys = mnist.train.next_batch(batch_size)
			_, c = sess.run([optimizer, cost], feed_dict = {X:batch_xs})
		if eoch % display_step == 0:
			print("Eoch : ", '%04d' %(eoch + 1), "Cost = ", "{:.9f}".format(c))
	print("optimization finished !")
	
	#plot soure and result image
	encode_decode = sess.run(y_pred, feed_dict = {X:mnist.test.images[:examples_to_show]})
	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()

practice makes perfect!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值