Skip to content

Instantly share code, notes, and snippets.

@janhuenermann
Created May 9, 2018 08:13
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 janhuenermann/b284470c3ebd481b81ce02e122a6eac2 to your computer and use it in GitHub Desktop.
Save janhuenermann/b284470c3ebd481b81ce02e122a6eac2 to your computer and use it in GitHub Desktop.
TensorFlow using MNIST. Using standard TF APIs, in 30 lines of code.
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
session = tf.Session()
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
input = tf.placeholder(tf.float32, shape=(None, 784))
x = tf.layers.dense(input, 128, activation=tf.nn.relu) # layer 1
x = tf.layers.dense(x, 128, activation=tf.nn.relu) # layer 2
x = tf.layers.dense(x, 10, activation=None) # layer 3
y = tf.nn.softmax(x)
label = tf.placeholder(tf.float32, (None, 10))
loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(labels=label, logits=x) )
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
train_op = optimizer.minimize(loss)
session.run(tf.global_variables_initializer())
for i in range(0, 10000):
batch_xs, batch_ys = mnist.train.next_batch(64)
_, current_loss = session.run([train_op, loss], feed_dict={ input: batch_xs, label: batch_ys })
print("[%d.] Current Loss: %f" % (i, current_loss))
batch_xs, batch_ys = mnist.train.next_batch(1)
_y = session.run(y, feed_dict={ input: batch_xs })
print("Model prediction: %s \nActual label: %s" % (_y[0], batch_ys[0]))
session.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment