forked from frsong/tf-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnist_cnn.py
More file actions
162 lines (127 loc) · 4.4 KB
/
mnist_cnn.py
File metadata and controls
162 lines (127 loc) · 4.4 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""
MNIST handwritten digit classification with a convolutional neural network,
based on
https://www.tensorflow.org/get_started/mnist/pros
"""
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# Load the data
data = input_data.read_data_sets('datasets/mnist', one_hot=True)
#-------------------------------------------------------------------------------
# Model
#-------------------------------------------------------------------------------
def weight(shape):
W = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(W, name='W')
def bias(shape):
b = tf.constant(0.1, shape=shape)
return tf.Variable(b, name='b')
def conv(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
def inference(x):
# Reshape input
x = tf.reshape(x, [-1, 28, 28, 1])
# First convolutional + pooling layer
with tf.name_scope('conv1'):
W = weight([5, 5, 1, 32])
b = bias([32])
x = conv(x, W) + b
x = tf.nn.relu(x)
x = max_pool(x) # 14x14
# Second convolutional + pooling layer
with tf.name_scope('conv2'):
W = weight([5, 5, 32, 64])
b = bias([64])
x = conv(x, W) + b
x = tf.nn.relu(x)
x = max_pool(x) # 7x7
# Flatten feature planes
x = tf.reshape(x, [-1, 7*7*64])
# Fully connected layer
with tf.name_scope('fc'):
W = weight([7*7*64, 1024])
b = bias([1024])
x = tf.matmul(x, W) + b
x = tf.nn.relu(x)
# Softmax layer
with tf.name_scope('softmax'):
W = weight([1024, 10])
b = bias([10])
x = tf.matmul(x, W) + b
return x
# Seed the TF random number generator for reproducible initialization
tf.set_random_seed(0)
# For feeding in data
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
# Define the model
logits = inference(x)
# Prediction
predict_op = tf.argmax(logits, 1)
correct_op = tf.equal(predict_op, tf.argmax(y, 1))
accuracy_op = tf.reduce_mean(tf.cast(correct_op, tf.float32))
#-------------------------------------------------------------------------------
# Train
#-------------------------------------------------------------------------------
# Loss function
loss = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=logits)
loss = tf.reduce_mean(loss)
# Hyperparameters
learning_rate = 1e-4
num_epochs = 20
batch_size = 50
# Optimizer
optimizer = tf.train.AdamOptimizer(learning_rate)
train_op = optimizer.minimize(loss)
# Seed the random number generator for reproducible batches
np.random.seed(0)
# Print list of variables
print("")
print("Variables")
print("---------")
variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
num_params = 0
for v in variables:
num_params += np.prod(v.get_shape().as_list())
print(v.name, v.get_shape())
print("=> Total number of parameters =", num_params)
# TF session
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# Minimize the loss function
num_batches_per_epoch = data.train.num_examples // batch_size
for epoch in range(num_epochs):
# Present one mini-batch at a time
for _ in range(num_batches_per_epoch):
batch = data.train.next_batch(batch_size)
feed_dict = {x: batch[0], y: batch[1]}
sess.run(train_op, feed_dict)
# Progress report
feed_dict = {x: data.validation.images, y: data.validation.labels}
accuracy = sess.run(accuracy_op, feed_dict)
print("After {} epochs, validation accuracy = {}".format(epoch+1, accuracy))
# Test accuracy
feed_dict = {x: data.test.images, y: data.test.labels}
accuracy = sess.run(accuracy_op, feed_dict)
print("Test accuracy =", accuracy)
def predict(images):
feed_dict = {x: images}
return sess.run(predict_op, feed_dict)
#-------------------------------------------------------------------------------
# Use the model to make predictions
#-------------------------------------------------------------------------------
idx = 0
image = data.test.images[idx]
label = data.test.labels[idx]
predicted_label = predict([image])[0]
# Plot image with label
plt.imshow(image.reshape((28, 28)), cmap='gray')
plt.title("True label: {}, predicted: {}"
.format(label.argmax(), predicted_label))
# Save figure
plt.savefig('figs/mnist_cnn.png')