-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf.TensorBoard_basic2.py
102 lines (77 loc) · 3.38 KB
/
tf.TensorBoard_basic2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
# 텐서보드를 이용하기 위해 각종 변수들을 설정하고 저장하는 방법을 익혀봅니다.
import tensorflow as tf
import numpy as np
# 적용시킬 data를 가져온다.
data = np.loadtxt('./data.csv', delimiter=',',
unpack=True, dtype='float32')
x_data = np.transpose(data[0:2])
y_data = np.transpose(data[2:])
####
# 신경망 모델 구성
####
global_step = tf.Variable(0, trainable=False, name='global_step')
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
# with tf.name_scope 으로 묶은 블럭은 텐서보드에서 한 레이어안에 표현해줍니다
with tf.name_scope('layer1'):
W1 = tf.Variable(tf.random_uniform([2, 10], -1., 1.), name='W1')
L1 = tf.nn.relu(tf.matmul(X, W1))
# 추가코드
tf.summary.histogram("X", X)
tf.summary.histogram("Weights", W1)
with tf.name_scope('layer2'):
W2 = tf.Variable(tf.random_uniform([10, 20], -1., 1.), name='W2')
L2 = tf.nn.relu(tf.matmul(L1, W2))
# 추가코드
tf.summary.histogram("Weights", W2)
with tf.name_scope('output'):
W3 = tf.Variable(tf.random_uniform([20, 3], -1., 1.), name='W3')
model = tf.matmul(L2, W3)
# 추가코드
tf.summary.histogram("Weights", W3)
tf.summary.histogram("Model", model)
with tf.name_scope('optimizer'):
cost = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=model))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost, global_step=global_step)
# tf.summary.scalar 를 이용해 수집하고 싶은 값들을 지정할 수 있습니다.
tf.summary.scalar('cost', cost)
#########
# 신경망 모델 학습
######
sess = tf.Session()
saver = tf.train.Saver(tf.global_variables())
ckpt = tf.train.get_checkpoint_state('./model')
if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
saver.restore(sess, ckpt.model_checkpoint_path)
else:
sess.run(tf.global_variables_initializer())
# 텐서보드에서 표시해주기 위한 텐서들을 수집합니다.
merged = tf.summary.merge_all()
# 저장할 그래프와 텐서값들을 저장할 디렉토리를 설정합니다.
writer = tf.summary.FileWriter('./logs/tb_basic2', sess.graph)
# 이렇게 저장한 로그는, 학습 후 다음의 명령어를 이용해 웹서버를 실행시킨 뒤
# tensorboard --logdir=./logs
# 다음 주소와 웹브라우저를 이용해 텐서보드에서 확인할 수 있습니다.
# http://localhost:6006
# 최적화 진행
for step in range(2000):
sess.run(optimizer, feed_dict={X: x_data, Y: y_data})
print('Step: %d, ' % sess.run(global_step),
'Cost: %.3f' % sess.run(cost, feed_dict={X: x_data, Y: y_data}))
# 적절한 시점에 저장할 값들을 수집하고 저장합니다.
summary = sess.run(merged, feed_dict={X: x_data, Y: y_data})
writer.add_summary(summary, global_step=sess.run(global_step))
saver.save(sess, './model/dnn.ckpt', global_step=global_step)
#########
# 결과 확인
# 0: 기타 1: 포유류, 2: 조류
######
prediction = tf.argmax(model, 1)
target = tf.argmax(Y, 1)
print('예측값:', sess.run(prediction, feed_dict={X: x_data}))
print('실제값:', sess.run(target, feed_dict={Y: y_data}))
is_correct = tf.equal(prediction, target)
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
print('정확도: %.2f' % sess.run(accuracy * 100, feed_dict={X: x_data, Y: y_data}))