Skip to content Skip to sidebar Skip to footer

Conv2d_transpose Is Dependent On Batch_size When Making Predictions

I have a neural network currently implemented in tensorflow, but I am having a problem making predictions after training, because I have a conv2d_transpose operations, and the shap

Solution 1:

So I came across a solution based on the issues forum of tensorflow at https://github.com/tensorflow/tensorflow/issues/833.

In my code

conv4 = layers.deconvLayer(conv3['layer_output'],
                                    filter_shape=[2, 2, 64, 128],
                                    output_shape=[batch_size, 32, 40, 64],
                                    strides=[1, 2, 2, 1])

my output shape that get passed to deconvLayer was hard coded with a predetermined batch shape when training. By altering this to the following:

def deconvLayer(input, filter_shape, output_shape, strides):
    W1_1 = weight_variable(filter_shape)

    dyn_input_shape = tf.shape(input)
    batch_size = dyn_input_shape[0]

    output_shape = tf.pack([batch_size, output_shape[1], output_shape[2], output_shape[3]])

    output = tf.nn.conv2d_transpose(input, W1_1, output_shape, strides, padding="SAME")

    returnoutput

This allows the shape to be dynamically inferred at run time and can handle a variable batch size.

Running the code, I no longer receive this error when passing in any batch size of test data. I believe this is necessary due to the fact that the inference of shapes for transpose ops is not as straightforward at the moment as it is for normal convolutional ops. So where we would usually use None for the batch_size in normal convolutional ops, we must provide a shape, and since this could vary based on input, we must go through the effort of dynamically determining it.

Post a Comment for "Conv2d_transpose Is Dependent On Batch_size When Making Predictions"