Skip to main content

Layer

Struct Layer 

Source
pub struct Layer { /* private fields */ }
Expand description

A dense layer: y = activation(Wx + b).

Weights use row-major layout with shape (out_dim, in_dim).

Implementations§

Source§

impl Layer

Source

pub fn activation(&self) -> Activation

Returns this layer’s activation.

Source§

impl Layer

Source

pub fn new_with_rng<R: Rng + ?Sized>( in_dim: usize, out_dim: usize, init: Init, activation: Activation, rng: &mut R, ) -> Result<Self>

Source

pub fn in_dim(&self) -> usize

Returns the input dimension.

Source

pub fn out_dim(&self) -> usize

Returns the output dimension.

Source

pub fn forward(&self, inputs: &[f32], outputs: &mut [f32])

Forward pass for a single sample.

Computes:

  • z = W * inputs + b
  • outputs = activation(z)

Shape contract:

  • inputs.len() == self.in_dim
  • outputs.len() == self.out_dim
Source

pub fn backward( &self, inputs: &[f32], outputs: &[f32], d_outputs: &[f32], d_inputs: &mut [f32], d_weights: &mut [f32], d_biases: &mut [f32], )

Backward pass for a single sample.

This uses overwrite semantics:

  • d_inputs is overwritten (and internally zeroed before accumulation)
  • d_weights is overwritten
  • d_biases is overwritten

Inputs:

  • inputs: the same inputs passed to forward
  • outputs: the outputs previously produced by forward (post-activation)
  • d_outputs: upstream gradient dL/d(outputs)

Shape contract:

  • inputs.len() == self.in_dim
  • outputs.len() == self.out_dim
  • d_outputs.len() == self.out_dim
  • d_inputs.len() == self.in_dim
  • d_weights.len() == self.weights.len()
  • d_biases.len() == self.out_dim
Source

pub fn backward_accumulate( &self, inputs: &[f32], outputs: &[f32], d_outputs: &[f32], d_inputs: &mut [f32], d_weights: &mut [f32], d_biases: &mut [f32], )

Backward pass for a single sample (parameter accumulation semantics).

This is identical to backward except that parameter gradients are accumulated:

  • d_inputs is overwritten (and internally zeroed before accumulation)
  • d_weights is accumulated into (+=)
  • d_biases is accumulated into (+=)

This is useful for mini-batch training where you sum gradients over multiple samples.

Shape contract:

  • inputs.len() == self.in_dim
  • outputs.len() == self.out_dim
  • d_outputs.len() == self.out_dim
  • d_inputs.len() == self.in_dim
  • d_weights.len() == self.weights.len()
  • d_biases.len() == self.out_dim
Source

pub fn sgd_step(&mut self, d_weights: &[f32], d_biases: &[f32], lr: f32)

Applies an SGD update: param -= lr * d_param.

Shape contract:

  • d_weights.len() == self.weights.len()
  • d_biases.len() == self.biases.len()

Trait Implementations§

Source§

impl Clone for Layer

Source§

fn clone(&self) -> Layer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Layer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&Layer> for SerializedLayer

Source§

fn from(layer: &Layer) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for Layer

§

impl RefUnwindSafe for Layer

§

impl Send for Layer

§

impl Sync for Layer

§

impl Unpin for Layer

§

impl UnwindSafe for Layer

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V