tensorlayer学习日志5_chapter3_3.4

本文是TensorLayer学习的补充,聚焦于Chapter3的3.4部分,主要介绍了自编码器的概念。由于作者家中突发情况,导致这部分内容之前未发布。通过反复研读,作者对自编码器有了更深入的理解,并推荐了一个详细的知乎页面来帮助读者全面了解该主题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上周发现3.4给漏了,家里小孩突然发烧,医院里待了快一周,所以现在补上~~这几天来回第三章看了好几遍,终于对自编码器有一点了解,这个知乎网页说得很全:https://www.zhihu.com/question/41490383

import tensorflow as tf
import tensorlayer as tl
import numpy as np


learning_rate = 0.0001
lambda_l2_w = 0.01
n_epochs = 200
batch_size =128
print_interval = 200
hidden_size = 196
input_size = 784
image_width = 28
model = 'sigmoid'
# model = 'relu'

x = tf.placeholder(tf.float32, shape=[None, 784], name='x')

print('~~~~~~~~~~~~~~~~build network~~~~~~~~~~~~~~~~~')

if model == 'relu':
	network = tl.layers.InputLayer(x, name='input')
	network = tl.layers.DropoutLayer(network, keep=0.5, is_fix=True, name='denoising1')  #这里要加is_fix=True,否则默认是False会报错
	network = tl.layers.DenseLayer(network, hidden_size, tf.nn.relu, name='relu1')

	encoded_img = network.outputs
	recon_layer1 = tl.layers.DenseLayer(network, input_size, tf.nn.softplus, name='recon_layer1')

elif model == 'sigmoid':
	network = tl.layers.InputLayer(x, name='input')
	network = tl.layers.DropoutLayer(network, keep=0.5, is_fix=True, name='denoising1')  #这里要加is_fix=True,否则默认是False会报错
	network = tl.layers.DenseLayer(network, hidden_size, tf.nn.sigmoid, name='sigmoid1')

	encoded_img = network.outputs
	recon_layer1 = tl.layers.DenseLayer(network, input_size, tf.nn.sigmoid, name='recon_layer1')

y = recon_layer1.outputs
train_params = recon_layer1.all_params[-4:]

mse = tf.reduce_sum(tf.squared_difference(y, x), 1)
mse = tf.reduce_mean(mse)

L2_w = tf.contrib.layers.l2_regularizer(lambda_l2_w)(train_params[0] + tf.contrib.layers.l2_regularizer(lambda_l2_w)(train_params[2]))

activation_out = recon_layer1.all_layers[-2]
L1_a = 0.001 * tf.reduce_mean(activation_out)

beta = 5
rho = 0.15
p_hat = tf.reduce_mean(activation_out, 0)
KLD = beta * tf.reduce_sum(rho * tf.log(tf.divide(rho, p_hat)) + (1-rho) * tf.log((1-rho)/(tf.subtract(float(1), p_hat))))

if model == 'sigmoid':
	cost = mse + L2_w + KLD
if model == 'relu':
	cost = mse +L2_w +L1_a

train_op = tf.train.AdamOptimizer(learning_rate).minimize(cost)

saver = tf.train.Saver()

print('~~~~~~~~~~~模型训练~~~~~~~~~~~~~~~~~~')

X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1, 784))

total_batch = X_train.shape[0] // batch_size

with tf.Session() as sess:
	tl.layers.initialize_global_variables(sess)
	for epoch in range(n_epochs):
		avg_cost = 0.
		for i in range(total_batch):
			batch_x, batch_y = X_train[i*batch_size:(i+1)*batch_size], y_train[i*batch_size:(i+1)*batch_size]
			batch_x = np.array(batch_x).astype(np.float32)
			batch_cost,_ = sess.run([cost,train_op], feed_dict={x:batch_x})
			avg_cost += batch_cost

			if not i % print_interval:			
				print('Minibatch:    %03d | Cost:    %.3f' %(i+1, batch_cost))

		print('Epoch:    %03d | AvgCost:    %.3f' %(epoch+1, avg_cost/(i+1)) )
	saver.save(sess, save_path='./ae_tl_3.4/autoencoder-sigmoid.ckpt')
	# saver.save(sess, save_path='./ae_tl_3.4/autoencoder-relu.ckpt')		


print('~~~~~~~~~~~~~~~image_show~~~~~~~~~~~~~~~~')

import matplotlib.pyplot as plt

