简体   繁体   中英

Tensorflow Vector Rank / Dimensions

In Tensorflow, does a vector of N elements have shape(0,N) or (1,N) ?

I am feeding a vector to a placeholder but am getting this error:

ValueError: Cannot feed value of shape (1, 984) for Tensor u'Placeholder:0', which has shape '(0, 984)'

Is this the type of situation where tf.expand_dims is needed to add a "phantom" dimension to the vector?

Background : I train my model with many examples, in batches. I save the trained model to a checkpoint. Then I restore the model from checkpoint and run it on a single example, in feed-forward inference mode (no training), with batch size = 1. Apparently this issue arises from having 1 input example vs. many. The placeholder:

images_placeholder = tf.placeholder(tf.float32, shape=(shared.batchSize, IMAGE_PIXELS))

When you use shape=(1, x) you do not create a vector, you create a matrix, with one row and x columns. To create a vector, use shape=(x,) . Take a look at this example:

import tensorflow as tf

a = tf.ones((1, 3))
b = tf.ones((3,))
with tf.Session() as sess:
    print sess.run(a)
    print sess.run(b)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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