简体   繁体   English

如何在Tensorflow中提取占位符Tensor的形状值?

[英]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设置为intbatch_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.

相关问题 TensorFlow无法为Tensor'占位符:0'提供形状值(100,784) - TensorFlow Cannot feed value of shape (100, 784) for Tensor 'Placeholder:0' tensorflow:如何创建与占位符形状相同的常量张量 - tensorflow: how create an const tensor the same shape as a placeholder Tensorflow值错误:无法为形状为((?,1)'的Tensor'Placeholder_5:0'输入形状(8009,)的值 - Tensorflow value error : Cannot feed value of shape (8009,) for Tensor 'Placeholder_5:0', which has shape '(?, 1)' Tensorflow-ValueError:无法为形状为((,, 3433)的Tensor'Placeholder_142:0'输入形状(128,)的值 - Tensorflow - ValueError: Cannot feed value of shape (128,) for Tensor 'Placeholder_142:0', which has shape '(?, 3433) 无法为张量为'(?,2)'的Tensor'Placeholder_24:0'输入形状(25,2,1)的值tensorflow python - Cannot feed value of shape (25, 2, 1) for Tensor 'Placeholder_24:0', which has shape '(?, 2)' tensorflow python TensorFlow ValueError:无法为形状为“(?, 64, 64, 3)”的张量 u'Placeholder:0' 提供形状 (64, 64, 3) 的值 - TensorFlow ValueError: Cannot feed value of shape (64, 64, 3) for Tensor u'Placeholder:0', which has shape '(?, 64, 64, 3)' TensorFlow 无法为形状为“(?, 8)”的张量“Placeholder_21:0”提供形状 (538, 1) 的值? - TensorFlow cannot feed value of shape (538, 1) for Tensor 'Placeholder_21:0', which has shape '(?, 8)'? tensorflow:分析图像得出张量为'Placeholder:0'的形状为((?,1296000)'的Cannot feed形状值为(1296000,) - tensorflow : Analysing images gives Cannot feed value of shape (1296000,) for Tensor 'Placeholder:0', which has shape '(?, 1296000)' 无法为Tensor占位符提供形状的值 - Cannot feed value of shape for Tensor Placeholder Python-TensorFlow / tf ValueError:无法为Tensor'占位符:0'提供形状((,28,28,1)')的形状(100,784)值 - Python - TensorFlow/tf ValueError: Cannot feed value of shape (100, 784) for Tensor 'Placeholder:0', which has shape '(?, 28, 28, 1)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM