python - tensorflow : Shape mismatch issue with one dimensional data

标签 python tensorflow

我试图将 x_data 作为 feed_dict 传递,但出现以下错误,我不确定代码中有什么问题。

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'x_12' with dtype int32 and shape [1000]
     [[Node: x_12 = Placeholder[dtype=DT_INT32, shape=[1000], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

我的代码:

import tensorflow as tf
import numpy as np
model = tf.global_variables_initializer()
#define x and y
x = tf.placeholder(shape=[1000],dtype=tf.int32,name="x")
y = tf.Variable(5*x**2-3*x+15,name = "y")
x_data = tf.pack(np.random.randint(0,100,size=1000))
print(x_data)
print(x)
with tf.Session() as sess:
    sess.run(model)
    print(sess.run(y,feed_dict={x:x_data}))

我检查了 xx_data 的形状,它们是一样的

Tensor("pack_8:0", shape=(1000,), dtype=int32)
Tensor("x_14:0", shape=(1000,), dtype=int32)

我正在处理一维数据。 感谢任何帮助,谢谢!

最佳答案

为了让它工作,我改变了两件事,首先我将 y 更改为 Tensor。其次,我没有将 x_data 更改为 Tensor,如评论 here :

The optional feed_dict argument allows the caller to override the value of tensors in the graph. Each key in feed_dict can be one of the following types:

If the key is a Tensor, the value may be a Python scalar, string, list, or numpy ndarray that can be converted to the same dtype as that tensor. Additionally, if the key is a placeholder, the shape of the value will be checked for compatibility with the placeholder.

对我有用的更改代码:

import tensorflow as tf
import numpy as np
model = tf.global_variables_initializer()
#define x and y
x = tf.placeholder(shape=[1000],dtype=tf.int32,name="x")
y = 5*x**2-3*x+15 # without tf.Variable, making it a tf.Tensor
x_data = np.random.randint(0,100,size=1000) # without tf.pack
print(x_data)
print(x)
with tf.Session() as sess:
    sess.run(model)
    print(sess.run(y,feed_dict={x:x_data}))

关于python - tensorflow : Shape mismatch issue with one dimensional data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42154108/

相关文章:

android - 理解 Python 脚本所需的帮助

python - 运行 python 代码的 :pyf % and :! python % 之间的区别

python - python列表计数方法的性能

tensorflow - 无法在 Jupyter Notebook 中多次运行 Tensorflow 代码

tensorflow - 使用 tf.image.Dataset 时卡住 tensorflow 中的图形

python - 使用 Python 的 telnetlib 进行非常缓慢的交互

python - 如何在 Regex Python 中的每个子字符串之后拆分字符串

python - 从张量列表创建参差不齐的张量

python - 在 Ubuntu 12.04 中的 Python 2.7 中导入 Tensorflow 时出错。 'GLIBC_2.17 not found'

python - 具有 Lambda 函数的 HSTACK CNN 输出