Skip to content

Instantly share code, notes, and snippets.

@eremo2002
Created March 28, 2018 03:37
Show Gist options
  • Save eremo2002/578d7e415d732d53877f393db852170f to your computer and use it in GitHub Desktop.
Save eremo2002/578d7e415d732d53877f393db852170f to your computer and use it in GitHub Desktop.
Deep Learning
import tensorflow as tf
import time
from tensorflow.examples.tutorials.mnist import input_data
tf.reset_default_graph()
mnist = input_data.read_data_sets("/data/", one_hot=True)
X = tf.placeholder(tf.float32, [None, 28, 28, 1])
Y = tf.placeholder(tf.float32, [None, 10])
W1 = tf.Variable(tf.random_normal(shape=[3, 3, 1, 32], stddev=0.01))
L1 = tf.nn.conv2d(X, W1, strides=[1, 1, 1, 1], padding='SAME')
L1 = tf.nn.relu(L1)
L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# max pooling까지 하면 Volume의 크기는 14x14x32
W2 = tf.Variable(tf.random_normal(shape=[3, 3, 32, 64], stddev=0.01))
L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME')
L2 = tf.nn.relu(L2)
L2 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# max pooling까지 하면 Volume의 크기는 7x7x64
# fully connected하기 위해 reshape
L3 = tf.reshape(L2, [-1, 7 * 7 * 64])
# 10개의 class로 분류하기 위해 Output 10
W3 = tf.Variable(tf.random_normal(shape=[7*7*64, 10], stddev=0.01))
b = tf.Variable(tf.random_normal([10]))
hyp = tf.matmul(L3, W3) + b
# cost함수와 최적화
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
logits=hyp, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
start = time.time()
total_batch = int(mnist.train.num_examples / 100)
batch_size = 100
# training
for i in range(10):
avg_cost = 0
for j in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
batch_xs = batch_xs.reshape(-1, 28, 28, 1)
c, _ = sess.run([cost, optimizer], feed_dict={X : batch_xs, Y : batch_ys})
avg_cost += c
avg_cost = avg_cost / total_batch
print("epoch : ", (i+1), " cost : ", '{:.5f}'.format(avg_cost))
print("트레이닝 완료")
prediction = tf.equal(tf.argmax(hyp, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32))
print("Accuracy : ", accuracy,
feed_dict={X : mnist.test.images.reshape(-1, 28, 28, 1), Y : mnist.test.labels})
print("수행시간 : ", '{.3f}'.format(time.time()-start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment