pub struct Linear { /* private fields */ }Expand description
A linear transformation layer (fully connected layer).
The Linear layer applies a linear transformation to the incoming data:
y = xW^T + b. This is one of the most fundamental building blocks in
neural networks.
§Parameters
weight: Learnable weight matrix of shape[out_features, in_features]bias: Optional learnable bias vector of shape[out_features]
§Input/Output Shapes
- Input:
[..., in_features]- Can be 2D or 3D - Output:
[..., out_features]- Same number of dimensions as input
§Example
use trustformers_core::layers::Linear;
use trustformers_core::tensor::Tensor;
use trustformers_core::traits::Layer;
// Create a linear layer: 768 → 3072
let linear = Linear::new(768, 3072, true);
// Apply to 2D input: [seq_len, in_features]
let input_2d = Tensor::randn(&[128, 768])?;
let output_2d = linear.forward(input_2d)?; // Shape: [128, 3072]
// Apply to 3D input: [batch, seq_len, in_features]
let input_3d = Tensor::randn(&[4, 128, 768])?;
let output_3d = linear.forward(input_3d)?; // Shape: [4, 128, 3072]Implementations§
Source§impl Linear
impl Linear
Sourcepub fn new(in_features: usize, out_features: usize, bias: bool) -> Self
pub fn new(in_features: usize, out_features: usize, bias: bool) -> Self
Creates a new linear layer.
§Arguments
in_features- Size of each input sampleout_features- Size of each output samplebias- Whether to include a learnable bias
§Returns
A new Linear layer with randomly initialized weights using a normal
distribution, and bias initialized to zeros if enabled.
§Example
use trustformers_core::layers::Linear;
// Linear layer without bias
let linear1 = Linear::new(512, 1024, false);
// Linear layer with bias
let linear2 = Linear::new(512, 1024, true);Sourcepub fn new_with_device(
in_features: usize,
out_features: usize,
bias: bool,
device: Device,
) -> Self
pub fn new_with_device( in_features: usize, out_features: usize, bias: bool, device: Device, ) -> Self
Creates a new linear layer with specified device.
§Arguments
in_features- Size of each input sampleout_features- Size of each output samplebias- Whether to include a learnable biasdevice- Device to use for computations (CPU, Metal, CUDA, etc.)
§Returns
A new Linear layer with randomly initialized weights.
§Example
use trustformers_core::layers::Linear;
use trustformers_core::Device;
// Create a linear layer on Metal GPU
let linear = Linear::new_with_device(768, 3072, true, Device::metal_if_available(0));Sourcepub fn set_weight(&mut self, weight: Tensor) -> Result<()>
pub fn set_weight(&mut self, weight: Tensor) -> Result<()>
Sourcepub fn weight(&self) -> &Tensor
pub fn weight(&self) -> &Tensor
Returns a reference to the weight matrix.
§Returns
A reference to the weight tensor of shape [out_features, in_features].
Sourcepub fn bias(&self) -> Option<&Tensor>
pub fn bias(&self) -> Option<&Tensor>
Returns a reference to the bias vector if present.
§Returns
Some(&bias) if bias is enabled, None otherwise.
Sourcepub fn parameter_count(&self) -> usize
pub fn parameter_count(&self) -> usize
Returns the total number of learnable parameters in this layer.
§Returns
The total parameter count including weights and bias (if present).
Trait Implementations§
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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