Skip to main content

Layer

Struct Layer 

Source
pub struct Layer<L: LinAlg = CpuLinAlg> {
    pub weights: L::Matrix,
    pub bias: L::Vector,
    pub activation: Activation,
    /* private fields */
}
Expand description

A single dense layer with weights, bias, and activation function.

Generic over a LinAlg backend L. Defaults to CpuLinAlg for backward compatibility.

Weights have shape [output_size × input_size]. Bias has length output_size.

§Serde and the backend field

The backend field is annotated with #[serde(skip, default)] so it is not included in the JSON format. On deserialization, L::default() is used to fill the field. However, direct serde deserialization of Layer<GpuLinAlg> is not the intended usage path. Use PcActor::from_weights or MlpCritic::from_weights instead, which inject the correct backend instance into every layer.

§Examples

use pc_rl_core::activation::Activation;
use pc_rl_core::layer::Layer;
use pc_rl_core::linalg::cpu::CpuLinAlg;
use rand::SeedableRng;
use rand::rngs::StdRng;

let backend = CpuLinAlg::new();
let mut rng = StdRng::seed_from_u64(42);
let layer: Layer = Layer::new(4, 3, Activation::Tanh, &backend, &mut rng);
let output: Vec<f64> = layer.forward(&vec![1.0, 0.0, -1.0, 0.5]);
assert_eq!(output.len(), 3);

Fields§

§weights: L::Matrix

Weight matrix of shape [output_size × input_size].

§bias: L::Vector

Bias vector of length output_size.

§activation: Activation

Activation function applied element-wise after the linear transform.

Implementations§

Source§

impl<L: LinAlg> Layer<L>

Source

pub fn new( input_size: usize, output_size: usize, activation: Activation, backend: &L, rng: &mut impl Rng, ) -> Self

Creates a new layer with Xavier-initialized weights and zero bias.

§Arguments
  • input_size - Number of inputs to this layer.
  • output_size - Number of neurons (outputs) in this layer.
  • activation - Activation function to apply after the linear transform.
  • rng - Random number generator for weight initialization.
Source

pub fn forward(&self, input: &L::Vector) -> L::Vector

Computes activation(W * input + bias).

§Panics

Panics if input.len() != input_size (number of columns in weights).

Source

pub fn transpose_forward( &self, input: &L::Vector, activation: Activation, ) -> L::Vector

Computes activation(W^T * input) (no bias).

Used for PC top-down predictions. The activation parameter is separate from self.activation because at the output→last-hidden boundary, different activations may apply.

§Panics

Panics if input.len() != output_size (number of rows in weights).

Source

pub fn backward( &mut self, input: &L::Vector, output: &L::Vector, delta: &L::Vector, lr: f64, surprise_scale: f64, ) -> L::Vector

Backpropagation with gradient and weight clipping.

Returns the propagated delta for the layer below (length = input_size).

§Arguments
  • input - Input that was fed to this layer during forward pass.
  • output - Output of this layer from the forward pass (post-activation).
  • delta - Error signal from the layer above.
  • lr - Base learning rate.
  • surprise_scale - Multiplier on lr based on surprise score.
§Panics

Panics on dimension mismatches.

Trait Implementations§

Source§

impl<L: Clone + LinAlg> Clone for Layer<L>
where L::Matrix: Clone, L::Vector: Clone,

Source§

fn clone(&self) -> Layer<L>

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<L: Debug + LinAlg> Debug for Layer<L>
where L::Matrix: Debug, L::Vector: Debug,

Source§

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

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

impl<'de, L> Deserialize<'de> for Layer<L>
where L::Matrix: for<'a> Deserialize<'a>, L::Vector: for<'a> Deserialize<'a>, L: Default + LinAlg,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<L: LinAlg> Serialize for Layer<L>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<L> Freeze for Layer<L>
where <L as LinAlg>::Matrix: Freeze, <L as LinAlg>::Vector: Freeze, L: Freeze,

§

impl<L> RefUnwindSafe for Layer<L>

§

impl<L> Send for Layer<L>

§

impl<L> Sync for Layer<L>

§

impl<L> Unpin for Layer<L>
where <L as LinAlg>::Matrix: Unpin, <L as LinAlg>::Vector: Unpin, L: Unpin,

§

impl<L> UnsafeUnpin for Layer<L>

§

impl<L> UnwindSafe for Layer<L>
where <L as LinAlg>::Matrix: UnwindSafe, <L as LinAlg>::Vector: UnwindSafe, L: UnwindSafe,

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

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,