ConvTranspose1d
ModuleConvTranspose1d(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, output_padding: int = 0, groups: int = 1, bias: bool = True, dilation: int = 1, device: DeviceLike = None, dtype: DTypeLike = None)Applies a 1D transposed convolution (fractionally-strided convolution).
A transposed convolution — sometimes loosely called a deconvolution, though it is not a true mathematical inverse — is the gradient of a standard convolution with respect to its input. It upsamples the spatial dimension by inserting implicit zeros between input elements before applying a convolution, and is the go-to building block for decoders, generators, and any network that must increase sequence length.
Formally, for a stride and kernel size :
where is padding, is dilation, and
is output_padding.
Parameters
in_channelsintout_channelsintkernel_sizeintstrideint= 1> 1 upsample the input.
Default: 1.paddingint= 0dilation * (kernel_size - 1) - padding zero-padding is added
to both sides of each dimension in the input. Default: 0.output_paddingint= 00 <= output_padding < max(stride, dilation). Used
to disambiguate the output size when the formula
is
compatible with multiple . Default: 0.groupsint= 11.biasbool= TrueTrue, adds a learnable bias. Default: True.dilationint= 11.deviceDeviceLike= NoneNone.dtypeDTypeLike= NoneNone.Attributes
weightParameter(in_channels, out_channels // groups, kernel_size).
Note the channel axis ordering is transposed relative to
Conv1d: the leading dimension corresponds to
in_channels, not out_channels.
Initialized with Kaiming uniform ().biasParameter or None(out_channels,), or None.Notes
Input: Output: where
$$
L_{\text{out}} = (L - 1) \cdot s - 2p + d(K - 1) + p_{\text{out}} + 1
Not a true inverse. ConvTranspose1d is the transpose
(adjoint) of Conv1d in the linear-algebra sense: if
conv_forward maps , then
ConvTranspose1d with the same weights maps back
. When used in autoencoders or
flow models the weights are learned independently and no exact
inversion is implied.
output_padding. The transposed convolution output size formula
can map multiple L_in values to the same L_out.
output_padding breaks this ambiguity by adding a single extra row
on the output; it does not add actual padding to the input.
Examples
Upsample a sequence by factor 2:
>>> import lucid
>>> import lucid.nn as nn
>>> upsample = nn.ConvTranspose1d(in_channels=16, out_channels=16,
... kernel_size=4, stride=2, padding=1)
>>> x = lucid.zeros(2, 16, 32)
>>> y = upsample(x)
>>> y.shape
(2, 16, 64)
Simple 1D decoder layer:
>>> import lucid
>>> import lucid.nn as nn
>>> decoder = nn.ConvTranspose1d(64, 32, kernel_size=3, stride=1, padding=1)
>>> x = lucid.zeros(4, 64, 20)
>>> y = decoder(x)
>>> y.shape
(4, 32, 20)Methods (3)
__init__
→None__init__(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, output_padding: int = 0, groups: int = 1, bias: bool = True, dilation: int = 1, device: DeviceLike = None, dtype: DTypeLike = None)Initialise the ConvTranspose1d module. See the class docstring for parameter semantics.
forward
→Tensorforward(x: Tensor)Apply the convolution to the input tensor.
Parameters
inputTensorReturns
TensorOutput tensor of shape with spatial dimensions determined by stride, padding, dilation, and kernel size.
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.