Implementation:LaurentMazare Tch rs Conv Transpose
| Knowledge Sources | |
|---|---|
| Domains | Neural Networks, Convolution, Deep Learning |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
The conv_transpose module provides transposed (deconvolution) layers for 1D, 2D, and 3D inputs, using a generic ConvTransposeND<ND> struct parameterized by dimension-specific array types.
Description
This module implements transposed convolution layers that perform the inverse spatial operation of standard convolution, commonly used in decoder networks, generative models, and upsampling pipelines. The design mirrors the standard convolution module but with the addition of output_padding to resolve the ambiguity in output size that arises from transposed convolutions.
The core abstraction is ConvTransposeConfigND<ND>, a generic configuration struct parameterized over the dimension type ND. It holds stride, padding, output_padding, dilation, groups, bias, ws_init (weight initialization), and bs_init (bias initialization). A convenience type alias ConvTransposeConfig uses i64 as the dimension type for uniform per-dimension values.
The layer struct ConvTransposeND<ND> stores the weight tensor ws, an optional bias tensor bs, and the configuration. Three type aliases are provided: ConvTranspose1D ([i64; 1]), ConvTranspose2D ([i64; 2]), and ConvTranspose3D ([i64; 3]). A private Create trait handles conversion from uniform i64 values to dimension-specific arrays.
Each layer type implements Module::forward by delegating to the corresponding Tensor::conv_transpose{1,2,3}d method.
Usage
Use transposed convolution layers for upsampling in U-Net architectures, generative adversarial networks (GANs), variational autoencoders (VAEs), or any model requiring learnable spatial upsampling.
Code Reference
Source Location
- Repository: LaurentMazare_Tch_rs
- File: src/nn/conv_transpose.rs
Signature
// Configuration
pub struct ConvTransposeConfigND<ND> {
pub stride: ND,
pub padding: ND,
pub output_padding: ND,
pub groups: i64,
pub bias: bool,
pub dilation: ND,
pub ws_init: super::Init,
pub bs_init: super::Init,
}
pub type ConvTransposeConfig = ConvTransposeConfigND<i64>;
// Layer struct
pub struct ConvTransposeND<ND> {
pub ws: Tensor,
pub bs: Option<Tensor>,
config: ConvTransposeConfigND<ND>,
}
pub type ConvTranspose1D = ConvTransposeND<[i64; 1]>;
pub type ConvTranspose2D = ConvTransposeND<[i64; 2]>;
pub type ConvTranspose3D = ConvTransposeND<[i64; 3]>;
// Constructor functions
pub fn conv_transpose1d(vs: T, i: i64, o: i64, k: i64, c: ConvTransposeConfig) -> ConvTranspose1D;
pub fn conv_transpose2d(vs: T, i: i64, o: i64, k: i64, c: ConvTransposeConfig) -> ConvTranspose2D;
pub fn conv_transpose3d(vs: T, i: i64, o: i64, k: i64, c: ConvTransposeConfig) -> ConvTranspose3D;
Import
use tch::nn::{conv_transpose1d, conv_transpose2d, conv_transpose3d, ConvTransposeConfig};
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| vs | impl Borrow<Path> | Variable store path for parameter allocation |
| i | i64 | Number of input channels |
| o | i64 | Number of output channels |
| k | i64 | Kernel size (uniform across all dimensions) |
| c | ConvTransposeConfig | Configuration with stride, padding, output_padding, dilation, groups, bias, and init |
| Config Field | Default Value |
|---|---|
| stride | 1 |
| padding | 0 |
| output_padding | 0 |
| dilation | 1 |
| groups | 1 |
| bias | true |
| ws_init | DEFAULT_KAIMING_UNIFORM |
| bs_init | Const(0.) |
| Forward Input | Forward Output |
|---|---|
| &Tensor with shape matching the layer dimensionality | Tensor with transposed convolution applied |
Usage Examples
use tch::{nn, nn::Module, Device};
let vs = nn::VarStore::new(Device::Cpu);
let root = vs.root();
// Create a 2D transposed convolution: 64 input channels -> 32 output channels, 4x4 kernel
let deconv = nn::conv_transpose2d(&root / "deconv", 64, 32, 4, nn::ConvTransposeConfig {
stride: 2,
padding: 1,
..Default::default()
});
// Forward pass: upsamples spatial dimensions by factor of 2
let input = tch::Tensor::randn([1, 64, 8, 8], (tch::Kind::Float, Device::Cpu));
let output = deconv.forward(&input);
// output shape: [1, 32, 16, 16]