pub struct Linear { /* private fields */ }Expand description
Fully connected layer: y = xW^T + b
Applies a linear transformation to the incoming data. Weight initialization follows Xavier/Glorot (Glorot & Bengio, 2010).
§Shape
- Input:
(*, in_features)where*means any number of batch dimensions - Output:
(*, out_features)
§Example
use aprender::nn::{Module, Linear};
use aprender::autograd::Tensor;
let layer = Linear::new(20, 30); // 20 inputs, 30 outputs
let x = Tensor::randn(&[128, 20]); // batch of 128
let output = layer.forward(&x); // [128, 30]
assert_eq!(output.shape(), &[128, 30]);Implementations§
Source§impl Linear
impl Linear
Sourcepub fn with_seed(
in_features: usize,
out_features: usize,
seed: Option<u64>,
) -> Self
pub fn with_seed( in_features: usize, out_features: usize, seed: Option<u64>, ) -> Self
Create a Linear layer with a specific random seed.
Sourcepub fn without_bias(in_features: usize, out_features: usize) -> Self
pub fn without_bias(in_features: usize, out_features: usize) -> Self
Create a Linear layer without bias.
Useful when followed by BatchNorm which has its own bias.
Sourcepub fn without_bias_with_seed(
in_features: usize,
out_features: usize,
seed: Option<u64>,
) -> Self
pub fn without_bias_with_seed( in_features: usize, out_features: usize, seed: Option<u64>, ) -> Self
Create a Linear layer without bias with a specific random seed.
Sourcepub fn in_features(&self) -> usize
pub fn in_features(&self) -> usize
Get the input feature dimension.
Sourcepub fn out_features(&self) -> usize
pub fn out_features(&self) -> usize
Get the output feature dimension.
Sourcepub fn set_weight(&mut self, weight: Tensor)
pub fn set_weight(&mut self, weight: Tensor)
Set weight tensor from external data.
Used for loading pre-trained weights from SafeTensors or other formats.
Automatically computes and caches the transposed weight for fast forward.
Sourcepub fn set_bias(&mut self, bias: Tensor)
pub fn set_bias(&mut self, bias: Tensor)
Set bias tensor from external data.
Used for loading pre-trained weights.
Sourcepub fn placeholder(in_features: usize, out_features: usize) -> Self
pub fn placeholder(in_features: usize, out_features: usize) -> Self
Create a placeholder Linear layer with minimal memory allocation.
Used for lazy initialization when loading pre-trained weights. The placeholder uses 1-element tensors instead of full matrices, reducing memory from O(in*out) to O(1).
IMPORTANT: This layer will NOT work for inference until
set_weight() is called with real weights.
Trait Implementations§
Source§impl Module for Linear
impl Module for Linear
Source§fn parameters_mut(&mut self) -> Vec<&mut Tensor>
fn parameters_mut(&mut self) -> Vec<&mut Tensor>
Source§fn refresh_caches(&mut self)
fn refresh_caches(&mut self)
Source§fn num_parameters(&self) -> usize
fn num_parameters(&self) -> usize
Auto Trait Implementations§
impl Freeze for Linear
impl !RefUnwindSafe for Linear
impl Send for Linear
impl Sync for Linear
impl Unpin for Linear
impl UnsafeUnpin for Linear
impl !UnwindSafe for Linear
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
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>
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>
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