2

I am trying to use the tf.nn.deconv2d() op on a variable-sized batch of data. However, it appears that I need to set the output_shape argument as follows:

tf.nn.deconv2d(x, filter, output_shape=[12, 24, 24, 5], strides=[1, 2, 2, 1],
               padding="SAME")

Why does tf.nn.deconv2d() take a fixed output_shape? Is there any way to specify a variable batch dimension? What happens if the input batch size varies?

mrry
  • 125,488
  • 26
  • 399
  • 400
Shaban
  • 113
  • 2
  • 11

1 Answers1

5

N.B. tf.nn.deconv2d() will be called tf.nn.conv2d_transpose() in the next release of TensorFlow (0.7.0).

The output_shape argument to tf.nn.deconv2d() accepts a computed Tensor as its value, which enables you specify a dynamic shape. For example, let's say your input is defined as follows:

# N.B. Other dimensions are chosen arbitrarily.
input = tf.placeholder(tf.float32, [None, 24, 24, 5])

...then the batch size for a particular step can be computed at runtime:

batch_size = tf.shape(input)[0]

With this value, you can then build the output_shape argument to tf.nn.deconv2d() using tf.pack():

output_shape = tf.pack([batch_size, 24, 24, 5])

result = tf.nn.deconv2d(..., filter, output_shape=output_shape,
                        strides=[1, 2, 2, 1], padding='SAME')
mrry
  • 125,488
  • 26
  • 399
  • 400
  • 2
    In r0.12 tf.pack() has been deprecated, tf.stack() is the proper usage as per the documentation: https://www.tensorflow.org/versions/master/api_docs/python/array_ops/slicing_and_joining#pack – David Parks Jan 17 '17 at 20:37