Created
January 9, 2017 14:49
-
-
Save Trion129/2cc3886301712da06f20c593798f2707 to your computer and use it in GitHub Desktop.
Stochastic Gradient Descent and 2 layer neural network
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
batch_size = 128 | |
valid_dataset = valid_dataset[:batch_size] | |
valid_labels = valid_labels[:batch_size] | |
test_dataset = test_dataset[:batch_size] | |
test_labels = test_labels[:batch_size] | |
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, image_size * image_size)) | |
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) | |
tf_valid_dataset = tf.constant(valid_dataset) | |
tf_test_dataset = tf.constant(test_dataset) | |
# Variables. | |
weights1 = tf.Variable( | |
tf.truncated_normal([image_size * image_size, batch_size])) | |
biases1 = tf.Variable(tf.zeros([batch_size])) | |
weights2 = tf.Variable( | |
tf.truncated_normal([batch_size, num_labels])) | |
biases2 = tf.Variable(tf.zeros([batch_size, num_labels])) | |
# Training computation. | |
layer1 = tf.matmul(tf_train_dataset, weights1) + biases1 | |
layer1 = tf.nn.relu(layer1) | |
layer2 = tf.matmul(layer1, weights2) + biases2 | |
loss = tf.reduce_mean( | |
tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=layer2)) | |
# Optimizer. | |
optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) | |
# Predictions for the training, validation, and test data. | |
train_prediction = tf.nn.softmax(layer2) | |
valid_prediction = tf.nn.softmax( | |
tf.matmul(tf.nn.relu( | |
tf.matmul(tf_valid_dataset, weights1) + biases1), weights2) + biases2) | |
test_prediction = tf.nn.softmax( | |
tf.matmul(tf.nn.relu( | |
tf.matmul(tf_test_dataset, weights1) + biases1), weights2) + biases2) | |
num_steps = 3001 | |
with tf.Session(graph=graph) as session: | |
tf.global_variables_initializer().run() | |
print("Initialized") | |
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} | |
_, l, predictions = session.run( | |
[optimizer, loss, train_prediction], feed_dict=feed_dict) | |
if (step % 500 == 0): | |
print("Minibatch loss at step %d: %f" % (step, l)) | |
print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) | |
print("Validation accuracy: %.1f%%" % accuracy( | |
valid_prediction.eval(), valid_labels)) | |
print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment