In [ ]:
# Import libraries
import tensorflow as tf
import numpy as np
import time
import collections
import os
In [ ]:
# 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)
In [ ]:
# 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 = YOUR CODE HERE
In [ ]:
# computational graph variables
initial = tf.truncated_normal([d,nc], stddev=0.1); W = tf.Variable(initial); print('W=',W.get_shape())
b = YOUR CODE HERE
In [ ]:
# Construct CG / output value
y = YOUR CODE HERE; print('y1=',y,y.get_shape())
y += b; print('y2=',y,y.get_shape())
y = YOUR CODE HERE; print('y3=',y,y.get_shape())
In [ ]:
# Loss
cross_entropy = tf.reduce_mean(-tf.reduce_sum(YOUR CODE HERE * tf.log(YOUR CODE HERE), 1))
In [ ]:
reg_loss = YOUR CODE HERE
In [ ]:
reg_par = 1e-3
total_loss = YOUR CODE HERE
In [ ]:
# Update CG variables / backward pass
train_step = YOUR CODE HERE
In [ ]:
# Accuracy
correct_prediction = YOUR CODE HERE
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
In [ ]:
# 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]
In [ ]:
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)