conv2d_backward_input

Function conv2d_backward_input 

Source
pub fn conv2d_backward_input<F>(
    grad_output: &ArrayView4<'_, F>,
    kernel: &ArrayView4<'_, F>,
    inputshape: (usize, usize, usize, usize),
    stride: (usize, usize),
    padding: (usize, usize),
    dilation: (usize, usize),
) -> LinalgResult<Array4<F>>
Expand description

Apply backward pass of convolution operation for input gradient

This function computes the gradient of the input in a convolutional layer given the gradient of the output.

§Arguments

  • grad_output - Gradient of the output tensor of shape (batchsize, out_channels_, output_h, output_w)
  • kernel - Kernel tensor of shape (out_channels_, in_channels, kernel_h, kernel_w)
  • inputshape - Shape of the input tensor (batchsize, in_channels, height, width)
  • stride - Stride as (stride_height, stride_width)
  • padding - Padding as (padding_height, padding_width)
  • dilation - Dilation as (dilation_height, dilation_width)

§Returns

  • Gradient of the input tensor of shape (batchsize, in_channels, height, width)

§Examples

use scirs2_core::ndarray::Array4;
use scirs2_linalg::convolution::{conv2d_im2col, conv2d_backward_input};

// Forward pass
let input = Array4::<f32>::zeros((2, 3, 32, 32));
let kernel = Array4::<f32>::zeros((16, 3, 3, 3));
let bias = None;
let output = conv2d_im2col(
    &input.view(),
    &kernel.view(),
    bias,
    (1, 1),
    (1, 1),
    (1, 1),
).unwrap();

// Backward pass
let grad_output = Array4::<f32>::ones((2, 16, 32, 32));
let grad_input = conv2d_backward_input(
    &grad_output.view(),
    &kernel.view(),
    (2, 3, 32, 32),
    (1, 1),
    (1, 1),
    (1, 1),
).unwrap();

// Gradient shape matches input shape
assert_eq!(grad_input.shape(), &[2, 3, 32, 32]);