11.mnist_matplotlib
In [1]:
import pandas as pd
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

plt.rcParams["axes.unicode_minus"] = False
plt.rcParams["figure.figsize"] = (12, 8)
In [3]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./mnist/data/", one_hot=True)
Extracting ./mnist/data/train-images-idx3-ubyte.gz
Extracting ./mnist/data/train-labels-idx1-ubyte.gz
Extracting ./mnist/data/t10k-images-idx3-ubyte.gz
Extracting ./mnist/data/t10k-labels-idx1-ubyte.gz


variable setting

In [4]:
global_step = tf.Variable(0, trainable=False, name="global_step")
X = tf.placeholder(tf.float32, shape=[None, 784], name="X")
Y = tf.placeholder(tf.float32, shape=[None,  10], name="Y")

W1 = tf.Variable(tf.random_normal([784, 256], mean=0, stddev=0.01), name="W1")
W2 = tf.Variable(tf.random_normal([256, 256], mean=0, stddev=0.01), name="W2")
W3 = tf.Variable(tf.random_normal([256,  10], mean=0, stddev=0.01), name="W3")

b1 = tf.zeros([256], name="bias1")
b2 = tf.zeros([256], name="bias2")
b3 = tf.zeros([10] , name="bais3")


model setting

In [5]:
keep_prob = tf.placeholder(tf.float32)

with tf.name_scope("layer1"):
    L1 = tf.add(tf.matmul(X, W1), b1)
    L1 = tf.nn.relu(L1)
    L1 = tf.nn.dropout(L1, keep_prob)
    
with tf.name_scope("layer2"):
    L2 = tf.add(tf.matmul(L1, W2), b2)
    L2 = tf.nn.relu(L2)
    L2 = tf.nn.dropout(L2, keep_prob)
    
with tf.name_scope("layer3"):
    model = tf.add(tf.matmul(L2, W3), b3)
    
with tf.name_scope("cost"):
    cost = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=model))
    opt = tf.train.AdamOptimizer(0.001).minimize(cost, global_step=global_step)
    
    tf.summary.scalar("cost", cost)


model initialization

In [6]:
init = tf.global_variables_initializer()
sess = tf.Session()

sess.run(init)

merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("./logs/mnist_matplotlib", sess.graph)
In [7]:
batch_size = 50
total_batch = int(mnist.train.num_examples / batch_size)
cost_epoch = []


model training

In [8]:
%%time
for epoch in range(20):
    total_cost = 0
    
    for i in range(total_batch):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        
        _, cost_val = sess.run([opt, cost], feed_dict={X:batch_xs, Y: batch_ys, keep_prob:0.8})
        total_cost += cost_val
        cost_epoch.append(total_cost)
        
        summary = sess.run(merged, feed_dict={X:batch_xs, Y: batch_ys, keep_prob:0.8})
        writer.add_summary(summary, global_step=sess.run(global_step))
        
    print("epoch: %d, Avg.cost: %.4f" % (
        epoch+1, total_cost / total_batch
    ))
epoch: 1, Avg.cost: 0.3481
epoch: 2, Avg.cost: 0.1395
epoch: 3, Avg.cost: 0.1000
epoch: 4, Avg.cost: 0.0806
epoch: 5, Avg.cost: 0.0697
epoch: 6, Avg.cost: 0.0591
epoch: 7, Avg.cost: 0.0507
epoch: 8, Avg.cost: 0.0455
epoch: 9, Avg.cost: 0.0417
epoch: 10, Avg.cost: 0.0394
epoch: 11, Avg.cost: 0.0362
epoch: 12, Avg.cost: 0.0361
epoch: 13, Avg.cost: 0.0305
epoch: 14, Avg.cost: 0.0303
epoch: 15, Avg.cost: 0.0271
epoch: 16, Avg.cost: 0.0282
epoch: 17, Avg.cost: 0.0267
epoch: 18, Avg.cost: 0.0267
epoch: 19, Avg.cost: 0.0219
epoch: 20, Avg.cost: 0.0238
CPU times: user 3min 22s, sys: 43.7 s, total: 4min 6s
Wall time: 2min 27s


cost function

In [9]:
plt.figure(figsize=(20, 8))
plt.plot(cost_epoch, "g")
plt.title("cost_epoch")
plt.show()


tensor graph

In [10]:
## jptensor.py 를 워킹디렉토리에 import
import jptensor as jp

tf_graph = tf.get_default_graph().as_graph_def()
jp.show_graph(tf_graph)


test

In [11]:
is_correct = tf.equal(tf.argmax(model, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))

accuracy_val = sess.run(accuracy, feed_dict={X: mnist.test.images, 
                                             Y: mnist.test.labels,
                                             keep_prob: 1})

print("accuracy: %.3f" % (accuracy_val))
accuracy: 0.980


labels

In [12]:
labels = sess.run(model, feed_dict={X: mnist.test.images,
                                    Y: mnist.test.labels,
                                    keep_prob: 1})
In [13]:
%matplotlib inline
fig = plt.figure()
for i in range(10):
    # (2, 5)의 그래프, i + 1번째 숫자 이미지 출력
    subplot = fig.add_subplot(2, 5, i+1)
    
    # x, y축 눈금 제거
    subplot.set_xticks([])
    subplot.set_yticks([])
    
    # 출력한 이미지 위에 예측한 숫자를 출력
    # np.argmax와 tf.argmax는 같은 기능
    # 결과값인 labels의 i번째 요소가 one-hot encoding으로 되어 있으므로
    # 해당 배열에서 가장 높은 값을 가진 인덱스를 예측한 숫자로 출력
    subplot.set_title("%d" % (np.argmax(labels[i])))
    
    # 1차원 배열로 되어 있는 i번째 이미지 데이터를
    # 28 x 28형태의 2차원 배열로 변환
    subplot.imshow(mnist.test.images[i].reshape((28, 28)))
plt.show()
In [14]:
from IPython.core.display import HTML, display

display(HTML("<style> .container{width:100% !important;}</style>"))

'Deep_Learning' 카테고리의 다른 글

13.auto-encoder  (0) 2018.12.15
12.mnist_cnn  (0) 2018.12.12
10.mnist_dropout  (0) 2018.12.10
00.write_csv  (0) 2018.12.09
09.mnist_01_minibatch  (0) 2018.12.09

+ Recent posts