Tensorboard 在同一个 scalar 图中绘制出多次试验中某个变量随时间推移的变化情况

一、环境

TensorFlow API r1.12

CUDA 9.2 V9.2.148

cudnn64_7.dll

Python 3.6.3

Windows 10、Ubuntu 16.04

 

二、代码示例

在命令行中运行:tensorboard --help

得到提示如下:

https://github.com/tensorflow/tensorboard

方法一:tensorboard --logdir="./logdir/"

TensorBoard 会以指定的 logdir 文件夹为根目录,递归地查找 logdir 下所有文件夹下的 .*tfevents.* 日志文件

方法二:tensorboard --logdir=name1:"./logdir/log_1/",name2:"./logdir/log_2/"

也可以在每个日志文件夹前添加一个 names,通过冒号连接对应的日志文件夹

方法三:tensorboard --logdir= "./logdir/log_1/", "./logdir/log_2/"(官方说可以但是不能使用的方法)

也可以使用逗号分隔的多个日志文件夹列表,TensorBoard 会逐个查找这几个指定文件夹下的 .*tfevents.* 日志文件

官方 github上虽然说明可以使用该形式,但是会在命令行一直提示“W0109 17:53:48.568122 Reloader plugin_event_multiplexer.py:139] Conflict for name .: old path C:\Users\WJW\logs\plot_1, new path C:\Users\WJW\logs\plot_2”的信息,并且在 TensorBoard 页面上也只显示 log_1 下的相关数据对象,该问题应该还是一个设计 bug 吧,具体问题分析可以参见:https://github.com/tensorflow/tensorboard/issues/179

同时,注意到 tensorboard 的 help 信息提示说是一个逗号分割的日志文件夹列表,那么把参数改为列表形式可以不?如:tensorboard --logdir= ["./logdir/log_1/", "./logdir/log_2/"],实测了不行,一个也显示不出来!还是建议先用方法一和方法二吧,别折腾浪费时间了,反正都能实现同样功能

 

三、实例

1、二维 sin 函数(300个数据点)

>>> import tensorflow as tf
>>> import math
>>> import numpy as np

>>> x = np.linspace(-math.pi,math.pi,300)[:,np.newaxis]
>>> x.shape
# (300, 1)
>>> noise = np.random.normal(loc=0.0, scale=0.9, size=x.shape)
>>> noise.shape
# (300, 1)

>>> x_holder = tf.placeholder(dtype=tf.float32, shape=[None,1], name="x")
>>> x_holder
# <tf.Tensor 'x:0' shape=(?, 1) dtype=float32>
>>> noise_holder = tf.placeholder(dtype=tf.float32, shape=[None,1], name="x")
>>> noise_holder
# <tf.Tensor 'x_1:0' shape=(?, 1) dtype=float32>

>>> y = tf.math.sin(x=x_holder) + noise_holder
>>> 
# <tf.Tensor 'add:0' shape=(?, 1) dtype=float32>
>>> y = tf.reduce_mean(input_tensor=y)
# <tf.Tensor 'Mean:0' shape=() dtype=float32>


>>> summary_writer = tf.summary.FileWriter("./summaries/logdir/")
>>> tf.summary.scalar(name="scalar_y", tensor=y)
>>> merged_summary = tf.summary.merge_all()

>>> init_op = tf.global_variables_initializer()

>>> sess = tf.InteractiveSession()
>>> sess.run(init_op)

>>> for i in range(300):
    each_x = np.asarray(a=[x[i]], dtype=np.float32)
    each_noise = np.asarray(a=[noise[i]], dtype=np.float32)
    summary_result = sess.run(fetches=merged_summary, feed_dict={x_holder:each_x, noise_holder:each_noise})
    summary_writer.add_summary(summary=summary_result, global_step=i)
>>> sess.close()

2、一维 sin 函数(1000个数据点)

>>> import tensorflow as tf
>>> import math
>>> import numpy as np


>>> x = np.linspace(-math.pi,math.pi,1000)
>>> x.shape
# (1000,)
>>> noise = np.random.normal(loc=0.0, scale=0.9, size=x.shape)
>>> noise.shape
# (1000,)


>>> x_holder = tf.placeholder(dtype=tf.float32, shape=[None], name="x")
>>> x_holder
# <tf.Tensor 'x:0' shape=(?,) dtype=float32>
>>> noise_holder = tf.placeholder(dtype=tf.float32, shape=[None], name="x")
>>> noise_holder
# <tf.Tensor 'x_1:0' shape=(?,) dtype=float32>


>>> y = tf.math.sin(x=x_holder) + noise_holder
>>> y
# <tf.Tensor 'add:0' shape=(?,) dtype=float32>
>>> y = tf.reduce_mean(input_tensor=y)
>>> y
# <tf.Tensor 'Mean:0' shape=() dtype=float32>


>>> summary_writer = tf.summary.FileWriter("./summaries/logdir_1d_sin/")
>>> tf.summary.scalar(name="scalar_y", tensor=y)
# <tf.Tensor 'scalar_y:0' shape=() dtype=string>
>>> merged_summary = tf.summary.merge_all()



>>> init_op = tf.global_variables_initializer()
>>> sess = tf.InteractiveSession()

>>> sess.run(init_op)
>>> for i in range(1000):
    each_x = np.asarray(a=[x[i]], dtype=np.float32)
    each_noise = np.asarray(a=[noise[i]], dtype=np.float32)
    summary_result = sess.run(fetches=merged_summary, feed_dict={x_holder:each_x, noise_holder:each_noise})
    summary_writer.add_summary(summary=summary_result, global_step=i)
>>> sess.close()

 

在命令行运行:tensorboard --logdir "./summaries/logdir_2d_sin/"

