How can I give a variable batch_dim in output_shap

2019-04-14 18:47发布

问题:

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?

回答1:

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')