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

An abstracted class for an array of SimplePCLayer. More...

#include <SimplePCNetwork.h>

Public Member Functions

 SimplePCNetwork (int batchSize, DeviceType device=DeviceType::DEVICE_CPU) noexcept
 Constructs an empty network with a predetermined batch size.
 SimplePCNetwork (const SimplePCNetwork &)=delete
SimplePCNetwork & operator= (const SimplePCNetwork &)=delete
 ~SimplePCNetwork ()=default
void AddLayer (int size, int nextSize, float lr, float ir, float lmbda, void(*act)(float *, size_t), void(*dAct)(float *, size_t, bool))
 Adds a layer to the network.
void AddLayer (int size, int nextSize, float lr, float ir, float lmbda, ActivationType aType, ActivationType dType)
 Same as the function-pointer overload above, taking an ActivationType pair instead – the enum-based dispatch used by callers (e.g. the Python bindings) that name an activation by type rather than linking against its concrete function.
void RandomizeWeights (std::mt19937 &rng, const char *distribution)
 Randomizes the weights of each layer.
void RandomizeWeights (std::mt19937 &rng)
 Randomizes the weights 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, necessary for prediction.
float CalculateState (bool needEnergy=true)
 Calculates the state of each layer.
void UpdateState ()
 Updates each layer's state.
void UpdateWeights ()
 Updates each layer's weights.
SimplePCLayer * GetTerminalLayer () noexcept
 Returns the network's terminal (final) layer.
std::vector< std::unique_ptr< SimplePCLayer > > & GetLayers () noexcept
 Returns every layer in the network, in the order they were added.
const std::vector< std::unique_ptr< SimplePCLayer > > & 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.
void SetOptimizer (OptimizerType o) noexcept
 Sets the optimizer used for weight updates on every layer.
float TrainStep (const std::vector< float > &x, const std::vector< float > &y, int inferenceSteps)
 Runs a complete training step (clamp, settle, update, unclamp).
std::vector< float > Predict (const std::vector< float > &x, int inferenceSteps)
 Runs a forward prediction pass (clamp, settle, read).
void ProjectForward () noexcept
 Runs a single, non-iterative forward pass through current weights, seeding each hidden layer's z from the PREVIOUS layer's mu – giving the settling loop a genuine, current-weights-based starting point instead of zero-init.
float TrainStepWithProjection (const std::vector< float > &x, const std::vector< float > &y, int inferenceSteps, bool computeEnergy=true)
 Full train step WITH forward-projection initialization, all in ONE call – reset, clamp, project, settle, update weights, unclamp. Matches TrainStep()'s signature/return convention exactly, just with ProjectForward() inserted between clamping the input and clamping the target.
std::vector< float > PredictWithProjection (const std::vector< float > &x, int inferenceSteps)
 Inference-only counterpart to TrainStepWithProjection(): reset, clamp input, project forward, settle, read out the terminal layer's beliefs. No target clamp, no weight update.
void SetMuCacheThreshold (float threshold) noexcept
 Sets mu-cache threshold on every layer – see SimplePCLayer::SetMuCacheThreshold() for semantics. Safe to call any time after Compile().
void Compile ()
 Loads all layers into one contiguous block of memory (MemoryArena for DEVICE_CPU, DeviceMemoryArena for DEVICE_GPU).

Detailed Description

An abstracted class for an array of SimplePCLayer.

Constructor & Destructor Documentation

◆ SimplePCNetwork() [1/2]

Deep::SimplePCNetwork::SimplePCNetwork ( 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.

◆ SimplePCNetwork() [2/2]

Deep::SimplePCNetwork::SimplePCNetwork ( const SimplePCNetwork & )
delete

◆ ~SimplePCNetwork()

Deep::SimplePCNetwork::~SimplePCNetwork ( )
default

Member Function Documentation

◆ AddLayer() [1/2]

void Deep::SimplePCNetwork::AddLayer ( int size,
int nextSize,
float lr,
float ir,
float lmbda,
ActivationType aType,
ActivationType dType )

Same as the function-pointer overload above, taking an ActivationType pair instead – the enum-based dispatch used by callers (e.g. the Python bindings) that name an activation by type rather than linking against its concrete function.

Parameters
sizeinput size
nextSizeoutput size
lrlearning rate for beliefs
irlearning rate for weights
lmbdaweight decay (L2 regularization) coefficient
aTypeactivation type
dTypeactivation derivative type

◆ AddLayer() [2/2]

void Deep::SimplePCNetwork::AddLayer ( int size,
int nextSize,
float lr,
float ir,
float lmbda,
void(* act )(float *, size_t),
void(* dAct )(float *, size_t, bool) )

Adds a layer to the network.

Parameters
sizeinput size
nextSizeoutput size
lrlearning rate for beliefs
irlearning rate for weights
lmbdaweight decay (L2 regularization) coefficient
actactivation function
dActderivative of previous activation function

◆ CalculateState()

float Deep::SimplePCNetwork::CalculateState ( bool needEnergy = true)

Calculates the state of each layer.

Parameters
needEnergyask for energy after
Returns
Returns total energy if asked for

◆ Clamp()

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

Clamps the input to the first layer, necessary for prediction.

Parameters
inputreference to input vector

◆ Compile()

void Deep::SimplePCNetwork::Compile ( )

Loads all layers into one contiguous block of memory (MemoryArena for DEVICE_CPU, DeviceMemoryArena for DEVICE_GPU).

◆ GetBatchSize()

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

Returns the batch size for the network's layers.

Returns
size_t batchSize

◆ GetDevice()

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

Returns which device this network's layers run on.

◆ GetLayers() [1/2]

const std::vector< std::unique_ptr< SimplePCLayer > > & Deep::SimplePCNetwork::GetLayers ( ) const
inlinenoexcept

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

Returns
A const reference to the internal layer list.

◆ GetLayers() [2/2]

std::vector< std::unique_ptr< SimplePCLayer > > & Deep::SimplePCNetwork::GetLayers ( )
inlinenoexcept

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

Returns
A reference to the internal layer list.

◆ GetTerminalLayer()

SimplePCLayer * Deep::SimplePCNetwork::GetTerminalLayer ( )
inlinenoexcept

Returns the network's terminal (final) layer.

Returns
Pointer to the last layer added via AddLayer().

◆ operator=()

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

◆ Predict()

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

Runs a forward prediction pass (clamp, settle, read).

Parameters
xThe batched input data
inferenceStepsThe number of relaxation iterations
Returns
A vector containing the batched predictions

◆ PredictWithProjection()

std::vector< float > Deep::SimplePCNetwork::PredictWithProjection ( const std::vector< float > & x,
int inferenceSteps )

Inference-only counterpart to TrainStepWithProjection(): reset, clamp input, project forward, settle, read out the terminal layer's beliefs. No target clamp, no weight update.

Parameters
xFlattened input batch.
inferenceStepsNumber of settling steps.
Returns
The terminal layer's beliefs after settling.

◆ ProjectForward()

void Deep::SimplePCNetwork::ProjectForward ( )
noexcept

Runs a single, non-iterative forward pass through current weights, seeding each hidden layer's z from the PREVIOUS layer's mu – giving the settling loop a genuine, current-weights-based starting point instead of zero-init.

Reuses each layer's EXISTING CalculateState() (already-verified forward computation, mu = f(W@z+b)) as a side effect – no new math, just a new sequence of existing calls. Assumes the input layer (layers[0]) is ALREADY clamped before this is called.

Warning
Layer 0's error (e) and this call's energy return value are meaningless here – CalculateState() computes both mu (what we want) and e/energy (a side effect we're discarding, since the NEXT layer's z hasn't been set to a meaningful value yet at the point each layer's CalculateState() runs). Only mu is used.

◆ RandomizeWeights() [1/2]

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

Randomizes the weights of each layer.

Parameters
rngThe classic Mersenne Twister

◆ RandomizeWeights() [2/2]

void Deep::SimplePCNetwork::RandomizeWeights ( std::mt19937 & rng,
const char * distribution )

Randomizes the weights of each layer.

Parameters
rngThe classic Mersenne Twister
distributionA string representation of (normal, uniform) distribution. For example: "normal(0, 1)" for a standard normal.

◆ ResetState()

void Deep::SimplePCNetwork::ResetState ( )
noexcept

Resets each layer's state without touching learned weights.

◆ SetMuCacheThreshold()

void Deep::SimplePCNetwork::SetMuCacheThreshold ( float threshold)
noexcept

Sets mu-cache threshold on every layer – see SimplePCLayer::SetMuCacheThreshold() for semantics. Safe to call any time after Compile().

◆ SetOptimizer()

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

Sets the optimizer used for weight updates on every layer.

Parameters
oThe optimizer type to apply.

◆ TrainStep()

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

Runs a complete training step (clamp, settle, update, unclamp).

Parameters
xThe batched input data
yThe batched target data
inferenceStepsThe number of relaxation iterations
Returns
The final energy state of the network before weight updates

◆ TrainStepWithProjection()

float Deep::SimplePCNetwork::TrainStepWithProjection ( const std::vector< float > & x,
const std::vector< float > & y,
int inferenceSteps,
bool computeEnergy = true )

Full train step WITH forward-projection initialization, all in ONE call – reset, clamp, project, settle, update weights, unclamp. Matches TrainStep()'s signature/return convention exactly, just with ProjectForward() inserted between clamping the input and clamping the target.

Exists specifically to eliminate the Python/pybind boundary- crossing overhead of doing this same sequence via many separate calls from Python (reset_state, clamp_input, project_forward, clamp_state, then STEPS*2 individual calculate_state/update_state calls, update_weights, unclamp_state – over 40 individual crossings per batch at STEPS=20). This does the whole sequence in ONE crossing instead.

◆ UpdateState()

void Deep::SimplePCNetwork::UpdateState ( )

Updates each layer's state.

◆ UpdateWeights()

void Deep::SimplePCNetwork::UpdateWeights ( )

Updates each layer's weights.


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