1.用try...except...避免因版本不同出现导入错误问题
1
2
3
4
5
6
7
8
9
10
11
12
|
try :
image_summary = tf.image_summary
scalar_summary = tf.scalar_summary
histogram_summary = tf.histogram_summary
merge_summary = tf.merge_summary
SummaryWriter = tf.train.SummaryWriter
except :
image_summary = tf.summary.image
scalar_summary = tf.summary.scalar
histogram_summary = tf.summary.histogram
merge_summary = tf.summary.merge
SummaryWriter = tf.summary.FileWriter
|
2.将代码写入作用域(作用域不影响代码的运行)
1
2
3
4
5
6
7
|
with tf.name_scope( 'loss' ):
loss = - tf.reduce_sum(y * tf.log(y_conv))
loss_summary = scalar_summary( 'loss' , loss)
with tf.name_scope( 'accuracy' ):
accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float' ))
acc_summary = scalar_summary( 'accuracy' , accuracy)
|
3.将要保存的变量存在一起
另外可使用 tf.merge_all_summaries() 或者 tf.summary.merge_all()
1
|
merged = merge_summary([loss_summary, acc_summary])
|
4.定义保存路径(在sess中完成)
1
|
writer = SummaryWriter( 'save-cnn20/logs' , sess.graph)
|
5.训练模型的同时训练变量集合merged(在sess中完成,counter为计数,每训练一次增加1)
1
2
3
|
summary, _ = sess.run([merged, train_step], feed_dict = {x:x_batch, y:y_batch})
counter + = 1
writer.add_summary(summary, counter)
|
6.训练完成后在 save/logs 文件夹里面会有一个events.out.开头的文件,以下通过终端操作。
1
2
|
cd save
tensorboard - - logdir = logs
|
终端会出现一个网址,复制到浏览器中打开就能看见tensorboard储存的图像了。(若打开后无数据或图像,检查 --logdir后面的文件夹名字是否给错了。)
以上这篇使用tensorboard可视化loss和acc的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_39674098/article/details/79242073