-
Notifications
You must be signed in to change notification settings - Fork 0
/
src
134 lines (96 loc) · 3.4 KB
/
src
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
## Reading the data
df=input_data.read_data_sets("/tmp/data/",one_hot=True)
###Neural Network Parameters
### 28*28=784
num_inputs = 784
num_h1 = 256
num_h2 = 128
###Tensorflow data inputs
X=tf.placeholder("float",shape=[None,num_inputs])
### Neural Network training parameters
batch_size=256
num_steps=40000
learning_rate=5e-1
display_step=1000
###Creating Network Architechture
def encoder_layer(x):
l1=tf.matmul(x,W["w1"])
l1=tf.add(l1,b["b1"])
l1=tf.nn.sigmoid(l1)
l1=tf.matmul(l1,W["w2"])
l1=tf.add(l1,b["b2"])
l1=tf.nn.sigmoid(l1)
return l1
def decoder_layer(x):
l2=tf.matmul(x,W["w3"])
l2=tf.add(l2,b["b3"])
l2=tf.nn.sigmoid(l2)
l2=tf.matmul(l2,W["w4"])
l2=tf.add(l2,b["b4"])
l2=tf.nn.sigmoid(l2)
return l2
###Model Architechture Weights
W={"w1":tf.Variable(tf.random_normal([num_inputs,num_h1])),
"w2": tf.Variable(tf.random_normal([num_h1,num_h2])),
"w3": tf.Variable(tf.random_normal([num_h2,num_h1])),
"w4": tf.Variable(tf.random_normal([num_h1,num_inputs]))}
b={"b1":tf.Variable(tf.random_normal([num_h1])),
"b2":tf.Variable(tf.random_normal([num_h2])),
"b3":tf.Variable(tf.random_normal([num_h1])),
"b4":tf.Variable(tf.random_normal([num_inputs]))}
###Model Architechture
encoder_fun = encoder_layer(X)
decoder_fun = decoder_layer(encoder_fun)
####Cost function Evaluation
# Prediction
predicted = decoder_fun
#Actual
actual=X
cost_fn=tf.reduce_mean(tf.pow(actual - predicted, 2))
optim=tf.train.RMSPropOptimizer(learning_rate=learning_rate)
training=optim.minimize(cost_fn)
# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()
###Staring the Model training Session
with tf.Session() as sess:
# Run the initializer
sess.run(init)
for step in range(1, num_steps+1):
batch_x, _ = df.train.next_batch(batch_size)
# Run optimization op (backprop)
sess.run(training, feed_dict={X: batch_x})
if step % display_step == 0 or step == 1:
# Calculate batch loss and accuracy
loss, _ = sess.run([cost_fn, training], feed_dict={X: batch_x})
print("Step " + str(step) + ", Minibatch Loss= " + \
"{:.4f}".format(loss))
print("Optimization Finished!")
# Testing
# Encode and decode images from test set and visualize their reconstruction.
n = 4
canvas_orig = np.empty((28 * n, 28 * n))
canvas_recon = np.empty((28 * n, 28 * n))
for i in range(n):
batch_x, _ = df.test.next_batch(n)
# Session
g = sess.run(decoder_fun, feed_dict={X: batch_x})
# original images
for j in range(n):
# Draw the generated digits
canvas_orig[i * 28:(i + 1) * 28, j * 28:(j + 1) * 28] = batch_x[j].reshape([28, 28])
# reconstructed images
for j in range(n):
# Draw the generated digits
canvas_recon[i * 28:(i + 1) * 28, j * 28:(j + 1) * 28] = g[j].reshape([28, 28])
print("Original Images")
plt.figure(figsize=(n, n))
plt.imshow(canvas_orig, origin="upper", cmap="gray")
plt.show()
print("Reconstructed Images")
plt.figure(figsize=(n, n))
plt.imshow(canvas_recon, origin="upper", cmap="gray")
plt.show()