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

Predictive Coding network built from DirectKPPCLayer, using Direct Kolen-Pollack feedback alignment (DFA) instead of symmetric weight transport. See the file-level warning above on its current verification status. More...

#include <DirectKPPCNetwork.h>

Public Member Functions

 DirectKPPCNetwork (int batchSize, DeviceType device=DeviceType::DEVICE_CPU) noexcept
 Constructs an empty network with a predetermined batch size.
 ~DirectKPPCNetwork ()=default
 Default destructor.
 DirectKPPCNetwork (const DirectKPPCNetwork &)=delete
 Copy construction is disabled; a network owns its layers' memory arena and is not meant to be duplicated.
DirectKPPCNetwork & operator= (const DirectKPPCNetwork &)=delete
 Copy assignment is disabled; see the copy constructor.
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 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 every hidden layer's z from a genuine forward pass through current weights. Assumes layers[0] is already clamped.
float CalculateTerminalError () noexcept
 Phase 0's final step: computes the terminal layer's error alone, producing a real, non-zero epsilon_L. Call AFTER clamping the target onto the terminal layer, and BEFORE DirectFeedbackUpdate().
void DirectFeedbackUpdate () noexcept
 Phase 1: runs DirectFeedbackUpdate() on every non-terminal layer. Order among layers does not matter (see class-level docs) – unlike GaussSeidelPCNetwork, no sweep ordering is required here. Still sequential in this revision; see class-level warning.
float Step (bool computeEnergy=true) noexcept
 Phase 2: runs one ordinary PC settling step (CalculateState() + UpdateState()) across every layer.
void UpdateWeights () noexcept
 Phase 3: updates every non-terminal layer's W and Psi. Called once after the settling loop completes.
void SetOptimizer (OptimizerType o) noexcept
 Sets the optimizer used for W updates, on every layer.
void SetPsiOptimizer (OptimizerType o) noexcept
 Sets the optimizer used for Psi updates, on every layer.
void SetLearningRate (float lr) noexcept
 Sets the learning rate used for W updates, on every layer.
void SetFeedbackRate (float fl) noexcept
 Sets the learning rate used for Psi updates, on every layer.
DirectKPPCLayer * GetTerminalLayer () noexcept
 Returns the network's terminal (final) layer.
std::vector< std::unique_ptr< DirectKPPCLayer > > & GetLayers () noexcept
 Returns every layer in the network, in the order they were added.
const std::vector< std::unique_ptr< DirectKPPCLayer > > & GetLayers () const noexcept
 Returns every layer in the network, in the order they were added.
int GetBatchSize () const noexcept
 Returns the batch size for the network's layers.
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 (MemoryArena for DEVICE_CPU, DeviceMemoryArena for DEVICE_GPU), and wires layerAbove/layerBelow/terminalLayer across every layer.

Detailed Description

Predictive Coding network built from DirectKPPCLayer, using Direct Kolen-Pollack feedback alignment (DFA) instead of symmetric weight transport. See the file-level warning above on its current verification status.

Constructor & Destructor Documentation

◆ DirectKPPCNetwork() [1/2]

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

Constructs an empty network with a predetermined batch size.

Parameters
batchSizeBatch size
deviceWhich device this network's layers should run on. Defaults to DEVICE_CPU, preserving existing behavior exactly for anyone not explicitly requesting DEVICE_GPU.

◆ ~DirectKPPCNetwork()

Deep::DirectKPPCNetwork::~DirectKPPCNetwork ( )
default

Default destructor.

◆ DirectKPPCNetwork() [2/2]

Deep::DirectKPPCNetwork::DirectKPPCNetwork ( const DirectKPPCNetwork & )
delete

Copy construction is disabled; a network owns its layers' memory arena and is not meant to be duplicated.

Member Function Documentation

◆ AddLayer()

void Deep::DirectKPPCNetwork::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 Layer implementatio)
lmbdaweight decay (L2 regularization) coefficient, shared between W and Psi
aTypeactivation type
dTypeactivation derivative type

◆ CalculateTerminalError()

float Deep::DirectKPPCNetwork::CalculateTerminalError ( )
noexcept

Phase 0's final step: computes the terminal layer's error alone, producing a real, non-zero epsilon_L. Call AFTER clamping the target onto the terminal layer, and BEFORE DirectFeedbackUpdate().

Returns
The terminal layer's energy contribution.

◆ Clamp()

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

Clamps the input to the first layer.

Parameters
inputReference to the input vector.

◆ Compile()

void Deep::DirectKPPCNetwork::Compile ( )

Loads all layers into one contiguous block of memory (MemoryArena for DEVICE_CPU, DeviceMemoryArena for DEVICE_GPU), and wires layerAbove/layerBelow/terminalLayer across every layer.

◆ DirectFeedbackUpdate()

void Deep::DirectKPPCNetwork::DirectFeedbackUpdate ( )
noexcept

Phase 1: runs DirectFeedbackUpdate() on every non-terminal layer. Order among layers does not matter (see class-level docs) – unlike GaussSeidelPCNetwork, no sweep ordering is required here. Still sequential in this revision; see class-level warning.

◆ GetBatchSize()

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

Returns the batch size for the network's layers.

◆ GetDevice()

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

Returns which device this network's layers run on.

◆ GetLayers() [1/2]

const std::vector< std::unique_ptr< DirectKPPCLayer > > & Deep::DirectKPPCNetwork::GetLayers ( ) const
inlinenoexcept

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

◆ GetLayers() [2/2]

std::vector< std::unique_ptr< DirectKPPCLayer > > & Deep::DirectKPPCNetwork::GetLayers ( )
inlinenoexcept

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

◆ GetTerminalLayer()

DirectKPPCLayer * Deep::DirectKPPCNetwork::GetTerminalLayer ( )
inlinenoexcept

Returns the network's terminal (final) layer.

◆ operator=()

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

Copy assignment is disabled; see the copy constructor.

◆ Predict()

std::vector< float > Deep::DirectKPPCNetwork::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::DirectKPPCNetwork::ProjectForward ( )
noexcept

Seeds every hidden layer's z from a genuine forward pass through current weights. Assumes layers[0] is already clamped.

◆ RandomizeWeights()

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

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

Parameters
rngThe classic Mersenne Twister

◆ ResetState()

void Deep::DirectKPPCNetwork::ResetState ( )
noexcept

Resets each layer's state without touching learned weights.

◆ SetFeedbackRate()

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

Sets the learning rate used for Psi updates, on every layer.

◆ SetLearningRate()

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

Sets the learning rate used for W updates, on every layer.

◆ SetOptimizer()

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

Sets the optimizer used for W updates, on every layer.

◆ SetPsiOptimizer()

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

Sets the optimizer used for Psi updates, on every layer.

◆ Step()

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

Phase 2: runs one ordinary PC settling step (CalculateState() + UpdateState()) across every layer.

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

◆ TrainStep()

float Deep::DirectKPPCNetwork::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. Defaults to 1, matching the paper's own headline result – unlike this codebase's other PC variants, DKP-PC's entire point is that a single step is enough BECAUSE of the DFA phase, so defaulting to a larger value here would silently waste most of the intended speed advantage.
Returns
The final settling step's total energy, before weight updates.

◆ UpdateWeights()

void Deep::DirectKPPCNetwork::UpdateWeights ( )
noexcept

Phase 3: updates every non-terminal layer's W and Psi. Called once after the settling loop completes.


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