def deconv2d(input: tf.Tensor,
output_shape: Sequence[Union[int, tf.Tensor]],
kernel_width: int = 5,
kernel_height: int = 5,
horizontal_stride: int = 2,
vertical_stride: int = 2,
weight_initializer: Optional[Initializer] = None,
bias_initializer: Optional[Initializer] = None,
name: str = "deconv2d"):
"""
Applies a 2D-deconvolution to a tensor.
Parameters
----------
input: tf.Tensor
The tensor to which a 2D-deconvolution should be applied. Must be of shape [batch_size, height, width, channels]
output_shape: list of int or tf.Tensor
The desired output shape.
kernel_width: int, optional
The width of the convolutional filters (default 5)
kernel_height: int, optional
The height of the convolutional filters (default 5)
horizontal_stride: int, optional
The horizontal stride of the convolutional filters (default 2)
vertical_stride: int, optional
The vertical stride of the convolutional filters (default 2)
weight_initializer: tf.Initializer, optional
A custom initializer for the weight matrices of the filters
bias_initializer: tf.Initializer, optional
A custom initializer for the bias vectors of the filters
name: str, optional
A name for the operation (default "deconv2d")
Returns
-------
tf.Tensor
The result of applying a 2D-deconvolution to the input tensor
"""
shape = input.get_shape().as_list()
with tf.variable_scope(name):
# filter : [height, width, output_channels, in_channels]
weights = tf.get_variable(name="weights",
shape=[kernel_height, kernel_width, output_shape[-1], shape[-1]],
initializer=weight_initializer)
biases = tf.get_variable(name="bias",
shape=[output_shape[-1]],
initializer=bias_initializer)
deconv = tf.nn.conv2d_transpose(input,
filter=weights,
output_shape=output_shape,
strides=[1, vertical_stride, horizontal_stride, 1])
deconv = tf.nn.bias_add(deconv, biases)
deconv.set_shape([None] + output_shape[1:])
return deconv
评论列表
文章目录