这第3.5节中的渐变,我尝试了很多次都无法跑出来,有没大神路过的帮忙一下?
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('~~~~~~~~~~~~~~~15图展示~~~~~~~~~~~~~~~~')
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')
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=True, printable=True))
elif model == 'sigmoid':
weights = sess.run(tl.layers.get_variables_with_name('sigmoid/W:0', train_only=True, printable=True))
recon_weights = sess.run(tl.layers.get_variables_with_name('recon_layer1/W:0', train_only=True, printable=True))
recon_bias = sess.run(tl.layers.get_variables_with_name('recon_layer1/b:0', train_only=True, 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()
print('~~~~~~~~~~~~~~~2到9的渐变~~~~~~~~~~~~')
import matplotlib.pyplot as plt
sample1_idx = 1
sample2_idx = 12
sample1 = test_images[sample1_idx].reshape([-1,784])
sample2 = test_images[sample2_idx].reshape([-1,784])
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, save_path='./ae_tl_3.4/autoencoder-sigmoid.ckpt')
dp_dict = tl.utils.dict_to_one(recon_layer1.all_drop)
feed_dict = {x:sample1}
feed_dict.update(dp_dict)
encoded1 = sess.run(encoded_img, feed_dict=feed_dict)
feed_dict = {x:sample2}
feed_dict.update(dp_dict)
encoded2 = sess.run(encoded_img, feed_dict=feed_dict)
encoded = tf.placeholder(tf.float32, shape = [None, hidden_size],name='encoded')
recon_weights, recon_bias = recon_weights[-1],recon_bias[-1]
test_network = tl.layers.InputLayer(encoded, name='test_input')
test_recon_layer1 = tl.layers.DenseLayer(test_network, input_size, act=tf.nn.sigmoid, name='test_recon_layer1')
diff = encoded2-encoded1
num_inter = 10
delta = diff/num_inter
encoded_all = encoded1
for i in range(1, num_inter+1):
encoded_all = np.vstack((encoded_all, encoded1 + delta*i))
with tf.Session() as sess:
tl.layers.initialize_global_variables(sess)
decoded_all = sess.run(test_recon_layer1.outputs, feed_dict={encoded:encoded_all})
fig, axes = plt.subplots(nrows=1, ncols=num_inter+1, sharex=True, sharey=True,figsize=(15,1.5))
for i in range(num_inter+1):
axes[i].imshow(decoded_all[i].reshape((image_width, image_width)), cmap='binary')
plt.show()
15图展示,没有问题,但是2到9的渐变就是出不来,是不是这里我加错了东东?
with tf.Session() as sess:
tl.layers.initialize_global_variables(sess) #自己加的
decoded_all = sess.run(test_recon_layer1.outputs, feed_dict={encoded:encoded_all})
但要是不加的话会报错:Attempting to use uninitialized value test_recon_layer1/b
加上的话,却只以得到11张雪花图~~
要是有大神路过,帮忙指点下哦~~~~