简体   繁体   English

Tensorflow:一维数据的形状不匹配问题

[英]Tensorflow : Shape mismatch issue with one dimensional data

I am trying to pass x_data as feed_dict but getting below error, I am not sure that what is wrong in the code.我试图将 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"]()]]

My Code:我的代码:

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}))

I checked the shape of the x and x_data and it is same我检查了xx_data的形状,它是一样的

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

I am working with one dimensional data.我正在处理一维数据。 Any help is appreciated, Thanks!任何帮助表示赞赏,谢谢!

To make it work I have changed two things, first I changed y to be a Tensor .为了使它工作,我改变了两件事,首先我将y改为Tensor And secondly I have not changed the x_data to Tensor , as commented here :其次,我没有将x_data更改为Tensor ,正如这里评论的那样:

The optional feed_dict argument allows the caller to override the value of tensors in the graph.可选的 feed_dict 参数允许调用者覆盖图中张量的值。 Each key in feed_dict can be one of the following types: feed_dict 中的每个键都可以是以下类型之一:

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.如果键是张量,则值可能是 Python 标量、字符串、列表或 numpy ndarray,可以转换为与该张量相同的 dtype。 Additionally, if the key is a placeholder, the shape of the value will be checked for compatibility with the placeholder.此外,如果键是占位符,将检查值的形状是否与占位符兼容。

The changed code which works for me:对我有用的更改后的代码:

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}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM