使用占位符

时间:2016-12-07 20:58:45

标签: tensorflow

在tf 0.12.0rc之前,我使用了表单的摘要占位符:

tag_ph = tf.placeholder(tf.string)
val_ph = tf.placeholder(tf.float)
sum_op = tf.scalar_summary(tag_ph, val_ph)
...
feed_dict = {tag_ph:[some string], val_ph:[some val]}
sum_str = sess.run(sum_op, feed_dict)
writer.add_summary(sum_str)

升级到0.12.0并将tf.scalar_summary()更改为tf.summary.scalar()后,使用name参数的占位符会出现以下错误:

TypeError: expected string or bytes-like object

如果我为name使用静态字符串,则没有错误,但我想在评估过程中更改字符串。我怎么能这样做?

最小例子:

tag = 'test'
val = 1.234
tag_ph = tf.placeholder(tf.string, [])
val_ph = tf.placeholder(tf.float32, [])
scalar_op = tf.summary.scalar(tag_ph, val_ph)
with tf.Session() as sess:
    writer = tf.summary.FileWriter('/tmp/summary_placeholders', sess.graph)
feed_dict = {tag_ph:tag, val_ph:val}
sum_str = sess.run(scalar_op, feed_dict)
writer.add_summary(sum_str)
writer.flush()

这个相同的代码(在恢复tf.summary名称之后)在TF 0.11.0中运行

1 个答案:

答案 0 :(得分:2)

如果问题是如何在版本> = 0.12中将非Tensorflow数据写为摘要,这是一个例子:

import tensorflow as tf
summary_writer = tf.summary.FileWriter('custom_summaries')
summary = tf.Summary()
mydata = {"a": 1, "b": 2}
for name, data in mydata.items():
    summary.value.add(tag=name, simple_value=data)
    summary_writer.add_summary(summary, global_step=1)

summary_writer.flush()

TensorBoard合并logdir中所有文件的摘要并显示它们,即,如果你tensorboard --logdir=.,你会看到类似这样的内容

enter image description here