[英]How to extract the shape value of a placeholder Tensor in Tensorflow?
I defined a x = tf.placeholder("float", shape=[None, 784])
for input data. 我为输入数据定义了一个
x = tf.placeholder("float", shape=[None, 784])
。 Later on, I need to know the first value of the shape of x
as batch size. 稍后,我需要知道
x
形状的第一个值作为批处理大小。 And I extract the value by x.get_shape().as_list()[0]
but I got None
. 然后我通过
x.get_shape().as_list()[0]
提取了值,但得到None
。 Could you please tell me how should I extract it properly? 您能告诉我如何正确提取吗? Thanks a lot!
非常感谢!
Edit: 编辑:
I have used tf.get_shape()
now but it cause another bug. 我现在使用过
tf.get_shape()
但它会导致另一个错误。 In my code, I have defined a deconv
funciton: 在我的代码中,我定义了一个
deconv
:
def deconv(X, W, b, output_shape):
X += b
return tf.nn.conv2d_transpose(X, W, output_shape, strides=[1, 1, 1, 1])
If I set the batch_size
to a int
in such way: batch_size = 50
, the calling of the deconv
functions works well as following: 如果我通过以下方式将
batch_size
设置为int
: batch_size = 50
,则调用deconv
函数的工作原理如下:
W_conv2_T = tf.ones([5, 5, 32, 64])
pool1_tr = deconv(conv2_tr, W_conv2_T, tf.zeros([64]), [batch_size, 14, 14, 32])
The shape of conv2_tr
is [50, 14, 14, 64]
. conv2_tr
的形状为[50, 14, 14, 64]
conv2_tr
[50, 14, 14, 64]
。 And the resulting shape of pool1_tr
is [50, 14, 14, 32]
. pool1_tr
的最终形状为[50, 14, 14, 32]
pool1_tr
[50, 14, 14, 32]
。 But if I set batch_size = tf.get_shape(x)[0]
, shape of conv2_tr
is [None, 14, 14, 64]
and the resulting shape of pool1_tr
becomes [None, None, None, None]
. 但是,如果我设置
batch_size = tf.get_shape(x)[0]
, conv2_tr
形状为[None, 14, 14, 64]
conv2_tr
[None, 14, 14, 64]
,结果pool1_tr
形状为[None, None, None, None]
。 This bug is so strange. 这个错误很奇怪。 Could you please help me with this issue?
您能帮我解决这个问题吗? Thanks in advance!
提前致谢!
A value of None
for the number of rows in your placeholder means that it can vary at runtime, so you must use tf.shape(x)
to get the shape as a tf.Tensor
. 占位符中行数的值为
None
意味着它可以在运行时变化,因此必须使用tf.shape(x)
来将形状作为tf.Tensor
。 The following code should work: 下面的代码应该工作:
batch_size = tf.shape(x)[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.