在Tensorboard中为两个不同的图表设置双轴

时间:2019-05-16 12:32:20

标签: python tensorflow keras tensorboard

以下是两个示例:
1个完美的秤,因为秤相同:

import tensorflow as tf
from numpy import random

writer_1 = tf.summary.FileWriter("./logs/plot_1")
writer_2 = tf.summary.FileWriter("./logs/plot_2")

log_var = tf.Variable(0.0)
tf.summary.scalar("loss", log_var)

write_op = tf.summary.merge_all()

session = tf.InteractiveSession()
session.run(tf.global_variables_initializer())

for i in range(100):
    # for writer 1
    summary = session.run(write_op, {log_var: random.rand()})
    writer_1.add_summary(summary, i)
    writer_1.flush()

    # for writer 2
    summary = session.run(write_op, {log_var: random.rand()})
    writer_2.add_summary(summary, i)
    writer_2.flush()
    print(i)

这个数字可以理解: good image

但是请看第二种情况,其中值不适合同一范围。在这种情况下,我需要在同一张图表上使用两个不同的轴,以便获得良好且易于理解的图像。检查代码:

import tensorflow as tf
from numpy import random

writer_1 = tf.summary.FileWriter("./logs/plot_1")
writer_2 = tf.summary.FileWriter("./logs/plot_2")

log_var = tf.Variable(0.0)
tf.summary.scalar("loss", log_var)

write_op = tf.summary.merge_all()

session = tf.InteractiveSession()
session.run(tf.global_variables_initializer())

for i in range(100):
    # for writer 1
    summary = session.run(write_op, {log_var: i*10})
    writer_1.add_summary(summary, i)
    writer_1.flush()

    # for writer 2
    summary = session.run(write_op, {log_var: random.rand()})
    writer_2.add_summary(summary, i)
    writer_2.flush()
    print(i)

查看所获得的图像:
bad image

请帮助我进行此查询。

1 个答案:

答案 0 :(得分:1)

同一坐标图中不能有两个轴,必须将值放在两个不同的坐标图中。这有点棘手,因为绘图由摘要的名称确定,因此在您的示例中,您将需要手工构建摘要对象。

import tensorflow as tf
from numpy import random

writer_1 = tf.summary.FileWriter("./logs/plot_1")
writer_2 = tf.summary.FileWriter("./logs/plot_2")

log_var = tf.Variable(0.0)

session = tf.InteractiveSession()
session.run(tf.global_variables_initializer())

for i in range(100):
    # for writer 1
    log1 = session.run(log_var, {log_var: i*10})
    summary1 = tf.train.Summary()
    summary1.value.add(tag='loss1', simple_value=log1)
    writer_1.add_summary(summary1, i)
    writer_1.flush()

    # for writer 2
    log2 = session.run(log_var, {log_var: random.rand()})
    summary2 = tf.train.Summary()
    summary2.value.add(tag='loss2', simple_value=log2)
    writer_2.add_summary(summary2, i)
    writer_2.flush()
    print(i)