目录
输出结果
网络异常,图片无法展示
|
代码设计
1. import tensorflow as tf 2. import numpy as np 3. import matplotlib.pyplot as plt 4. 5. #Import MNIST data 6. from tensorflow.examples.tutorials.mnist import input_data 7. mnist=input_data.read_data_sets("/niu/mnist_data/",one_hot=False) 8. 9. 10. # Parameter 11. learning_rate = 0.01 12. training_epochs = 10 13. batch_size = 256 14. display_step = 1 15. examples_to_show = 10 16. 17. # Network Parameters 18. n_input = 784 19. 20. #tf Graph input(only pictures) 21. X=tf.placeholder("float", [None,n_input]) 22. 23. # hidden layer settings 24. n_hidden_1 = 256 25. n_hidden_2 = 128 <br> 26. weights = { 27. 'encoder_h1':tf.Variable(tf.random_normal([n_input,n_hidden_1])), 28. 'encoder_h2': tf.Variable(tf.random_normal([n_hidden_1,n_hidden_2])), 29. 'decoder_h1': tf.Variable(tf.random_normal([n_hidden_2,n_hidden_1])), 30. 'decoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_input])), 31. } 32. biases = { 33. 'encoder_b1': tf.Variable(tf.random_normal([n_hidden_1])), 34. 'encoder_b2': tf.Variable(tf.random_normal([n_hidden_2])), 35. 'decoder_b1': tf.Variable(tf.random_normal([n_hidden_1])), 36. 'decoder_b2': tf.Variable(tf.random_normal([n_input])), 37. } 38. 39. #定义encoder 40. def encoder(x): 41. # Encoder Hidden layer with sigmoid activation #1 42. layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['encoder_h1']), 43. biases['encoder_b1'])) 44. # Decoder Hidden layer with sigmoid activation #2 45. layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['encoder_h2']), 46. biases['encoder_b2'])) 47. return layer_2 48. 49. #定义decoder 50. def decoder(x): 51. # Encoder Hidden layer with sigmoid activation #1 52. layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['decoder_h1']), 53. biases['decoder_b1'])) 54. # Decoder Hidden layer with sigmoid activation #2 55. layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['decoder_h2']), 56. biases['decoder_b2'])) 57. return layer_2 58. 59. # Construct model 60. encoder_op = encoder(X) # 128 Features 61. decoder_op = decoder(encoder_op) # 784 Features 62. 63. # Prediction 64. y_pred = decoder_op 65. # Targets (Labels) are the input data. 66. y_true = X 67. 68. # Define loss and optimizer, minimize the squared error 69. 70. cost = tf.reduce_mean(tf.pow(y_true - y_pred, 2)) 71. optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost) 72. 73. # Launch the graph 74. with tf.Session() as sess:<br> 75. sess.run(tf.initialize_all_variables()) 76. total_batch = int(mnist.train.num_examples/batch_size) 77. # Training cycle 78. for epoch in range(training_epochs): 79. # Loop over all batches 80. for i in range(total_batch): 81. batch_xs, batch_ys = mnist.train.next_batch(batch_size) # max(x) = 1, min(x) = 0 82. # Run optimization op (backprop) and cost op (to get loss value) 83. _, c = sess.run([optimizer, cost], feed_dict={X: batch_xs}) 84. # Display logs per epoch step 85. if epoch % display_step == 0: 86. print("Epoch:", '%04d' % (epoch+1), 87. "cost=", "{:.9f}".format(c)) 88. 89. print("Optimization Finished!") 90. # # Applying encode and decode over test set 91. encode_decode = sess.run( 92. y_pred, feed_dict={X: mnist.test.images[:examples_to_show]}) 93. # Compare original images with their reconstructions 94. f, a = plt.subplots(2, 10, figsize=(10, 2)) 95. plt.title('Matplotlib,AE--Jason Niu') 96. for i in range(examples_to_show): 97. a[0][i].imshow(np.reshape(mnist.test.images[i], (28, 28))) 98. a[1][i].imshow(np.reshape(encode_decode[i], (28, 28))) 99. plt.show()