Skip to content

Commit 43a8c04

Browse files
committed
multiple hidden layers
1 parent 4bf1816 commit 43a8c04

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

NeuralNetsImplementation/MultiLayerPerceptron.py

+36-21
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
X_train = mnist.train.images
1616
Y_train = mnist.train.labels
1717

18-
# X_train = X_train[:100, :]
19-
# Y_train = Y_train[:100, :]
20-
2118
# (5000, 784)
2219
X_validate = mnist.validation.images
2320
Y_validate = mnist.validation.labels
@@ -28,34 +25,47 @@
2825

2926
# details about the network
3027
input_layer = 784 # 28 * 28 images flattened
31-
hidden_layer = 256
28+
hidden_layer_1 = 256
29+
hidden_layer_2 = 256
3230
output_layer = 10
31+
3332
print '\nNetwork details...'
3433
print 'Input size: ', input_layer
35-
print 'Hidden layer units: ', hidden_layer
34+
print 'Hidden layer 1 units: ', hidden_layer_1
35+
print 'Hidden layer 2 units: ', hidden_layer_2
3636
print 'Output layer units: ', output_layer
3737

3838
# graph input
3939
x = tf.placeholder(tf.float32, [None, input_layer])
4040
y = tf.placeholder(tf.float32, [None, output_layer])
4141

4242
# model weights
43-
print '\nInitialising random weights and biases'
44-
w_hidden_vals = tf.random_normal([input_layer, hidden_layer])
45-
b_hidden_vals = tf.random_normal([hidden_layer])
46-
w_hidden = tf.Variable(w_hidden_vals, name='hidden_weights')
47-
b_hidden = tf.Variable(b_hidden_vals, name='hidden_bias')
43+
print '\nInitialising random weights and biases...'
44+
w_hidden1_vals = tf.random_normal([input_layer, hidden_layer_1])
45+
b_hidden1_vals = tf.random_normal([hidden_layer_1])
46+
w_hidden1 = tf.Variable(w_hidden1_vals, name='hidden1_weights')
47+
b_hidden1 = tf.Variable(b_hidden1_vals, name='hidden1_bias')
48+
49+
w_hidden2_vals = tf.random_normal([hidden_layer_1, hidden_layer_2])
50+
b_hidden2_vals = tf.random_normal([hidden_layer_2])
51+
w_hidden2 = tf.Variable(w_hidden2_vals, name='hidden2_weights')
52+
b_hidden2 = tf.Variable(b_hidden2_vals, name='hidden2_bias')
4853

49-
w_output_vals = tf.random_normal([hidden_layer, output_layer])
54+
55+
w_output_vals = tf.random_normal([hidden_layer_2, output_layer])
5056
b_output_vals = tf.random_normal([output_layer])
5157
w_output = tf.Variable(w_output_vals, name='output_weights')
5258
b_output = tf.Variable(b_output_vals, name='output_bias')
5359

54-
# model for a multi-layer-perceptron with single hidden layer
55-
# ReLU activation for the hidden layer
56-
hidden_activations = tf.nn.relu(tf.add(tf.matmul(x, w_hidden), b_hidden))
57-
# softmax activation for the output layer
58-
output_activations = tf.add(tf.matmul(hidden_activations, w_output), b_output)
60+
# model for a multi-layer-perceptron with two hidden layers
61+
# ReLU activations for the first hidden layer
62+
hidden1_activations = tf.nn.relu(tf.add(tf.matmul(x, w_hidden1), b_hidden1))
63+
64+
#ReLU activations for the second hidden layer
65+
hidden2_activations = tf.nn.relu(tf.add(tf.matmul(hidden1_activations, w_hidden2), b_hidden2))
66+
67+
# linear activations for the output layer
68+
output_activations = tf.add(tf.matmul(hidden2_activations, w_output), b_output)
5969

6070
# using negative log-likelihood as the cost function
6171
# cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(output_activations), reduction_indices=1))
@@ -71,14 +81,17 @@
7181
# other variables
7282
batch_size = 100
7383
cost_vec = []
74-
training_epochs = 25
84+
training_epochs = 15
7585

7686
# launch the graph
7787
print '\nLaunching the graph...'
7888
with tf.Session() as sess:
7989
sess.run(init_op)
8090

8191
total_batches = int(mnist.train.num_examples / batch_size)
92+
print 'Implementing batchwise stochastic gradient descent...'
93+
print 'batch size: ', batch_size
94+
print 'Total number of batches: ', total_batches
8295

8396
for epoch in xrange(training_epochs):
8497
avg_cost = 0
@@ -121,9 +134,11 @@
121134
print 'Test accuracy: ', sess.run(accuracy, feed_dict={x: X_test, y: Y_test}) * 100
122135

123136
print '\nSaving the parameters...'
124-
np.savez('./w_hidden', w_hidden=sess.run(w_hidden))
125-
np.savez('./b_hidden', b_hidden=sess.run(b_hidden))
126-
np.savez('./w_output', w_output=sess.run(w_output))
127-
np.savez('./b_output', b_output=sess.run(b_output))
137+
np.savez('./Params/w_hidden1', w_hidden1=sess.run(w_hidden1))
138+
np.savez('./Params/b_hidden1', b_hidden1=sess.run(b_hidden1))
139+
np.savez('./Params/w_hidden2', w_hidden2=sess.run(w_hidden2))
140+
np.savez('./Params/b_hidden2', b_hidden2=sess.run(b_hidden2))
141+
np.savez('./Params/w_output', w_output=sess.run(w_output))
142+
np.savez('./Params/b_output', b_output=sess.run(b_output))
128143

129144
print '\nTotal time taken: ', time.time() - init_time

0 commit comments

Comments
 (0)