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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Reads Self::weight from {pre}weight.npy and Self::bias from {pre}bias.npy using npz_fread().

Loads data from a .npz zip archive at the specified path. Read more

1d forward using vecmat_mul() and add().

The type that this unit produces given Input.

Pass an Input through the unit and produce Self::Output. Can be implemented for multiple Input types. Read more

Batched 2d forward using matmul() and add()

The type that this unit produces given Input.

Pass an Input through the unit and produce Self::Output. Can be implemented for multiple Input types. Read more

Batched 3d forward using matmul() and add()

The type that this unit produces given Input.

Pass an Input through the unit and produce Self::Output. Can be implemented for multiple Input types. Read more

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.

Saves Self::weight to {pre}weight.npy and Self::bias to {pre}bias.npy using npz_fwrite().

Save this object into the .npz file determined located at path. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.