【问题标题】:InvalidArgumentError when using summary in Tensorflow v1.2.1在 Tensorflow v1.2.1 中使用摘要时出现 InvalidArgumentError
【发布时间】:2017-07-17 08:14:08
【问题描述】:

我编写了一个简单的代码来试用 Tensorflow 的汇总功能。代码如下。

import tensorflow as tf
import numpy as np

graph = tf.Graph()


with graph.as_default():
    x = tf.placeholder(tf.float32, [1, 2], name='x')
    W = tf.ones([2, 1], tf.float32, name='W')
    b = tf.constant([1.5], dtype=tf.float32, shape=(1, 1), name='bias')
    y_ = tf.add(tf.matmul(x, W, name='mul'), b, name='add')
tf.summary.scalar('y', y_)

with tf.Session(graph=graph) as session:
    merged = tf.summary.merge_all()
    fw = tf.summary.FileWriter("/tmp/tensorflow/logs", graph=graph)

    tf.global_variables_initializer().run()
    x_var = np.array([1., 1.], np.float32).reshape([1, 2])
    print(x_var)
    summary, y = session.run([merged, y_], feed_dict={x: x_var})
    fw.add_summary(summary, 0)
    print(y)

    fw.close()

基本上,它会尝试实现y=Wx + b

如果我删除所有 summary 相关代码,该代码将起作用。但是如果我添加 summary 相关代码,我得到以下错误:

InvalidArgumentError (see above for traceback): tags and values not the same shape: [] != [1,1] (tag 'y')
     [[Node: y = ScalarSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](y/tags, add)]]

我在普通 python 和 IPython 中都试过了。

【问题讨论】:

    标签: python tensorflow tensorboard


    【解决方案1】:

    标签和值的形状不同。您正在传递 x_var 这是一个向量,而 summary 采用一个标量值。你可以简单地使用tf.reduce_mean来解决这个问题:

    with graph.as_default():
        x = tf.placeholder(tf.float32, [None, 2], name='x')
        W = tf.ones([2, 1], tf.float32, name='W')
        b = tf.constant([1.5], dtype=tf.float32, shape=(1, 1), name='bias')
        y_ = tf.add(tf.matmul(x, W, name='mul'), b, name='add')
        tf.summary.scalar('y', tf.reduce_mean(y_))
    

    这将创建一个标量值。

    【讨论】:

    • 在您的回答和this 帖子的帮助下,我终于得到了代码工作。我也更新了你的答案。
    • keys 是,输入 x 不应该是固定大小;在输出 y 上使用 reduce_*
    猜你喜欢
    • 1970-01-01
    • 2016-05-08
    • 2018-09-27
    • 2017-12-02
    • 2018-05-17
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 2018-10-10
    相关资源
    最近更新 更多