n_images = 15
fig, axes = plt.subplots(nrows=2, ncols=n_images, sharex=True, sharey=True, figsize=(20, 2.5))
test_images = X_test[:n_images]
with tf.Session() as sess:	
	saver.restore(sess, save_path='./ae_tl_3.4/autoencoder-sigmoid.ckpt')
	# saver.restore(sess, save_path='./ae_tl_3.4/autoencoder-relu.ckpt')
	decoded = sess.run(recon_layer1.outputs, feed_dict={x:test_images})
	if model =='relu':
		weights = sess.run(tl.layers.get_variables_with_name('relu/W:0', train_only=False, printable=True))
	elif model == 'sigmoid':
		weights = sess.run(tl.layers.get_variables_with_name('sigmoid/W:0', train_only=False, printable=True))
	recon_weights = sess.run(tl.layers.get_variables_with_name('recon_layer1/W:0', train_only=False, printable=True))
	recon_bias = sess.run(tl.layers.get_variables_with_name('recon_layer1/b:0', train_only=False, printable=True))

for i in range(n_images):
	for ax, img in zip(axes, [test_images, decoded]):
		ax[i].imshow(img[i].reshape((image_width, image_width)), cmap='binary')
plt.show()

 输出如下:

~~~~~~~~~~~~~~~~build network~~~~~~~~~~~~~~~~~
[TL] InputLayer  input: (?, 784)
[TL] DropoutLayer denoising1: keep:0.500000 is_fix:True
[TL] DenseLayer  sigmoid1: 196 sigmoid
[TL] DenseLayer  recon_layer1: 784 sigmoid
~~~~~~~~~~~模型训练~~~~~~~~~~~~~~~~~~
[TL] Load or Download MNIST > data\mnist
[TL] data\mnist\train-images-idx3-ubyte.gz
[TL] data\mnist\t10k-images-idx3-ubyte.gz
Minibatch:    001 | Cost:    27641.484
Minibatch:    201 | Cost:    13699.196
Epoch:    001 | AvgCost:    15177.753
Minibatch:    001 | Cost:    7864.798
Minibatch:    201 | Cost:    4753.522
Epoch:    002 | AvgCost:    5038.634
Minibatch:    001 | Cost:    3118.037
Minibatch:    201 | Cost:    2102.419
Epoch:    003 | AvgCost:    2185.543
Minibatch:    001 | Cost:    1498.473
Minibatch:    201 | Cost:    1089.769
Epoch:    004 | AvgCost:    1120.570
Minibatch:    001 | Cost:    826.790
Minibatch:    201 | Cost:    638.751
Epoch:    005 | AvgCost:    651.981
~~~~~~~
~~~~~~~
Minibatch:    001 | Cost:    47.045
Minibatch:    201 | Cost:    50.873
Epoch:    196 | AvgCost:    48.965
Minibatch:    001 | Cost:    47.466
Minibatch:    201 | Cost:    50.894
Epoch:    197 | AvgCost:    48.939
Minibatch:    001 | Cost:    46.919
Minibatch:    201 | Cost:    50.641
Epoch:    198 | AvgCost:    48.950
Minibatch:    001 | Cost:    47.575
Minibatch:    201 | Cost:    51.019
Epoch:    199 | AvgCost:    48.927
Minibatch:    001 | Cost:    46.787
Minibatch:    201 | Cost:    50.829
Epoch:    200 | AvgCost:    48.949
~~~~~~~~~~~~~~~image_show~~~~~~~~~~~~~~~~
INFO:tensorflow:Restoring parameters from ./ae_tl_3.4/autoencoder-relu.ckpt
[TL] Restoring parameters from ./ae_tl_3.4/autoencoder-relu.ckpt
[TL]   [*] geting variables with sigmoid/W:0
[TL]   [*] geting variables with recon_layer1/W:0
[TL]   got   0: recon_layer1/W:0   (196, 784)
[TL]   [*] geting variables with recon_layer1/b:0
[TL]   got   0: recon_layer1/b:0   (784,)
QWindowsWindow::setGeometry: Unable to set geometry 2000x317+4+23 on QWidgetWindow/'MainWindowClassWindow'. Resulting geometry:  1284x317+4+23 (frame: 4, 23, 4, 4, custom margin: 0, 0, 0, 0, minimum size: 69x67, maximum size: 16777215x16777215).
[Finished in 2003.1s]

还有个relu版的,可以自己试试~~

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值