gpt4 book ai didi

python - feed 数据的形状与 tensorflow 中占位符的形状不匹配

转载 作者:行者123 更新时间:2023-11-30 22:13:44 25 4
gpt4 key购买 nike

当我用tensorflow实现XOR时出现错误。该错误消息表明输入数据的形状与占位符的形状不匹配。代码如下:

#!/usr/bin/python
import tensorflow as tf
import numpy as np

x = tf.placeholder(tf.float32,shape=[None,2])
data = np.random.rand(2,2)
print data.shape
print data
y = tf.add(x,x)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(y,{x:data})
print sess.run(y)

错误信息:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,2]

[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

最佳答案

有语法错误。正确的代码应该是:

x = tf.placeholder(dtype=tf.float32,shape=[None,2])
data = np.random.rand(2,2)
print data.shape
print data
y = tf.add(x,x)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
y_result = sess.run(y,{x:data})
print y_result

关于python - feed 数据的形状与 tensorflow 中占位符的形状不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50692972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com