Skip to content

Instantly share code, notes, and snippets.

@qbx2
Last active August 19, 2016 10:05
Show Gist options
  • Save qbx2/d1c34fd0c37257bd59768faa46a957b0 to your computer and use it in GitHub Desktop.
Save qbx2/d1c34fd0c37257bd59768faa46a957b0 to your computer and use it in GitHub Desktop.
import gzip
import tensorflow as tf
import struct
import numpy as np
import random
def one_hot_encode(i):
ret = [0] * 10
ret[i] = 1
return ret
def load_idx1_file(filename):
with gzip.GzipFile(filename) as f:
magic_number, num_items = struct.unpack('>LL', f.read(8))
assert magic_number == 0x0801
print('Loading %s (%d)'%(filename, num_items))
return [one_hot_encode(i) for i in f.read(num_items)]
def load_idx3_file(filename):
with gzip.GzipFile(filename) as f:
magic_number, num_images, num_rows, num_cols = struct.unpack('>LLLL', f.read(4 * 4))
assert magic_number == 0x0803
print('Loading %s (%d, %dx%d)'%(filename, num_images, num_rows, num_cols))
imgs = [(np.frombuffer(f.read(num_rows * num_cols), dtype=np.byte)&0xff)/255. for _ in range(num_images)]
return imgs
training_set_labels = load_idx1_file('mnist_data/train-labels-idx1-ubyte.gz')
training_set_images = load_idx3_file('mnist_data/train-images-idx3-ubyte.gz')
training_set = list(zip(training_set_images, training_set_labels))
test_set_labels = load_idx1_file('mnist_data/t10k-labels-idx1-ubyte.gz')
test_set_images = load_idx3_file('mnist_data/t10k-images-idx3-ubyte.gz')
test_set = list(zip(test_set_images, test_set_labels))
# model
placeholder_x = tf.placeholder('float', shape=[None, 28 * 28]) # images
placeholder_y = tf.placeholder('float', shape=[None, 10]) # labels
W = tf.Variable(tf.random_normal(shape=[28 * 28, 10], stddev=.1))
b = tf.Variable(tf.zeros([10]))
pred = tf.nn.softmax(tf.matmul(placeholder_x, W) + b)
loss = -tf.reduce_sum(placeholder_y * tf.log(pred)) # cross entropy
train_step = tf.train.GradientDescentOptimizer(.01).minimize(loss)
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(placeholder_y, 1), tf.argmax(pred, 1)), tf.float32))
sess = tf.Session()
sess.run(tf.initialize_all_variables())
for i in range(10000):
x, y = zip(*random.sample(training_set, 100)) # sample 100 training data
_, tmp_loss, tmp_accuracy = sess.run([train_step, loss, accuracy], feed_dict={placeholder_x: x, placeholder_y: y})
if i % 100 == 0:
print('training avg_loss: {}, avg_accuracy: {}'.format(tmp_loss/100., tmp_accuracy))
x, y = zip(*test_set)
tmp_loss, tmp_accuracy = sess.run([loss, accuracy], feed_dict={placeholder_x: x, placeholder_y: y})
print('test avg_loss: {}, avg_accuracy: {}'.format(tmp_loss/len(test_set), tmp_accuracy))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment