Tensorflow:在conv2D_transpose的output_shape中使用None [英] Tensorflow: use None in output_shape in conv2D_transpose
问题描述
我想在网络中使用conv2d_tranpose
(或反卷积)而不是向上采样.
这需要将output_shape
传递给函数调用.没问题,我可以计算出来.但是我想对Batch_size使用None来保持设置的灵活性.
有可能吗?
I would like to use a conv2d_tranpose
(or deconvolution) instead of upsampling in my network.
This requires passing an output_shape
to the function call. That is not a problem, I can calculate it. But I would like to use None for the batch_size to keep the setup flexible.
Is that possible ?
这是代码行:
tf.nn.conv2d_transpose(hd_conv1, Wd_conv1, [batch_size, 14,14,64], strides=[1,2,2,1], padding="SAME")
batch_size
只是我在脚本顶部设置的变量.这段代码运行良好,但是如果我使用None
而不是batch_size
:
batch_size
is simply a variable that I set at the top of my script. This code runs fine, but if I use None
instead of batch_size
:
TypeError:预期的二进制或Unicode字符串,没有任何内容
TypeError: Expected binary or unicode string, got None
如果我只是省略第一个维度:
If I just leave out the first dimension:
ValueError:output_shape必须具有形状(4,),得到的形状(3,)
ValueError: output_shape must have shape (4,), got (3,)
我认为奇怪的是,有很多方法可以处理batch_size.有些操作只是忽略它,例如普通的conv2d,但是在这里我需要明确地指定它. 无论如何,我想知道为什么我必须自己计算出output_shape.在给定输入,步幅,填充的情况下,应该易于计算. 关于output_shape的推断,有一个github 问题,可悲的是似乎没有任何后续行动.
I think it is strange that there are different ways to deal with the batch_size. Some operations simply ignore it, such as the normal conv2d, but here I need to specify it explicitly. In any case I wondered why I would have to calculate the output_shape myself, at all. With given input, strides, padding, that should be easy to calculate. There is a github issue regarding inference of output_shape, sadly there doesn't seem to be any follow-up.
我做对了吗-在output_shape中传递一个显式的batch_size
?
有没有办法省略显式的batch_size
?
Am I doing this right - passing in an explicit batch_size
in the output_shape ?
Is there a way to omit the explicit batch_size
?
推荐答案
请不要使用None
,而应使用如下所示的符号表示形式.
Instead of using None
, please use symbolic representation like below.
batch_size = tf.shape(something_or_other)[0]
deconv_shape = tf.pack([batch_size, 40, 40, 32])
conv2d_transpose(..., output_shape=deconv_shape, ...)
请注意不要使用tf.get_shape()
. tf.get_shape()
和tf.shape()
略有不同.
Be careful not to use tf.get_shape()
. tf.get_shape()
and tf.shape()
are slightly different.
另请参阅tensorflow网站中有关批量大小可变的建议.
Also see recomendations in tensorflow site about variable batch size.
https://www.tensorflow.org/programmers_guide/faq
这篇关于Tensorflow:在conv2D_transpose的output_shape中使用None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!