conv_transpose2d

Function conv_transpose2d 

Source
pub fn conv_transpose2d<F>(
    input: &ArrayView4<'_, F>,
    kernel: &ArrayView4<'_, F>,
    bias: Option<ArrayView1<'_, F>>,
    stride: (usize, usize),
    padding: (usize, usize),
    output_padding: (usize, usize),
    dilation: (usize, usize),
) -> LinalgResult<Array4<F>>
Expand description

Apply 2D transposed convolution (deconvolution) operation

This function implements a transposed convolution (also known as deconvolution or fractionally-strided convolution), which is commonly used in convolutional neural networks for upsampling.

§Arguments

  • input - Input tensor of shape (batchsize, channels, height, width)
  • kernel - Kernel tensor of shape (in_channels, out_channels_, kernel_h, kernel_w)
  • bias - Optional bias tensor of shape (out_channels_,)
  • stride - Stride as (stride_height, stride_width)
  • padding - Padding as (padding_height, padding_width)
  • output_padding - Additional padding for output as (padding_height, padding_width)
  • dilation - Dilation as (dilation_height, dilation_width)

§Returns

  • Output tensor of shape (batchsize, out_channels_, output_height, output_width)

§Examples

use scirs2_core::ndarray::{Array, Array4};
use scirs2_linalg::convolution::conv_transpose2d;

// Simple example with smaller dimensions
let input = Array4::<f32>::zeros((1, 2, 3, 3));

// Create a 2x1x2x2 kernel tensor (2 input channels, 1 output channel, 2x2 kernel)
let kernel = Array4::<f32>::zeros((2, 1, 2, 2));

// Apply transposed convolution
let output = conv_transpose2d(
    &input.view(),
    &kernel.view(),
    None,        // no bias
    (1, 1),      // stride
    (0, 0),      // padding
    (0, 0),      // output_padding
    (1, 1),      // dilation
).unwrap();

// Calculate expected output shape:
// output_h = (3 - 1) * 1 - 2 * 0 + 1 * (2 - 1) + 0 + 1 = 2 + 1 + 1 = 4
// output_w = (3 - 1) * 1 - 2 * 0 + 1 * (2 - 1) + 0 + 1 = 2 + 1 + 1 = 4
assert_eq!(output.shape(), &[1, 1, 4, 4]);