Skip to content

Instantly share code, notes, and snippets.

@aahoo
Last active April 20, 2016 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aahoo/c42130e82b45233c07cd1319a26f2cdd to your computer and use it in GitHub Desktop.
Save aahoo/c42130e82b45233c07cd1319a26f2cdd to your computer and use it in GitHub Desktop.
Udacity deep learning course assignment 3 problem 4
# download the file @ https://www.dropbox.com/s/urqmc4jgt66hbef/notMNIST.pickle?dl=0
pickle_file = 'notMNIST.pickle'
from time import strftime
from math import sqrt
from __future__ import print_function
import numpy as np
import tensorflow as tf
from six.moves import cPickle as pickle
#load data
with open(pickle_file, 'rb') as f:
save = pickle.load(f)
train_dataset = save['train_dataset']
train_labels = save['train_labels']
valid_dataset = save['valid_dataset']
valid_labels = save['valid_labels']
test_dataset = save['test_dataset']
test_labels = save['test_labels']
del save # hint to help gc free up memory
print('Training set', train_dataset.shape, train_labels.shape)
print('Validation set', valid_dataset.shape, valid_labels.shape)
print('Test set', test_dataset.shape, test_labels.shape)
# reformat
image_size = 28
num_labels = 10
def reformat(dataset, labels):
dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32)
# Map 2 to [0.0, 1.0, 0.0 ...], 3 to [0.0, 0.0, 1.0 ...]
labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
return dataset, labels
train_dataset, train_labels = reformat(train_dataset, train_labels)
valid_dataset, valid_labels = reformat(valid_dataset, valid_labels)
test_dataset, test_labels = reformat(test_dataset, test_labels)
print('Training set', train_dataset.shape, train_labels.shape)
print('Validation set', valid_dataset.shape, valid_labels.shape)
print('Test set', test_dataset.shape, test_labels.shape)
# accuracy calculation function
def accuracy(predictions, labels):
return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
/ predictions.shape[0])
batch_size = 128
weights = []
biases = []
dims = [image_size*image_size, 1024, 305, 75, num_labels]
graph = tf.Graph()
with graph.as_default():
# Input data. For the training data, we use a placeholder that will be fed
# at run time with a training minibatch.
tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, dims[0]))
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, dims[-1]))
tf_valid_dataset = tf.constant(valid_dataset)
tf_test_dataset = tf.constant(test_dataset)
# variables
for i in range(len(dims) - 1):
stddev = sqrt(2/dims[i])
weights.append(tf.Variable(tf.truncated_normal([dims[i], dims[i+1]], stddev=stddev)))
biases.append(tf.Variable(tf.zeros([dims[i+1]])))
keep_prob = tf.placeholder(tf.float32)
# prediction
def predict(_dataset, _weights, _biases, keep_prob=1.0):
_output = tf.matmul(_dataset, _weights[0]) + _biases[0]
for _w, _b in zip(_weights[1:], _biases[1:]):
_output = tf.matmul(tf.nn.dropout(tf.nn.relu(_output), keep_prob), _w) + _b
return _output
train_prediction = predict(tf_train_dataset, weights, biases, keep_prob=keep_prob)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(train_prediction, tf_train_labels))
# regularization with l2 loss
# beta = .002
# l2_loss = 0
# for w in weights:
# l2_loss += tf.nn.l2_loss(w)
# loss += beta * l2_loss
# Optimizer.
global_step = tf.Variable(0, trainable=False) # count the number of steps taken.
learning_rate = tf.train.exponential_decay(0.1, global_step, 3000, 0.96)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
# Predictions for the validation and test data.
valid_prediction = tf.nn.softmax(predict(tf_valid_dataset, weights, biases))
test_prediction = tf.nn.softmax(predict(tf_test_dataset, weights, biases))
num_epochs = 128
num_steps = num_epochs * train_labels.shape[0] // batch_size + 1
with tf.Session(graph=graph) as session:
tf.initialize_all_variables().run()
print("Initialized. Total steps: %d\n" % num_steps)
print(" time step offset minibatch loss minibatch accuracy validation accuracy")
for step in range(num_steps):
# Pick an offset within the training data, which has been randomized.
# Note: we could use better randomization across epochs.
offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
# Generate a minibatch.
batch_data = train_dataset[offset:(offset + batch_size), :]
batch_labels = train_labels[offset:(offset + batch_size), :]
# Prepare a dictionary telling the session where to feed the minibatch.
# The key of the dictionary is the placeholder node of the graph to be fed,
# and the value is the numpy array to feed to it.
feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels, keep_prob: 0.75}
_, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict)
if (step % (num_steps//20) == 0):
print("%s %6d %6d %14.1f %17.1f%% %18.1f%%" %
(strftime("%H:%M:%S"), step, offset, l, accuracy(predictions, batch_labels),
accuracy(valid_prediction.eval(feed_dict={keep_prob:1.0}), valid_labels)))
print("Test accuracy: %.1f%%" %
accuracy(test_prediction.eval(feed_dict={keep_prob:1.0}), test_labels))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment