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::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(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 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(config: PcActorConfig, weights: PcActorWeights) -> Self
pub fn from_weights(config: PcActorConfig, weights: PcActorWeights) -> Self
Restores an actor from saved weights without requiring an RNG.
Converts CPU-backed weight snapshots to the target backend L.