pub struct LinearLayer {
pub weight: Array2<f64>,
pub bias: Array2<f64>,
pub input_size: usize,
pub output_size: usize,
/* private fields */
}Expand description
A fully connected (linear/dense) layer for neural networks
Performs the transformation: output = input * weight^T + bias where weight has shape (output_size, input_size) and bias has shape (output_size, 1)
Fields§
§weight: Array2<f64>§bias: Array2<f64>§input_size: usize§output_size: usizeImplementations§
Source§impl LinearLayer
impl LinearLayer
Sourcepub fn new_zeros(input_size: usize, output_size: usize) -> Self
pub fn new_zeros(input_size: usize, output_size: usize) -> Self
Create a new linear layer with zero initialization
Sourcepub fn from_weights(weight: Array2<f64>, bias: Array2<f64>) -> Self
pub fn from_weights(weight: Array2<f64>, bias: Array2<f64>) -> Self
Create a new linear layer with custom initialization
Sourcepub fn backward(
&self,
grad_output: &Array2<f64>,
) -> (LinearGradients, Array2<f64>)
pub fn backward( &self, grad_output: &Array2<f64>, ) -> (LinearGradients, Array2<f64>)
Backward pass through the linear layer
§Arguments
grad_output- Gradient w.r.t. output of shape (output_size, batch_size)
§Returns
- Tuple of (gradients, input_gradient)
- gradients: LinearGradients containing weight and bias gradients
- input_gradient: Gradient w.r.t. input of shape (input_size, batch_size)
Sourcepub fn update_parameters<O: Optimizer>(
&mut self,
gradients: &LinearGradients,
optimizer: &mut O,
prefix: &str,
)
pub fn update_parameters<O: Optimizer>( &mut self, gradients: &LinearGradients, optimizer: &mut O, prefix: &str, )
Update parameters using the provided optimizer
Sourcepub fn zero_gradients(&self) -> LinearGradients
pub fn zero_gradients(&self) -> LinearGradients
Initialize zero gradients for accumulation
Sourcepub fn num_parameters(&self) -> usize
pub fn num_parameters(&self) -> usize
Get the number of parameters in this layer
Sourcepub fn dimensions(&self) -> (usize, usize)
pub fn dimensions(&self) -> (usize, usize)
Get layer dimensions
Trait Implementations§
Source§impl Clone for LinearLayer
impl Clone for LinearLayer
Source§fn clone(&self) -> LinearLayer
fn clone(&self) -> LinearLayer
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for LinearLayer
impl RefUnwindSafe for LinearLayer
impl Send for LinearLayer
impl Sync for LinearLayer
impl Unpin for LinearLayer
impl UnwindSafe for LinearLayer
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