[tf]tensorboard的使用

2018-12-13  本文已影响2人  VanJordan
tensorboard --logdir=./  --port=54321
# 然后在浏览器
http://127.0.0.1:54321

使用tf.summary.scalar记录标量数据,使用tf.summary.histogram直接记录变量var的直方图。

with tf.name_scope('summaries'):
    mean = tf.reduce_mean(var)
    tf.summary.scalar('mean', mean)
    tf.summary.scalar('histogram', var)

对tf.summary进行汇总

with tf.name_scope('summaries'):
    mean = tf.reduce_mean(var)
    tf.summary.scalar('mean', mean)
    tf.summary.scalar('histogram', var)
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter(log_dir + '/train', sess.graph)
test_writer = tf.summary.FileWriter(log_dir + '/test')
for i in range(max_steps):
    if i % 10 == 0:
        accuracy = sess.run(model.accuracy, feed_dict=feed_dict)
        test_writer.add_summary(accuracy, i)
    else :
        if i % 100 == 99:
            run_options = tf.RunOptions(trace_level = tf.RunOptions.FULL_TRACE)
            run_metadata = tf.RunMetadata()
            summary,_ = sess.run([merged, train_step],feed_dict = feed_dict(True),
                                 options = run_options, run_metadata = run_metadata)
            train_writer.add_run_metadata(run_metadata, 'step%03d' % i )
            train_writer.add_summary(summary, i )
            saver.save(sess, log_dir + '/model.ckpt', i)
        else:
            summary,_ = sess.run([merged, train_step], feed_dict = feed_dict(True))
            train_writer.add_summary(summary, i)
train_writer.close()
test_writer.close()

使用tf.summary.image记录图片数据

log_dir = './log/'
with tf.name_scope('input'):
    x = tf.placeholder(tf.float32,[None,784], name = 'x-input')
    y = tf.placeholder(tf.float32, [None, 10], name = 'y-input')
with tf.name_scope('input_reshape'):
    image_shaped_input = tf.reshape(x,[-1, 28, 28, 1])
    tf.summary.image('input', image_shaped_input, 10)

EMBEDDINGS 窗口可以看到降维以后的嵌入向量可视化效果

这是TensorBoard中的Embedding Projector功能,只要我们使用tf.train.Saver保存了整个模型,那么就可以使用TensorBoard自动对模型中所有的二维Variable进行可视化,(TensorFlow中只有Variable可以被保存,而Tenor是不可的),因此如果我们想要可视化Tensor我们可以选择T-SNE或者PCA等算法对数据的列特征进行降维,并且在3D或者2D的坐标中展示可视化展示。如果我们的模型是Word2Vec计算或Language Model,那么TensorBoardEMBEDDINGS可视化功能会变得非常有用。

添加自己的数据到TensorBoard显示

通常情况下,我们在训练网络的时候添加summary都是通过如下方式。

tf.scalar_summary(tags, values)
# ...
summary_op = tf.summary.merge_all()
summary_writer = tf.summary.FileWriter(logdir, graph=sess.graph)
summary_str = sess.run(summary_op)
summary_writer.add_summary(summary_str, global_step)
summary_writer = tf.summary.FileWriter(LOGDIR)
summary = tf.Summary()
summary.value.add(tag="summary_tag", simple_value=0)
summary.value.add(tag="summary_tag2", simple_value=1)
# step代表横轴坐标
summary_writer.add_summary(summary, step)
上一篇下一篇

猜你喜欢

热点阅读