I am missing something basic here. But I always used the tf.nn
API for transpose convolution, where I have to specify the output shape, because it is ambiguous(Link).
However, for TF 2.0 I switch to Keras sub-classing and now I wonder why I do not have to specify the output shape for transpose convolution in the higher level API. How to they compute it exactly?
tf.keras.layers.Conv2DTranpose
backends to tf.nn.conv2d_transpose
via tf.keras.backend.conv2d_transpose
.
To compute the output_shape
argument for tf.nn.conv2d_transpose
it utilizes the function deconv_output_length
(defined here):
def deconv_output_length(input_length, filter_size, padding, stride):
"""Determines output length of a transposed convolution given input length.
Arguments:
input_length: integer.
filter_size: integer.
padding: one of "same", "valid", "full".
stride: integer.
Returns:
The output length (integer).
"""
if input_length is None:
return None
input_length *= stride
if padding == 'valid':
input_length += max(filter_size - stride, 0)
elif padding == 'full':
input_length -= (stride + filter_size - 2)
return input_length