Skip to content

Instantly share code, notes, and snippets.

@arisliang
Created March 2, 2018 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arisliang/64d1bbdddc5eba0fd6cb9d3ba0ccabce to your computer and use it in GitHub Desktop.
Save arisliang/64d1bbdddc5eba0fd6cb9d3ba0ccabce to your computer and use it in GitHub Desktop.
play tensorlayer book Issue 11
"""
ch 3.5
https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_mnist.py
code for https://github.com/tensorlayer/chinese-book/issues/11
"""
import time
import numpy as np
import tensorflow as tf
import tensorlayer as tl
import matplotlib as mpl
mpl.use('TkAgg')
X_train, y_train, X_val, y_val, X_test, y_test = \
tl.files.load_mnist_dataset(shape=(-1, 784))
X_train = np.asarray(X_train, dtype=np.float32)
y_train = np.asarray(y_train, dtype=np.int32)
X_val = np.asarray(X_val, dtype=np.float32)
y_val = np.asarray(y_val, dtype=np.int32)
X_test = np.asarray(X_test, dtype=np.float32)
y_test = np.asarray(y_test, dtype=np.int32)
print('X_train.shape', X_train.shape)
print('y_train.shape', y_train.shape)
print('X_val.shape', X_val.shape)
print('y_val.shape', y_val.shape)
print('X_test.shape', X_test.shape)
print('y_test.shape', y_test.shape)
print('X %s y %s' % (X_test.dtype, y_test.dtype))
save_path = './ae_tl/autoencoder2.ckpt'
image_width = 28
saver = None
# 模型结构参数
hidden_size = 196
input_size = 784
def main_layers(model='relu', is_train=True, reuse=False):
"""build network
:param model: 模型类别,可选Sigmoid或Relu
"""
with tf.variable_scope("ae", reuse=reuse):
# set model reuse
tl.layers.set_name_reuse(reuse)
# # 定义模型
x = tf.placeholder(tf.float32, shape=[None, input_size], name='x')
# 输入层 f(x)
network = tl.layers.InputLayer(x, name='input')
# ch3.4 introduce noise to input data
network = tl.layers.DropoutLayer(network, keep=0.5, is_train=is_train, is_fix=False, name='denoising1')
print('Build Network')
if model == 'relu':
network = tl.layers.DenseLayer(network, hidden_size, tf.nn.relu, name='relu1')
# 隐层输出
encoded_img = network.outputs
# 重构层输出 g(h)
# recon_layer1 = tl.layers.DenseLayer(network, input_size, tf.nn.softplus, name='recon_layer1')
recon_layer1 = tl.layers.ReconLayer(network, x_recon=x, n_units=784, act=tf.nn.softplus,
name='recon_layer1')
elif model == 'sigmoid':
network = tl.layers.DenseLayer(network, hidden_size, tf.nn.sigmoid, name='sigmoid1')
# 隐层输出
encoded_img = network.outputs
# 重构层输出 g(h)
# recon_layer1 = tl.layers.DenseLayer(network, input_size, tf.nn.sigmoid, name='recon_layer1')
recon_layer1 = tl.layers.ReconLayer(network, x_recon=x, n_units=784, act=tf.nn.sigmoid,
name='recon_layer1')
return x, recon_layer1
def train_layers(model='relu'):
global saver
# 定义超参数
n_epochs = 10
batch_size = 128
print_interval = 200
x, recon_layer1 = main_layers(model, is_train=True, reuse=False)
saver = tf.train.Saver()
with tf.Session() as sess:
tl.layers.initialize_global_variables(sess)
recon_layer1.pretrain(sess, x=x, X_train=X_train, X_val=X_val, denoise_name='ae/denoising1', n_epoch=n_epochs,
batch_size=batch_size, print_freq=print_interval, save=True, save_name='w1pre_')
# 保存模型为TensorFlow的ckpt格式
saver.save(sess, save_path=save_path)
print('model saved.')
if __name__ == '__main__':
all_start_time = time.time()
model = 'sigmoid'
train_layers(model=model)
print('all finished took %.2fs' % (time.time() - all_start_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment