Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Tensorflow v1.2.1中使用摘要时,InvalidArgumentError无效_Python_Tensorflow_Tensorboard - Fatal编程技术网

Python 在Tensorflow v1.2.1中使用摘要时,InvalidArgumentError无效

Python 在Tensorflow v1.2.1中使用摘要时,InvalidArgumentError无效,python,tensorflow,tensorboard,Python,Tensorflow,Tensorboard,我已经编写了一个简单的代码来尝试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]

我已经编写了一个简单的代码来尝试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

如果我删除了所有与摘要相关的代码,则该代码可以工作。但如果我添加与摘要相关的代码,我会得到以下错误:

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。标记和值的形状不同。您正在传递
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_))

这将创建一个标量值

在你的回答和帖子的帮助下,我终于完成了代码工作。我也更新了你的答案。关键是,输入x不应该是固定大小;在输出y上使用reduce_*。