# Import libraries
import tensorflow as tf
import numpy as np
import time
import collections
import os
# Import MNIST data with TensorFlow
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(os.path.join('datasets', 'mnist'), one_hot=True) # load data in local folder
train_data = mnist.train.images.astype(np.float32)
train_labels = mnist.train.labels
test_data = mnist.test.images.astype(np.float32)
test_labels = mnist.test.labels
print(train_data.shape)
print(train_labels.shape)
print(test_data.shape)
print(test_labels.shape)
Extracting datasets/mnist/train-images-idx3-ubyte.gz Extracting datasets/mnist/train-labels-idx1-ubyte.gz Extracting datasets/mnist/t10k-images-idx3-ubyte.gz Extracting datasets/mnist/t10k-labels-idx1-ubyte.gz (55000, 784) (55000, 10) (10000, 784) (10000, 10)
Hint: You may use the function tf.placeholder()
# computational graph inputs
batch_size = 100
d = train_data.shape[1]
nc = 10
x = tf.placeholder(tf.float32,[batch_size,d]); print('x=',x,x.get_shape())
y_label = tf.placeholder(tf.float32,[batch_size,nc]); print('y_label=',y_label,y_label.get_shape())
x= Tensor("Placeholder:0", shape=(100, 784), dtype=float32) (100, 784) y_label= Tensor("Placeholder_1:0", shape=(100, 10), dtype=float32) (100, 10)
Hint: You may use the function tf.Variable(), tf.truncated_normal()
# computational graph variables
initial = tf.truncated_normal([d,nc], stddev=0.1); W = tf.Variable(initial); print('W=',W.get_shape())
b = tf.Variable(tf.zeros([nc],tf.float32)); print('b=',b.get_shape())
W= (784, 10) b= (10,)
Hint: You may use the function tf.matmul(), tf.nn.softmax()
# Construct CG / output value
y = tf.matmul(x, W); print('y1=',y,y.get_shape())
y += b; print('y2=',y,y.get_shape())
y = tf.nn.softmax(y); print('y3=',y,y.get_shape())
y1= Tensor("MatMul:0", shape=(100, 10), dtype=float32) (100, 10) y2= Tensor("add:0", shape=(100, 10), dtype=float32) (100, 10) y3= Tensor("Softmax:0", shape=(100, 10), dtype=float32) (100, 10)
Hint: You may use the function tf.Variable(), tf.truncated_normal()
# Loss
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_label * tf.log(y), 1))
Hint: You may use the function tf.nn.l2_loss()
reg_loss = tf.nn.l2_loss(W)
reg_loss += tf.nn.l2_loss(b)
reg_par = 1e-3
total_loss = cross_entropy + reg_par* reg_loss
Hint: You may use the function tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss)
# Update CG variables / backward pass
train_step = tf.train.GradientDescentOptimizer(0.25).minimize(total_loss)
Hint: You may use the function tf.equal(tf.argmax(y,1), tf.argmax(y_label,1)) and tf.reduce_mean()
# Accuracy
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_label,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
Check out the accuracy of test set
# Create test set
idx = np.random.permutation(test_data.shape[0]) # rand permutation
idx = idx[:batch_size]
test_x, test_y = test_data[idx,:], test_labels[idx]
n = train_data.shape[0]
indices = collections.deque()
# Running Computational Graph
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(50):
# Batch extraction
if len(indices) < batch_size:
indices.extend(np.random.permutation(n)) # rand permutation
idx = [indices.popleft() for i in range(batch_size)] # extract n_batch data
batch_x, batch_y = train_data[idx,:], train_labels[idx]
# Run CG for variable training
_,acc_train,total_loss_o = sess.run([train_step,accuracy,total_loss], feed_dict={x: batch_x, y_label: batch_y})
print('\nIteration i=',i,', train accuracy=',acc_train,', loss=',total_loss_o)
# Run CG for testset
acc_test = sess.run(accuracy, feed_dict={x: test_x, y_label: test_y})
print('test accuracy=',acc_test)
Iteration i= 0 , train accuracy= 0.1 , loss= 2.61576 test accuracy= 0.16 Iteration i= 1 , train accuracy= 0.12 , loss= 2.25159 test accuracy= 0.33 Iteration i= 2 , train accuracy= 0.3 , loss= 2.16757 test accuracy= 0.36 Iteration i= 3 , train accuracy= 0.38 , loss= 1.88768 test accuracy= 0.51 Iteration i= 4 , train accuracy= 0.5 , loss= 1.70732 test accuracy= 0.55 Iteration i= 5 , train accuracy= 0.59 , loss= 1.52506 test accuracy= 0.62 Iteration i= 6 , train accuracy= 0.53 , loss= 1.57437 test accuracy= 0.68 Iteration i= 7 , train accuracy= 0.62 , loss= 1.34377 test accuracy= 0.69 Iteration i= 8 , train accuracy= 0.64 , loss= 1.36525 test accuracy= 0.72 Iteration i= 9 , train accuracy= 0.68 , loss= 1.25514 test accuracy= 0.72 Iteration i= 10 , train accuracy= 0.63 , loss= 1.22931 test accuracy= 0.68 Iteration i= 11 , train accuracy= 0.66 , loss= 1.17098 test accuracy= 0.74 Iteration i= 12 , train accuracy= 0.68 , loss= 1.24049 test accuracy= 0.75 Iteration i= 13 , train accuracy= 0.71 , loss= 1.10969 test accuracy= 0.84 Iteration i= 14 , train accuracy= 0.86 , loss= 0.891327 test accuracy= 0.79 Iteration i= 15 , train accuracy= 0.78 , loss= 0.980348 test accuracy= 0.76 Iteration i= 16 , train accuracy= 0.74 , loss= 1.00763 test accuracy= 0.79 Iteration i= 17 , train accuracy= 0.75 , loss= 0.942338 test accuracy= 0.83 Iteration i= 18 , train accuracy= 0.84 , loss= 0.807404 test accuracy= 0.83 Iteration i= 19 , train accuracy= 0.82 , loss= 0.828357 test accuracy= 0.86 Iteration i= 20 , train accuracy= 0.82 , loss= 0.817 test accuracy= 0.83 Iteration i= 21 , train accuracy= 0.78 , loss= 0.844171 test accuracy= 0.86 Iteration i= 22 , train accuracy= 0.81 , loss= 0.837174 test accuracy= 0.85 Iteration i= 23 , train accuracy= 0.78 , loss= 0.974055 test accuracy= 0.81 Iteration i= 24 , train accuracy= 0.81 , loss= 0.809938 test accuracy= 0.85 Iteration i= 25 , train accuracy= 0.9 , loss= 0.642673 test accuracy= 0.85 Iteration i= 26 , train accuracy= 0.74 , loss= 0.901251 test accuracy= 0.87 Iteration i= 27 , train accuracy= 0.71 , loss= 1.01265 test accuracy= 0.85 Iteration i= 28 , train accuracy= 0.83 , loss= 0.750725 test accuracy= 0.86 Iteration i= 29 , train accuracy= 0.85 , loss= 0.667404 test accuracy= 0.88 Iteration i= 30 , train accuracy= 0.8 , loss= 0.719768 test accuracy= 0.81 Iteration i= 31 , train accuracy= 0.82 , loss= 0.680213 test accuracy= 0.84 Iteration i= 32 , train accuracy= 0.75 , loss= 0.878949 test accuracy= 0.85 Iteration i= 33 , train accuracy= 0.8 , loss= 0.787001 test accuracy= 0.84 Iteration i= 34 , train accuracy= 0.87 , loss= 0.620717 test accuracy= 0.87 Iteration i= 35 , train accuracy= 0.8 , loss= 0.723095 test accuracy= 0.83 Iteration i= 36 , train accuracy= 0.83 , loss= 0.719377 test accuracy= 0.85 Iteration i= 37 , train accuracy= 0.8 , loss= 0.716804 test accuracy= 0.84 Iteration i= 38 , train accuracy= 0.88 , loss= 0.591883 test accuracy= 0.85 Iteration i= 39 , train accuracy= 0.82 , loss= 0.74946 test accuracy= 0.86 Iteration i= 40 , train accuracy= 0.84 , loss= 0.657294 test accuracy= 0.86 Iteration i= 41 , train accuracy= 0.83 , loss= 0.613971 test accuracy= 0.84 Iteration i= 42 , train accuracy= 0.86 , loss= 0.646873 test accuracy= 0.86 Iteration i= 43 , train accuracy= 0.9 , loss= 0.529555 test accuracy= 0.84 Iteration i= 44 , train accuracy= 0.83 , loss= 0.648258 test accuracy= 0.85 Iteration i= 45 , train accuracy= 0.83 , loss= 0.608415 test accuracy= 0.85 Iteration i= 46 , train accuracy= 0.89 , loss= 0.620664 test accuracy= 0.85 Iteration i= 47 , train accuracy= 0.83 , loss= 0.653725 test accuracy= 0.85 Iteration i= 48 , train accuracy= 0.83 , loss= 0.686351 test accuracy= 0.85 Iteration i= 49 , train accuracy= 0.84 , loss= 0.608361 test accuracy= 0.85