Created
May 9, 2018 08:13
-
-
Save janhuenermann/b284470c3ebd481b81ce02e122a6eac2 to your computer and use it in GitHub Desktop.
TensorFlow using MNIST. Using standard TF APIs, in 30 lines of code.
This file contains 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
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