lhk
lhk

Reputation: 30086

Tensorflow: use None in output_shape in conv2D_transpose

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 ?

Here is the line of code:

tf.nn.conv2d_transpose(hd_conv1, Wd_conv1, [batch_size, 14,14,64], strides=[1,2,2,1], padding="SAME")

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: Expected binary or unicode string, got None

If I just leave out the first dimension:

ValueError: output_shape must have shape (4,), got (3,)

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.

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 ?

Upvotes: 4

Views: 1679

Answers (1)

hwalsuklee
hwalsuklee

Reputation: 41

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, ...)  

Be careful not to use tf.get_shape(). tf.get_shape() and tf.shape() are slightly different.

Also see recomendations in tensorflow site about variable batch size.

https://www.tensorflow.org/programmers_guide/faq

Upvotes: 4

Related Questions