Skip to content

Instantly share code, notes, and snippets.

@igaiga
Created June 21, 2016 00:04
Show Gist options
  • Save igaiga/3b85773cdaf5e81c8629465363b53d8f to your computer and use it in GitHub Desktop.
Save igaiga/3b85773cdaf5e81c8629465363b53d8f to your computer and use it in GitHub Desktop.
# TensorFlow tutorial
# "MNIST For ML Beginners"
# https://www.tensorflow.org/versions/r0.9/tutorials/mnist/beginners/index.html
# "Deep MNIST for Experts"
# https://www.tensorflow.org/versions/r0.9/tutorials/mnist/pros/index.html
# prepare input data(MNIST_data) before running
# https://www.tensorflow.org/versions/r0.9/tutorials/mnist/beginners/index.html
# from tensorflow.examples.tutorials.mnist import input_data
# mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# Load input data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
sess.run(tf.initialize_all_variables())
# Predicted Class and Cost Function
y = tf.nn.softmax(tf.matmul(x,W) + b)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
for i in range(1000):
batch = mnist.train.next_batch(50)
train_step.run(feed_dict={x: batch[0], y_: batch[1]})
# Evaluate model
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
#=> 0.9092
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment