Deepity
Ultra-fast, SIMD-accelerated Predictive Coding
Loading...
Searching...
No Matches
Deep::FullPCNetwork Class Reference

Predictive Coding network combining muPC scaling, optional residual connections, and DKP direct feedback – see the file-level note above for how its defaults reproduce DirectKPPCNetwork exactly. More...

#include <FullPCNetwork.h>

Public Member Functions

 FullPCNetwork (int batchSize, DeviceType device=DeviceType::DEVICE_CPU) noexcept
 Constructs an empty network; add layers via AddLayer(), then Compile() before use.
 ~FullPCNetwork ()=default
 FullPCNetwork (const FullPCNetwork &)=delete
FullPCNetwork & operator= (const FullPCNetwork &)=delete
void AddLayer (size_t size, size_t nextSize, size_t terminalSize, float lr, float ir, float fl, float lmbda, ActivationType aType, ActivationType dType)
 Adds a layer to the network.
void SetUseMuPCScaling (bool enabled) noexcept
 Enables muPC-style per-layer forward scaling (Table 1 of the muPC paper). OFF by default. Must be called before Compile() – Compile() is what actually computes and applies each layer's a, once the full architecture (every AddLayer call) is known.
void SetUseResidualConnections (bool enabled) noexcept
 Enables residual/skip connections on middle hidden layers (muPC's "1-skip" ResNet formulation – see that paper's A.2.4). OFF by default. Requires every middle hidden layer to have the SAME width as its neighbor; Compile() throws std::invalid_argument if that's violated, rather than silently skipping the mismatched connection (a silent skip risks masking a real configuration mistake).
void RandomizeWeights (std::mt19937 &rng)
 Randomizes the weights (W and Psi) of each layer.
void ResetState () noexcept
 Resets each layer's state without touching learned weights.
void Clamp (const std::vector< float > &input)
 Clamps the input to the first layer.
void ProjectForward () noexcept
 Seeds hidden layers from a genuine forward pass through current weights, instead of zero-init. Call after Clamp(), before the settling loop.
float CalculateTerminalError () noexcept
 Computes the terminal layer's error/energy against its clamped target. Call AFTER clamping the target onto the terminal layer, and BEFORE DirectFeedbackUpdate().
void DirectFeedbackUpdate () noexcept
 Runs the DFA phase on every non-terminal layer: perturbs each layer's W using the layer above's Psi and the terminal layer's error.
float Step (bool computeEnergy=true) noexcept
 Runs one settling step on every layer.
void UpdateWeights () noexcept
 Applies weight updates to every layer.
void SetOptimizer (OptimizerType o) noexcept
 Sets the weight optimizer (W) on every layer.
void SetPsiOptimizer (OptimizerType o) noexcept
 Sets the feedback-weight optimizer (Psi) on every layer.
void SetLearningRate (float lr) noexcept
 Sets the learning rate (W) on every layer.
void SetFeedbackRate (float fl) noexcept
 Sets the feedback rate (Psi) on every layer.
FullPCLayer * GetTerminalLayer () noexcept
 Returns the last layer (nextSize==0, the terminal one).
std::vector< std::unique_ptr< FullPCLayer > > & GetLayers () noexcept
 Returns every layer in the network, in the order they were added.
const std::vector< std::unique_ptr< FullPCLayer > > & GetLayers () const noexcept
 const overload of GetLayers() above.
int GetBatchSize () const noexcept
 Returns the batch size given at construction.
DeviceType GetDevice () const noexcept
 Returns which device this network's layers run on.
float TrainStep (const std::vector< float > &x, const std::vector< float > &y, int inferenceSteps=1)
 Full train step: reset, clamp input+target, run all four DKP-PC phases in order, unclamp.
std::vector< float > Predict (const std::vector< float > &x, int inferenceSteps)
 Forward prediction: clamp input, settle with no DFA perturbation and no target clamped, read the terminal's beliefs.
void Compile ()
 Loads all layers into one contiguous block of memory, wires layerAbove/layerBelow/terminalLayer across every layer – same as DirectKPPCNetwork::Compile() – PLUS: if useMuPCScaling, computes and applies each layer's a (Table 1) from the full, now-known architecture; if useResidualConnections, applies SetResidual(true) to every middle hidden layer after checking width match (throws std::invalid_argument on mismatch).
void SetUseIPC (bool enabled) noexcept
 Enables iPC: UpdateWeights() runs every settling step instead of once after settling completes. OFF by default – TrainStep() matches DirectKPPCNetwork's standard, two-phase behavior exactly when this is false.
void SetUseMomentum (bool enabled, float beta=0.9f) noexcept
 Enables momentum (inertial) settling on every layer. Loops over layers and calls each one's own SetMomentum() – same pattern as SetLearningRate/SetFeedbackRate. OFF by default.
void SetUseCrossEntropy (bool enabled) noexcept
 Enables softmax cross-entropy energy on the TERMINAL layer only (unlike the other Set* toggles, this does NOT loop over every layer – cross-entropy only ever makes sense on the final output, against a one-hot/class-probability target). OFF by default (plain Gaussian energy everywhere, matching DirectKPPCNetwork).

Detailed Description

Predictive Coding network combining muPC scaling, optional residual connections, and DKP direct feedback – see the file-level note above for how its defaults reproduce DirectKPPCNetwork exactly.

Constructor & Destructor Documentation

◆ FullPCNetwork() [1/2]

Deep::FullPCNetwork::FullPCNetwork ( int batchSize,
DeviceType device = DeviceType::DEVICE_CPU )
explicitnoexcept

Constructs an empty network; add layers via AddLayer(), then Compile() before use.

Parameters
batchSizeFixed batch size for every layer.
deviceWhich device this network's layers run on.

◆ ~FullPCNetwork()

Deep::FullPCNetwork::~FullPCNetwork ( )
default

◆ FullPCNetwork() [2/2]

Deep::FullPCNetwork::FullPCNetwork ( const FullPCNetwork & )
delete

Member Function Documentation

◆ AddLayer()

void Deep::FullPCNetwork::AddLayer ( size_t size,
size_t nextSize,
size_t terminalSize,
float lr,
float ir,
float fl,
float lmbda,
ActivationType aType,
ActivationType dType )

Adds a layer to the network.

Parameters
sizeinput size
nextSizeoutput size (0 marks a terminal layer)
terminalSizethe size of the network's final output layer (e.g. 10 for MNIST) – required on every AddLayer call, not inferred, since the true terminal layer isn't known until the whole network has been assembled. Compile() sanity-checks this against the actual last layer's size.
lrlearning rate for W
irinference rate (Euler integration step size)
flfeedback rate (see FullPCLayer's own docs)
lmbdaweight decay (L2 regularization) coefficient, shared between W and Psi
aTypeactivation type
dTypeactivation derivative type

◆ CalculateTerminalError()

float Deep::FullPCNetwork::CalculateTerminalError ( )
noexcept

Computes the terminal layer's error/energy against its clamped target. Call AFTER clamping the target onto the terminal layer, and BEFORE DirectFeedbackUpdate().

Returns
The terminal layer's energy contribution.

◆ Clamp()

void Deep::FullPCNetwork::Clamp ( const std::vector< float > & input)

Clamps the input to the first layer.

Parameters
inputReference to the input vector.

◆ Compile()

void Deep::FullPCNetwork::Compile ( )

Loads all layers into one contiguous block of memory, wires layerAbove/layerBelow/terminalLayer across every layer – same as DirectKPPCNetwork::Compile() – PLUS: if useMuPCScaling, computes and applies each layer's a (Table 1) from the full, now-known architecture; if useResidualConnections, applies SetResidual(true) to every middle hidden layer after checking width match (throws std::invalid_argument on mismatch).

◆ DirectFeedbackUpdate()

void Deep::FullPCNetwork::DirectFeedbackUpdate ( )
noexcept

Runs the DFA phase on every non-terminal layer: perturbs each layer's W using the layer above's Psi and the terminal layer's error.

◆ GetBatchSize()

int Deep::FullPCNetwork::GetBatchSize ( ) const
inlinenoexcept

Returns the batch size given at construction.

◆ GetDevice()

DeviceType Deep::FullPCNetwork::GetDevice ( ) const
inlinenoexcept

Returns which device this network's layers run on.

◆ GetLayers() [1/2]

const std::vector< std::unique_ptr< FullPCLayer > > & Deep::FullPCNetwork::GetLayers ( ) const
inlinenoexcept

const overload of GetLayers() above.

◆ GetLayers() [2/2]

std::vector< std::unique_ptr< FullPCLayer > > & Deep::FullPCNetwork::GetLayers ( )
inlinenoexcept

Returns every layer in the network, in the order they were added.

◆ GetTerminalLayer()

FullPCLayer * Deep::FullPCNetwork::GetTerminalLayer ( )
inlinenoexcept

Returns the last layer (nextSize==0, the terminal one).

◆ operator=()

FullPCNetwork & Deep::FullPCNetwork::operator= ( const FullPCNetwork & )
delete

◆ Predict()

std::vector< float > Deep::FullPCNetwork::Predict ( const std::vector< float > & x,
int inferenceSteps )

Forward prediction: clamp input, settle with no DFA perturbation and no target clamped, read the terminal's beliefs.

◆ ProjectForward()

void Deep::FullPCNetwork::ProjectForward ( )
noexcept

Seeds hidden layers from a genuine forward pass through current weights, instead of zero-init. Call after Clamp(), before the settling loop.

◆ RandomizeWeights()

void Deep::FullPCNetwork::RandomizeWeights ( std::mt19937 & rng)

Randomizes the weights (W and Psi) of each layer.

Parameters
rngThe classic Mersenne Twister

◆ ResetState()

void Deep::FullPCNetwork::ResetState ( )
noexcept

Resets each layer's state without touching learned weights.

◆ SetFeedbackRate()

void Deep::FullPCNetwork::SetFeedbackRate ( float fl)
inlinenoexcept

Sets the feedback rate (Psi) on every layer.

◆ SetLearningRate()

void Deep::FullPCNetwork::SetLearningRate ( float lr)
inlinenoexcept

Sets the learning rate (W) on every layer.

◆ SetOptimizer()

void Deep::FullPCNetwork::SetOptimizer ( OptimizerType o)
inlinenoexcept

Sets the weight optimizer (W) on every layer.

◆ SetPsiOptimizer()

void Deep::FullPCNetwork::SetPsiOptimizer ( OptimizerType o)
inlinenoexcept

Sets the feedback-weight optimizer (Psi) on every layer.

◆ SetUseCrossEntropy()

void Deep::FullPCNetwork::SetUseCrossEntropy ( bool enabled)
inlinenoexcept

Enables softmax cross-entropy energy on the TERMINAL layer only (unlike the other Set* toggles, this does NOT loop over every layer – cross-entropy only ever makes sense on the final output, against a one-hot/class-probability target). OFF by default (plain Gaussian energy everywhere, matching DirectKPPCNetwork).

◆ SetUseIPC()

void Deep::FullPCNetwork::SetUseIPC ( bool enabled)
inlinenoexcept

Enables iPC: UpdateWeights() runs every settling step instead of once after settling completes. OFF by default – TrainStep() matches DirectKPPCNetwork's standard, two-phase behavior exactly when this is false.

See also
Salvatori et al., "Incremental Predictive Coding", https://arxiv.org/abs/2212.00720

◆ SetUseMomentum()

void Deep::FullPCNetwork::SetUseMomentum ( bool enabled,
float beta = 0.9f )
inlinenoexcept

Enables momentum (inertial) settling on every layer. Loops over layers and calls each one's own SetMomentum() – same pattern as SetLearningRate/SetFeedbackRate. OFF by default.

◆ SetUseMuPCScaling()

void Deep::FullPCNetwork::SetUseMuPCScaling ( bool enabled)
inlinenoexcept

Enables muPC-style per-layer forward scaling (Table 1 of the muPC paper). OFF by default. Must be called before Compile() – Compile() is what actually computes and applies each layer's a, once the full architecture (every AddLayer call) is known.

◆ SetUseResidualConnections()

void Deep::FullPCNetwork::SetUseResidualConnections ( bool enabled)
inlinenoexcept

Enables residual/skip connections on middle hidden layers (muPC's "1-skip" ResNet formulation – see that paper's A.2.4). OFF by default. Requires every middle hidden layer to have the SAME width as its neighbor; Compile() throws std::invalid_argument if that's violated, rather than silently skipping the mismatched connection (a silent skip risks masking a real configuration mistake).

◆ Step()

float Deep::FullPCNetwork::Step ( bool computeEnergy = true)
noexcept

Runs one settling step on every layer.

Parameters
computeEnergyasks for energy to be returned
Returns
Total energy, summed from every layer's CalculateState().

◆ TrainStep()

float Deep::FullPCNetwork::TrainStep ( const std::vector< float > & x,
const std::vector< float > & y,
int inferenceSteps = 1 )

Full train step: reset, clamp input+target, run all four DKP-PC phases in order, unclamp.

Parameters
xFlattened input batch, clamped to the input layer.
yFlattened target batch, clamped to the terminal layer.
inferenceStepsNumber of settling steps for phase 2.
Returns
The final settling step's total energy, before weight updates.

◆ UpdateWeights()

void Deep::FullPCNetwork::UpdateWeights ( )
noexcept

Applies weight updates to every layer.


The documentation for this class was generated from the following files: