Skip to main content

Conv2d

Struct Conv2d 

Source
pub struct Conv2d {
    pub weight: Parameter,
    pub bias: Option<Parameter>,
    pub in_channels: usize,
    pub out_channels: usize,
    pub kernel_size: usize,
    pub stride: usize,
    pub padding: usize,
}
Expand description

2D convolutional layer.

Uses the im2col algorithm: reshape input patches into columns, then compute the convolution as a single matmul per batch element.

Weight shape: [out_channels, in_channels * kernel_size * kernel_size]. Bias shape: [out_channels] (optional).

Forward: for each batch element:

  1. x_col = input[b].im2col(k, stride, padding)[col_height, num_patches]
  2. out_b = weight @ x_col[out_channels, num_patches]
  3. Stack all out_b[batch, out_channels, num_patches]
  4. Add channel bias if present.
  5. Reshape to [batch, out_channels, out_h, out_w].

All ops are individually tape-recorded — the autograd engine handles the entire backward pass via Kahn’s traversal.

Fields§

§weight: Parameter§bias: Option<Parameter>§in_channels: usize§out_channels: usize§kernel_size: usize§stride: usize§padding: usize

Implementations§

Source§

impl Conv2d

Source

pub fn new( in_channels: usize, out_channels: usize, kernel_size: usize, stride: usize, padding: usize, with_bias: bool, ) -> Self

Create a new Conv2d layer with Kaiming Uniform initialization.

Source

pub fn forward(&self, input: &Tensor) -> Tensor

Forward pass.

input shape: [batch, in_channels, height, width]. Output shape: [batch, out_channels, out_h, out_w].

Each step is a tracked op so the autograd tape records the full graph: slice_batch → im2col → matmul → add_channel_bias → stack.

Trait Implementations§

Source§

impl Module for Conv2d

Source§

fn parameters(&self) -> Vec<Parameter>

Return clones of all learnable parameters.
Source§

fn state_dict(&self, prefix: &str) -> HashMap<String, Tensor>

Serialize all parameters into a flat name → Tensor map. Read more
Source§

fn load_state_dict( &mut self, dict: &HashMap<String, Tensor>, prefix: &str, ) -> Result<(), AutogradError>

Load parameters from a flat name → Tensor map. Read more
Source§

fn train(&mut self)

Switch to training mode.
Source§

fn eval(&mut self)

Switch to evaluation mode.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.