Skip to content
class

ConvTranspose1d

extends_ConvTransposeNd
ConvTranspose1d(in_channels: int, out_channels: int, kernel_size: int | tuple[int], stride: int | tuple[int] = 1, padding: int | tuple[int] = 0, output_padding: int | tuple[int] = 0, groups: int = 1, bias: bool = True, dilation: int | tuple[int] = 1, padding_mode: str = 'zeros', device: DeviceLike = None, dtype: DTypeLike = None)

Implementing kernel

C++ engine symbols that back this Python API.

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 ss and kernel size KK:

Lout=(Lin−1)⋅s−2p+d(K−1)+pout+1L_{\text{out}} = (L_{\text{in}} - 1) \cdot s - 2p + d(K - 1) + p_{\text{out}} + 1

where pp is padding, dd is dilation, and poutp_{\text{out}} is output_padding.

Parameters

in_channelsint
Number of channels in the input.
out_channelsint
Number of channels produced by the transposed convolution.
kernel_sizeint
Size of the convolving kernel.
strideint= 1
Stride of the convolution. Values > 1 upsample the input. Default: 1.
paddingint= 0
dilation * (kernel_size - 1) - padding zero-padding is added to both sides of each dimension in the input. Default: 0.
output_paddingint= 0
Additional size added to one side of the output shape. Must satisfy 0 <= output_padding < max(stride, dilation). Used to disambiguate the output size when the formula (Lin−1)⋅s−2p+d(K−1)+1(L_{\text{in}} - 1) \cdot s - 2p + d(K-1) + 1 is compatible with multiple LinL_{\text{in}}. Default: 0.
groupsint= 1
Number of blocked connections. Default: 1.
biasbool= True
If True, adds a learnable bias. Default: True.
dilationint= 1
Spacing between kernel elements. Default: 1.
padding_modestr= 'zeros'
"zeros", the only mode; another raises ValueError.
deviceDeviceLike= None
Device on which to allocate parameters. Default: None.
dtypeDTypeLike= None
Data type for the parameters. Default: None.

Attributes

weightParameter
Learnable kernel of shape (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 (a=5a = \sqrt{5}).
biasParameter or None
Learnable bias of shape (out_channels,), or None.

Notes

Input: (N,Cin,L)(N, C_{\text{in}}, L) Output: (N,Cout,Lout)(N, C_{\text{out}}, L_{\text{out}}) where

Lout=(L−1)⋅s−2p+d(K−1)+pout+1L_{\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 RCin×L→RCout×L′\mathbb{R}^{C_{\text{in}} \times L} \to \mathbb{R}^{C_{\text{out}} \times L'}, then ConvTranspose1d with the same weights maps back RCout×L′→RCin×L\mathbb{R}^{C_{\text{out}} \times L'} \to \mathbb{R}^{C_{\text{in}} \times L}. 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)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(in_channels: int, out_channels: int, kernel_size: int | tuple[int], stride: int | tuple[int] = 1, padding: int | tuple[int] = 0, output_padding: int | tuple[int] = 0, groups: int = 1, bias: bool = True, dilation: int | tuple[int] = 1, padding_mode: str = 'zeros', device: DeviceLike = None, dtype: DTypeLike = None)

Initialise the ConvTranspose1d module. See the class docstring for parameter semantics.

Instance methods

3
fn

extra_repr

→strfrom _ConvTransposeNd
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensor
forward(input: Tensor, output_size: Sequence[int] | None = None)

Upsample input of shape (N, C_in, L), or unbatched.

output_size picks one of the stride sizes the layer can produce, in place of output_padding: the output's length, optionally after its batch and channel sizes.

fn

reset_parameters

→Nonefrom _ConvParameters
reset_parameters()

Re-draw weight (Kaiming uniform) and bias (bounded by the weight's fan-in).