pub struct BatchNorm2d<B>where
B: Backend,{ /* private fields */ }Expand description
2D Batch Normalization layer for convolutional networks.
Normalizes each channel across the batch: for input [N, C, H, W],
mean and variance are computed over (N, H, W) for each of C channels.
§Examples
ⓘ
let bn = BatchNorm2d::<CpuBackend>::new(16, 1e-5, 0.1, DType::F64, &dev)?;
let x: [batch, 16, H, W] tensor
let y = bn.forward(&x)?; // normalized, same shapeImplementations§
Source§impl<B> BatchNorm2d<B>where
B: Backend,
impl<B> BatchNorm2d<B>where
B: Backend,
Sourcepub fn new(
num_features: usize,
eps: f64,
momentum: f64,
dtype: DType,
device: &<B as Backend>::Device,
) -> Result<BatchNorm2d<B>, Error>
pub fn new( num_features: usize, eps: f64, momentum: f64, dtype: DType, device: &<B as Backend>::Device, ) -> Result<BatchNorm2d<B>, Error>
Create a new BatchNorm2d layer.
§Arguments
num_features: number of channels (C)eps: numerical stability constant (typically 1e-5)momentum: EMA momentum for running stats (typically 0.1)dtype: data type for learnable parametersdevice: device
Sourcepub fn is_training(&self) -> bool
pub fn is_training(&self) -> bool
Whether the module is in training mode.
pub fn num_features(&self) -> usize
pub fn eps(&self) -> f64
pub fn weight(&self) -> &Tensor<B>
pub fn bias(&self) -> &Tensor<B>
Sourcepub fn from_tensors(
weight: Tensor<B>,
bias: Tensor<B>,
eps: f64,
) -> Result<BatchNorm2d<B>, Error>
pub fn from_tensors( weight: Tensor<B>, bias: Tensor<B>, eps: f64, ) -> Result<BatchNorm2d<B>, Error>
Create from existing weight and bias tensors (for executor/model loading). Initializes running stats to mean=0, var=1.
Trait Implementations§
Source§impl<B> Module<B> for BatchNorm2d<B>where
B: Backend,
impl<B> Module<B> for BatchNorm2d<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: batch-normalize each channel.
Training: use batch mean/var, update running stats. Eval: use running mean/var.
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 BatchNorm2d<B>
impl<B> !RefUnwindSafe for BatchNorm2d<B>
impl<B> Send for BatchNorm2d<B>
impl<B> !Sync for BatchNorm2d<B>
impl<B> Unpin for BatchNorm2d<B>
impl<B> UnwindSafe for BatchNorm2d<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