class

ConvTranspose1d

extendsModule
ConvTranspose1d(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)
source

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=(Lin1)s2p+d(K1)+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 (Lin1)s2p+d(K1)+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.
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

$$

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 RCin×LRCout×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×LRCin×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)

Methods (3)

dunder

__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)
source

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

fn

forward

Tensor
forward(x: Tensor)
source

Apply the convolution to the input tensor.

Parameters

inputTensor
Input tensor of shape (N,Cin,)(N, C_{\text{in}}, *).

Returns

Tensor

Output tensor of shape (N,Cout,)(N, C_{\text{out}}, *) with spatial dimensions determined by stride, padding, dilation, and kernel size.

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.