티스토리 뷰
이번에는 MNIST 예제를 이용해서 Tensorboard를 사용해 보겠습니다.
Tensorboard에 사용되는 데이터를 생성하기 위해서는 Summary라는 기능을 사용합니다.
자세한 설명은 아래에 있네요
https://www.tensorflow.org/api_docs/python/summary/
지난 포스트에서 사용했던 mnist 예제에 코드를 추가해 Tensorboard를 사용 해 보겠습니다.
기존 코드에서 tensorboard를 위해 추가한 부분을 굵은 글씨로 표시 했습니다.
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
import tensorflow as tf
with tf.name_scope("input") as scope:
x = tf.placeholder(tf.float32, [None, 784])
with tf.name_scope("weight") as scope:
W = tf.Variable(tf.zeros([784, 10]))
with tf.name_scope("bias") as scope:
b = tf.Variable(tf.zeros([10]))
with tf.name_scope("layer1") as scope:
y = tf.nn.softmax(tf.matmul(x, W) + b)
w_hist = tf.summary.histogram("weight", W)
b_hist = tf.summary.histogram("bias", b)
y_hist = tf.summary.histogram("y", y)
with tf.name_scope("y_") as scope:
y_ = tf.placeholder(tf.float32, [None, 10])
with tf.name_scope("cost") as scope:
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
cost_sum = tf.summary.scalar("cost",cross_entropy)
with tf.name_scope("train") as scope:
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
with tf.Session() as sess:
merged = tf.summary.merge_all()
writer =tf.summary.FileWriter("./board/mnist", sess.graph)
init = tf.global_variables_initializer()
sess.run(init)
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
if i % 10 == 0:
summary = sess.run(merged, feed_dict={x: batch_xs, y_: batch_ys})
writer.add_summary(summary,i)
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
코드를 실행한 결과는 이전 실행결과와 같습니다.
이제 생성된 로그를 이용해서 tensorboard를 실행 해 봅니다.
실행 방법은 이전 포스팅을 참조 해 주세요
텐서보드 실행 결과 아래 그래프 들이 얻어집니다.
SCALARS
상수값으로 저장한 Cost가 그래프로 그려집니다.
Cost의 변화 상태를 보면서 학습이 정상적으로 이루어 지는지 확인 할 수 있겠습니다.
Graphs
학습 모델의 연결 상태를 시각화 해서 볼 수 있습니다.
각 블럭들을 누르면 내용이 보입니다.
복잡한 연결을 블럭별로 나누어서 연결이 유효한지 확인하는데 사용하면 좋을 것 같습니다.
Distributions
Bias와 Weight가 학습이 진행되면서 분산되어 가는 것을 볼 수 있습니다.
Histograms
Bias와 Weight는 Vector 이기 때문에 여러개의 값이 겹쳐서 보입니다.
tensorboard를 위한 코드를 살펴보면
1. 해당 연산또는 변수의 이름을 지정
변수도 가능하고 연산도 이름을 지정하는 것이 가능합니다.
네트워크 층의 연결 그래프를 그리는 데 사용됩니다.
with tf.name_scope("input") as scope:
2. 학습에 사용된 변수나 학습 결과 값, Loss, 정확도등의 출력을 원할 때 사용
tf.summary.histogram
tf.summary.Scalar
3. log들을 하나로 통합
merged = tf.summary.merge_all()
4. log 저장 경로 지정
writer =tf.summary.FileWriter("./board/mnist", sess.graph)
5. 학습을 진행하며 결과를 저장
summary = sess.run(merged, feed_dict={x: batch_xs, y_: batch_ys})
writer.add_summary(summary,i)
이 중에서 1번 과정을 하지 않아도 그래프는 그려집니다.
아래 코드를 실행 시키고 (이름을 지정하지 않고)
텐서보드를 실행시키면
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
cost_sum = tf.summary.scalar("cost",cross_entropy)
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
with tf.Session() as sess:
merged = tf.summary.merge_all()
writer =tf.summary.FileWriter("./board/mnist", sess.graph)
init = tf.global_variables_initializer()
sess.run(init)
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
if i % 10 == 0:
summary = sess.run(merged, feed_dict={x: batch_xs, y_: batch_ys})
writer.add_summary(summary,i)
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
아래와 같이 모듈들이 묶여있지 않고 풀어져서 보입니다.
이 예제와 같이 간단한 네트워크에서는 괜찮지만 학습층이 많이 늘어나면
눈으로 확인하는데 어려움이 있고 그래서 name_scope를 이용해 묶어주는 것으로 생각됩니다.
텐서보드를 사용해 보니 학습에서 변화하는 값이나 연결상태를 보고 싶을 때
그것을 출력해주는 툴 이라는 것을 알게 되었습니다.
'Tensorflow Step By Step' 카테고리의 다른 글
Tensorflow CSV File Read 2 (0) | 2017.02.22 |
---|---|
Tensorflow CSV File Read 1 (0) | 2017.02.16 |
Tensorboard 사용하기 1 (5) | 2017.02.12 |
Tensorflow Tutorial (MNIST for beginer) (0) | 2017.01.27 |
Windows Tensorflow 환경 구축 2 (Tensorflow 설치) (3) | 2017.01.26 |
- Total
- Today
- Yesterday