[딥러닝] 텐서플로우 모델저장 / 텐서보드 이용하기
텐서플로우 모델 저장 및 재사용, 텐서보드 사용하기
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))
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))
with tf.name_scope('output'):
W3 = tf.Variable(tf.random_uniform([20, 3], -1., 1.), name='W3')
model = tf.matmul(L2, W3)
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.01)
train_op = optimizer.minimize(cost, global_step=global_step)
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', sess.graph) # 저장디렉터리 설정
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)
텐서보드 실행하기
tensorboard –logdir=./logs
http://localhost:6006
최근 댓글