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: PcActorConfigActor configuration.
Implementations§
Source§impl<L: LinAlg> PcActor<L>
impl<L: LinAlg> PcActor<L>
Sourcepub 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>
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.
Sourcepub fn latent_size(&self) -> usize
pub fn latent_size(&self) -> usize
Returns the total size of the latent concatenation (sum of hidden layer sizes).
pub fn infer(&self, input: &[f64]) -> InferResult<L>
Sourcepub fn select_action(
&self,
y_conv: &L::Vector,
valid_actions: &[usize],
mode: SelectionMode,
rng: &mut impl Rng,
) -> usize
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.
Sourcepub fn update_weights(
&mut self,
output_delta: &[f64],
infer_result: &InferResult<L>,
input: &[f64],
surprise_scale: f64,
)
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 toinfer.surprise_scale- Multiplier on learning rate based on surprise.
§Panics
Panics if input.len() != config.input_size.
Sourcepub fn to_weights(&self) -> PcActorWeights
pub fn to_weights(&self) -> PcActorWeights
Extracts a serializable snapshot of current weights.
Converts generic layers and skip projections to CPU-backed types.
Sourcepub fn from_weights(
backend: L,
config: PcActorConfig,
weights: PcActorWeights,
) -> Result<Self, PcError>
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.