pub struct Conv2d<B>where
B: Backend,{ /* private fields */ }Expand description
2D convolutional layer.
Applies a set of learnable filters to a 4D input [N, C_in, H, W],
producing output of shape [N, C_out, H_out, W_out].
§Examples
ⓘ
let conv = Conv2d::<CpuBackend>::new(1, 16, [3, 3], [1, 1], [1, 1], true, DType::F32, &dev)?;
let x = CpuTensor::rand((4, 1, 28, 28), DType::F32, &dev)?;
let y = conv.forward(&x)?; // [4, 16, 28, 28]Implementations§
Source§impl<B> Conv2d<B>where
B: Backend,
impl<B> Conv2d<B>where
B: Backend,
Sourcepub fn new(
in_channels: usize,
out_channels: usize,
kernel_size: [usize; 2],
stride: [usize; 2],
padding: [usize; 2],
use_bias: bool,
dtype: DType,
device: &<B as Backend>::Device,
) -> Result<Conv2d<B>, Error>
pub fn new( in_channels: usize, out_channels: usize, kernel_size: [usize; 2], stride: [usize; 2], padding: [usize; 2], use_bias: bool, dtype: DType, device: &<B as Backend>::Device, ) -> Result<Conv2d<B>, Error>
Create a new Conv2d layer with Kaiming uniform initialization.
§Arguments
in_channels: number of input channels (C_in)out_channels: number of output channels / filters (C_out)kernel_size:[kH, kW]spatial size of each filterstride:[sH, sW]stride of the convolutionpadding:[pH, pW]zero-padding added to both sidesuse_bias: whether to include an additive biasdtype: data type for parametersdevice: device to create parameters on
Sourcepub fn from_tensors(
weight: Tensor<B>,
bias: Option<Tensor<B>>,
stride: [usize; 2],
padding: [usize; 2],
) -> Result<Conv2d<B>, Error>
pub fn from_tensors( weight: Tensor<B>, bias: Option<Tensor<B>>, stride: [usize; 2], padding: [usize; 2], ) -> Result<Conv2d<B>, Error>
Create a Conv2d from existing weight and bias tensors (e.g. for loading).
pub fn in_channels(&self) -> usize
pub fn out_channels(&self) -> usize
pub fn kernel_size(&self) -> [usize; 2]
pub fn stride(&self) -> [usize; 2]
pub fn padding(&self) -> [usize; 2]
pub fn weight(&self) -> &Tensor<B>
pub fn bias(&self) -> Option<&Tensor<B>>
Trait Implementations§
Source§impl<B> Module<B> for Conv2d<B>where
B: Backend,
impl<B> Module<B> for Conv2d<B>where
B: Backend,
Source§fn forward(&self, x: &Tensor<B>) -> Result<Tensor<B>, Error>
fn forward(&self, x: &Tensor<B>) -> Result<Tensor<B>, Error>
Forward pass: 2D convolution.
Input: [N, C_in, H, W]
Output: [N, C_out, H_out, W_out]
Source§fn parameters(&self) -> Vec<Tensor<B>>
fn parameters(&self) -> Vec<Tensor<B>>
Return all trainable parameters of this module.
The optimizer uses these to update weights during training.
Source§fn named_parameters(&self) -> Vec<(String, Tensor<B>)>
fn named_parameters(&self) -> Vec<(String, Tensor<B>)>
Return all trainable parameters with human-readable names. Read more
Source§fn set_training(&self, _training: bool)
fn set_training(&self, _training: bool)
Set training or evaluation mode. Read more
Source§fn is_training(&self) -> bool
fn is_training(&self) -> bool
Whether the module is in training mode (default: true).
Source§fn num_parameters(&self) -> usize
fn num_parameters(&self) -> usize
Total number of scalar parameters in this module.
Source§fn trainable_params_count(&self) -> usize
fn trainable_params_count(&self) -> usize
Number of trainable (variable) parameters.
Auto Trait Implementations§
impl<B> Freeze for Conv2d<B>
impl<B> RefUnwindSafe for Conv2d<B>
impl<B> Send for Conv2d<B>
impl<B> Sync for Conv2d<B>
impl<B> Unpin for Conv2d<B>
impl<B> UnwindSafe for Conv2d<B>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more