Skip to content

Instantly share code, notes, and snippets.

@dongkwan-kim
Created May 18, 2019 17:27
Show Gist options
  • Save dongkwan-kim/d141c8fff11750513ff2f7edb385d590 to your computer and use it in GitHub Desktop.
Save dongkwan-kim/d141c8fff11750513ff2f7edb385d590 to your computer and use it in GitHub Desktop.
# https://github.com/golbin/TensorFlow-Tutorials/blob/master/06%20-%20MNIST/01%20-%20MNIST.py
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./mnist/data/", one_hot=True)
X = tf.placeholder(tf.float32, [None, 784])
Y = tf.placeholder(tf.float32, [None, 10])
W1 = tf.Variable(tf.random_normal([784, 256], stddev=0.01))
L1 = tf.nn.relu(tf.matmul(X, W1))
W2 = tf.Variable(tf.random_normal([256, 256], stddev=0.01))
L2 = tf.nn.relu(tf.matmul(L1, W2))
W3 = tf.Variable(tf.random_normal([256, 10], stddev=0.01))
model = tf.matmul(L2, W3)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=model, labels=Y))
optimizer = tf.train.AdamOptimizer(0.001).minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
batch_size = 100
total_batch = int(mnist.train.num_examples / batch_size)
for epoch in range(5):
total_cost = 0
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
_, cost_val = sess.run([optimizer, loss], feed_dict={X: batch_xs, Y: batch_ys})
total_cost += cost_val
print('Epoch:', '%04d' % (epoch + 1),
'Avg. cost =', '{:.3f}'.format(total_cost / total_batch))
print('최적화 완료!')
is_correct = tf.equal(tf.argmax(model, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
print('정확도:',
sess.run(accuracy, feed_dict={X: mnist.test.images,
Y: mnist.test.labels}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment