How can I give a variable batch_dim in output_shape argument of deconv2d in tensorflow?

元气小坏坏 提交于 2019-12-06 12:14:43

问题


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


来源:https://stackoverflow.com/questions/35346599/how-can-i-give-a-variable-batch-dim-in-output-shape-argument-of-deconv2d-in-tens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!