如何塑造Tensor阵列?

时间:2017-10-10 03:21:49

标签: tensorflow

我最近被以下错误消息所困扰:

ValueError: Cannot feed value of shape (2455040,) for Tensor 'Placeholder:0', which has shape '(2455040, ?)'

通过运行以下代码生成了以下代码:

NUMCLASSES=16
NUMPIXELS=959*640*4
# set up to feed an array of images [images, size_of_image]
x = tf.placeholder(tf.float32, [NUMPIXELS,None])

.... .... deletia

# Define loss and optimizer..why is this 2d?
y_ = tf.placeholder(tf.float32, [None,NUMCLASSES])


sess = tf.InteractiveSession()
tf.global_variables_initializer().run(session=sess)
tl = get_tensor_list()

for f, n in tl:
    str = '/users/me/downloads/train/' + f
    mm = Image.open(str)
    mm = mm.convert('F')
    mma=np.array(mm)
    i = mma.flatten() #now this is an array of floats of size NUMPIXELS 
    sess.run(train_step, feed_dict={x: i, y_: n})  # <<DEATH 

不知何故,该数组正在获得一个tf不喜欢[(x,)它想要的形状(x,?)]。在这种情况下如何满足tensorgods?由于未讨论的其他数学原因,张量必须是它必须的。

2 个答案:

答案 0 :(得分:0)

重塑阵列可能会有所帮助。

i = mma.flatten().reshape((NUMPIXELS,1))

答案 1 :(得分:0)

错误发生是因为两个张量具有不同的ranks:张量与形状(2455040)具有等级1,而张量形状(2455040,?)具有等级2.

你可以这样做:

x = tf.placeholder(tf.float32, [None])
x = tf.reshape(x, [NUMPIXELS,-1])