在浏览器地址栏:http://localhost:6006

 

在命令行运行:tensorboard --logdir "./summaries/logdir_1d_sin/"

在浏览器地址栏:http://localhost:6006

 

在命令行运行:tensorboard --logdir "./summaries/"

在浏览器地址栏:http://localhost:6006

 

3、同时创建多个 tf.summary.FileWriter,在一个会话中执行

(1)第一个 tf.summary.FileWriter 没创建日志文件,被第二个覆盖了

>>> import tensorflow as tf
>>> import math
>>> import numpy as np


>>> x = np.linspace(-math.pi,math.pi,1000)
>>> x.shape
# (1000,)
>>> noise = np.random.normal(loc=0.0, scale=0.9, size=x.shape)
>>> noise.shape
# (1000,)


>>> x_holder = tf.placeholder(dtype=tf.float32, shape=[None], name="x")
>>> x_holder
# <tf.Tensor 'x:0' shape=(?,) dtype=float32>
>>> noise_holder = tf.placeholder(dtype=tf.float32, shape=[None], name="x")
>>> noise_holder
# <tf.Tensor 'x_1:0' shape=(?,) dtype=float32>


>>> y = tf.math.sin(x=x_holder) + noise_holder
>>> y
# <tf.Tensor 'add:0' shape=(?,) dtype=float32>
>>> y = tf.reduce_mean(input_tensor=y)
>>> y
# <tf.Tensor 'Mean:0' shape=() dtype=float32>


>>> summary_writer_1 = tf.summary.FileWriter("./summaries/logdir_1_sin/")
>>> summary_writer_2 = tf.summary.FileWriter("./summaries/logdir_2_sin/")
>>> tf.summary.scalar(name="scalar_y", tensor=y)
# <tf.Tensor 'scalar_y:0' shape=() dtype=string>
>>> merged_summary = tf.summary.merge_all()



>>> init_op = tf.global_variables_initializer()
>>> sess = tf.InteractiveSession()

>>> sess.run(init_op)
>>> for i in range(1000):
    each_x_1 = np.asarray(a=[x[i]], dtype=np.float32)
    each_noise_1 = np.asarray(a=[noise[i]], dtype=np.float32)
    summary_result_1 = sess.run(fetches=merged_summary, feed_dict={x_holder:each_x_1, noise_holder:each_noise_1})
    summary_writer_1.add_summary(summary=summary_result_1, global_step=i)

    each_x_2 = np.asarray(a=[x[999-i]], dtype=np.float32)
    each_noise_2 = np.asarray(a=[noise[999-i]], dtype=np.float32)
    summary_result_2 = sess.run(fetches=merged_summary, feed_dict={x_holder:each_x_2, noise_holder:each_noise_2})
    summary_writer_2.add_summary(summary=summary_result_2, global_step=i)

>>> sess.close()

解决方法: 在 summary_writer_1.add_summary(summary=summary_result_1, global_step=i) 之后添加 summary_writer_1.flush() 强制将日志文件写入到硬盘,具体如下:

>>> import tensorflow as tf
>>> import math
>>> import numpy as np


>>> x = np.linspace(-math.pi,math.pi,1000)
>>> x.shape
# (1000,)
>>> noise = np.random.normal(loc=0.0, scale=0.9, size=x.shape)
>>> noise.shape
# (1000,)


>>> x_holder = tf.placeholder(dtype=tf.float32, shape=[None], name="x")
>>> x_holder
# <tf.Tensor 'x:0' shape=(?,) dtype=float32>
>>> noise_holder = tf.placeholder(dtype=tf.float32, shape=[None], name="x")
>>> noise_holder
# <tf.Tensor 'x_1:0' shape=(?,) dtype=float32>


>>> y = tf.math.sin(x=x_holder) + noise_holder
>>> y
# <tf.Tensor 'add:0' shape=(?,) dtype=float32>
>>> y = tf.reduce_mean(input_tensor=y)
>>> y
# <tf.Tensor 'Mean:0' shape=() dtype=float32>


>>> summary_writer_1 = tf.summary.FileWriter("./summaries/logdir_1_sin/")
>>> summary_writer_2 = tf.summary.FileWriter("./summaries/logdir_2_sin/")
>>> tf.summary.scalar(name="scalar_y", tensor=y)
# <tf.Tensor 'scalar_y:0' shape=() dtype=string>
>>> merged_summary = tf.summary.merge_all()



>>> init_op = tf.global_variables_initializer()
>>> sess = tf.InteractiveSession()
>>> sess.run(init_op)
>>> for i in range(1000):
...     each_x_1 = np.asarray(a=[x[i]], dtype=np.float32)
...     each_noise_1 = np.asarray(a=[noise[i]], dtype=np.float32)
...     summary_result_1 = sess.run(fetches=merged_summary, feed_dict={x_holder:each_x_1, noise_holder:each_noise_1})
...     summary_writer_1.add_summary(summary=summary_result_1, global_step=i)
...     summary_writer_1.flush()
...     each_x_2 = np.asarray(a=[x[999-i]], dtype=np.float32)
...     each_noise_2 = np.asarray(a=[noise[999-i]], dtype=np.float32)
...     summary_result_2 = sess.run(fetches=merged_summary, feed_dict={x_holder:each_x_2, noise_holder:each_noise_2})
...     summary_writer_2.add_summary(summary=summary_result_2, global_step=i)
...     summary_writer_2.flush()
...

>>> summary_writer_1.close()
>>> summary_writer_2.close()
>>> sess.close()

在命令行运行:tensorboard --logdir summaries 

在浏览器地址栏:http://localhost:6006

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn-WJW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值