Skip to main content

PcActor

Struct PcActor 

Source
pub struct PcActor<L: LinAlg = CpuLinAlg> {
    pub config: PcActorConfig,
    /* private fields */
}
Expand description

Predictive coding actor network.

Uses iterative top-down/bottom-up inference loops to produce stable hidden representations and output logits.

Generic over a LinAlg backend L. Defaults to CpuLinAlg.

§Examples

use pc_rl_core::activation::Activation;
use pc_rl_core::layer::LayerDef;
use pc_rl_core::linalg::cpu::CpuLinAlg;
use pc_rl_core::pc_actor::{PcActor, PcActorConfig, SelectionMode};
use rand::SeedableRng;
use rand::rngs::StdRng;

let config = PcActorConfig {
    input_size: 9,
    hidden_layers: vec![LayerDef { size: 18, activation: Activation::Tanh }],
    output_size: 9,
    output_activation: Activation::Tanh,
    alpha: 0.1, tol: 0.01, min_steps: 1, max_steps: 20,
    lr_weights: 0.01, synchronous: true, temperature: 1.0,
    local_lambda: 1.0,
    residual: false,
    rezero_init: 0.001,
};
let mut rng = StdRng::seed_from_u64(42);
let actor: PcActor = PcActor::new(CpuLinAlg::new(), config, &mut rng).unwrap();
let result = actor.infer(&[0.0; 9]);
assert_eq!(result.y_conv.len(), 9);

Fields§

§config: PcActorConfig

Actor configuration.

Implementations§

Source§

impl<L: LinAlg> PcActor<L>

Source

pub fn new( backend: L, config: PcActorConfig, rng: &mut impl Rng, ) -> Result<Self, PcError>

Creates a new PC actor with Xavier-initialized layers.

§Arguments
  • config - Actor configuration specifying topology and hyperparameters.
  • rng - Random number generator for weight initialization.
§Errors

Returns PcError::ConfigValidation if input_size, output_size, or temperature are invalid.

Source

pub fn crossover( parent_a: &PcActor<L>, parent_b: &PcActor<L>, caches_a: &[L::Matrix], caches_b: &[L::Matrix], alpha: f64, child_config: PcActorConfig, rng: &mut impl Rng, ) -> Result<Self, PcError>

Creates a child actor by crossing over two parent actors using CCA neuron alignment.

Aligns hidden neurons functionally via CCA before blending weights. Input and output layers use positional crossover (no permutation problem).

§Arguments
  • parent_a - First parent (reference, typically higher fitness).
  • parent_b - Second parent (aligned to A via CCA).
  • caches_a - Per-layer activation matrices for parent A [batch × neurons].
  • caches_b - Per-layer activation matrices for parent B [batch × neurons].
  • alpha - Blending weight: 1.0 = all A, 0.0 = all B.
  • child_config - Topology configuration for the child network.
  • rng - Random number generator for Xavier initialization.
§Errors

Returns PcError::ConfigValidation if child_config is invalid.

Source

pub fn latent_size(&self) -> usize

Returns the total size of the latent concatenation (sum of hidden layer sizes).

Source

pub fn infer(&self, input: &[f64]) -> InferResult<L>

Source

pub fn select_action( &self, y_conv: &L::Vector, valid_actions: &[usize], mode: SelectionMode, rng: &mut impl Rng, ) -> usize

Selects an action given converged output logits and valid actions.

§Arguments
  • y_conv - Output logits from inference.
  • valid_actions - Indices of valid actions.
  • mode - Training (stochastic) or Play (deterministic).
  • rng - Random number generator (used only in Training mode).
§Panics

Panics if valid_actions is empty.

Source

pub fn update_weights( &mut self, output_delta: &[f64], infer_result: &InferResult<L>, input: &[f64], surprise_scale: f64, )

Updates network weights using a blend of backprop and local PC error.

The local_lambda config controls the blend: 1.0 = pure backprop, 0.0 = pure local PC learning (Millidge et al. 2022), intermediate = hybrid.

§Arguments
  • output_delta - Error signal at the output layer.
  • infer_result - Result from the most recent inference.
  • input - Original input that was fed to infer.
  • surprise_scale - Multiplier on learning rate based on surprise.
§Panics

Panics if input.len() != config.input_size.

Source

pub fn to_weights(&self) -> PcActorWeights

Extracts a serializable snapshot of current weights.

Converts generic layers and skip projections to CPU-backed types.

Source

pub fn from_weights( backend: L, config: PcActorConfig, weights: PcActorWeights, ) -> Result<Self, PcError>

Restores an actor from saved weights without requiring an RNG.

Converts CPU-backed weight snapshots to the target backend L. Validates that all weight matrix dimensions and bias lengths match the expected topology from config.

§Errors

Returns PcError::DimensionMismatch if any weight matrix or bias vector has dimensions inconsistent with the config topology.

Trait Implementations§

Source§

impl<L: Debug + LinAlg> Debug for PcActor<L>
where L::Matrix: Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<L> Freeze for PcActor<L>
where L: Freeze,

§

impl<L> RefUnwindSafe for PcActor<L>

§

impl<L> Send for PcActor<L>

§

impl<L> Sync for PcActor<L>

§

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

§

impl<L> UnsafeUnpin for PcActor<L>
where L: UnsafeUnpin,

§

impl<L> UnwindSafe for PcActor<L>
where L: UnwindSafe, <L as LinAlg>::Matrix: UnwindSafe, <L as LinAlg>::Vector: 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> 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, 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