使用tensorboard可视化loss和acc

1.用try...except...避免因版本不同出现导入错误问题
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.将代码写入作用域(作用域不影响代码的运行)
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()
merged = merge_summary([loss_summary, acc_summary])

4.定义保存路径(在sess中完成)
writer = SummaryWriter('save-cnn20/logs', sess.graph)

5.训练模型的同时训练变量集合merged(在sess中完成,counter为计数,每训练一次增加1)
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.开头的文件,以下通过终端操作。
cd save
tensorboard --logdir=logs
终端会出现一个网址,复制到浏览器中打开就能看见tensorboard储存的图像了。(若打开后无数据或图像,检查 --logdir后面的文件夹名字是否给错了。)

你可能感兴趣的:(图像深度学习)