pub struct Linear<const I: usize, const O: usize> {
pub weight: Tensor2D<O, I, NoneTape>,
pub bias: Tensor1D<O, NoneTape>,
}
Expand description
A linear transformation of the form weight * x + bias
, where weight
is a matrix, x
is a vector or matrix,
and bias
is a vector.
Generics
I
The “input” size of vectors & matrices.O
The “output” size of vectors & matrices.
Examples
Linear<5, 2>
can act on vectors with 5 elements, and results in vectors with 2 elements.
let model: Linear<5, 2> = Default::default();
assert_eq!(model.weight.data(), &[[0.0; 5]; 2]);
assert_eq!(model.bias.data(), &[0.0; 2]);
let x: Tensor1D<5> = Default::default();
let y: Tensor1D<2> = model.forward(x);
assert_eq!(y.data(), &[0.0; 2]);
Fields
weight: Tensor2D<O, I, NoneTape>
Transposed weight matrix, shape (O, I)
bias: Tensor1D<O, NoneTape>
Bias vector, shape (O, )
Trait Implementations
sourceimpl<const I: usize, const O: usize> CanUpdateWithGradients for Linear<I, O>
impl<const I: usize, const O: usize> CanUpdateWithGradients for Linear<I, O>
sourcefn update<G: GradientProvider>(
&mut self,
grads: &mut G,
unused: &mut UnusedTensors
)
fn update<G: GradientProvider>(
&mut self,
grads: &mut G,
unused: &mut UnusedTensors
)
Updates self given the GradientProvider. When any parameters that
are NOT present in G
, then this function should
add the tensor’s UniqueId to UnusedTensors. Read more
sourceimpl<const I: usize, const O: usize> LoadFromNpz for Linear<I, O>
impl<const I: usize, const O: usize> LoadFromNpz for Linear<I, O>
sourcefn read<R>(&mut self, pre: &str, r: &mut ZipArchive<R>) -> Result<(), NpzError> where
R: Read + Seek,
fn read<R>(&mut self, pre: &str, r: &mut ZipArchive<R>) -> Result<(), NpzError> where
R: Read + Seek,
Reads Self::weight from {pre}weight.npy
and Self::bias from {pre}bias.npy
using npz_fread().
sourceimpl<const I: usize, const O: usize, H: Tape> Module<Tensor1D<I, H>> for Linear<I, O>
impl<const I: usize, const O: usize, H: Tape> Module<Tensor1D<I, H>> for Linear<I, O>
sourcefn forward_mut(&mut self, input: Input) -> Self::Output
fn forward_mut(&mut self, input: Input) -> Self::Output
Pass an Input
through the unit and produce Self::Output.
Can be implemented for multiple Input
types. Read more
sourceimpl<const B: usize, const I: usize, const O: usize, H: Tape> Module<Tensor2D<B, I, H>> for Linear<I, O>
impl<const B: usize, const I: usize, const O: usize, H: Tape> Module<Tensor2D<B, I, H>> for Linear<I, O>
sourcefn forward_mut(&mut self, input: Input) -> Self::Output
fn forward_mut(&mut self, input: Input) -> Self::Output
Pass an Input
through the unit and produce Self::Output.
Can be implemented for multiple Input
types. Read more
sourceimpl<const B: usize, const S: usize, const I: usize, const O: usize, H: Tape> Module<Tensor3D<B, S, I, H>> for Linear<I, O>
impl<const B: usize, const S: usize, const I: usize, const O: usize, H: Tape> Module<Tensor3D<B, S, I, H>> for Linear<I, O>
sourcefn forward_mut(&mut self, input: Input) -> Self::Output
fn forward_mut(&mut self, input: Input) -> Self::Output
Pass an Input
through the unit and produce Self::Output.
Can be implemented for multiple Input
types. Read more
sourceimpl<const I: usize, const O: usize> ResetParams for Linear<I, O>
impl<const I: usize, const O: usize> ResetParams for Linear<I, O>
sourcefn reset_params<R: Rng>(&mut self, rng: &mut R)
fn reset_params<R: Rng>(&mut self, rng: &mut R)
Initializes Self::weight and Self::bias from a Uniform distribution between [-1 / sqrt(I), 1 / sqrt(I)].
This uses Randomize::randomize() to set the values of the tensor.
Auto Trait Implementations
impl<const I: usize, const O: usize> RefUnwindSafe for Linear<I, O>
impl<const I: usize, const O: usize> !Send for Linear<I, O>
impl<const I: usize, const O: usize> !Sync for Linear<I, O>
impl<const I: usize, const O: usize> Unpin for Linear<I, O>
impl<const I: usize, const O: usize> UnwindSafe for Linear<I, O>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more