![]() |
Deepity
Ultra-fast, SIMD-accelerated Predictive Coding
|
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). | |
An abstracted class for an array of SimplePCLayer.
|
explicitnoexcept |
Constructs an empty network with a predetermined batch size.
| batchSize | Batch size |
| device | Which device this network's layers should run on. Defaults to DEVICE_CPU, preserving existing behavior exactly for anyone not explicitly requesting DEVICE_GPU. |
|
delete |
|
default |
| 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.
| size | input size |
| nextSize | output size |
| lr | learning rate for beliefs |
| ir | learning rate for weights |
| lmbda | weight decay (L2 regularization) coefficient |
| aType | activation type |
| dType | activation derivative type |
| 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.
| size | input size |
| nextSize | output size |
| lr | learning rate for beliefs |
| ir | learning rate for weights |
| lmbda | weight decay (L2 regularization) coefficient |
| act | activation function |
| dAct | derivative of previous activation function |
| float Deep::SimplePCNetwork::CalculateState | ( | bool | needEnergy = true | ) |
Calculates the state of each layer.
| needEnergy | ask for energy after |
| void Deep::SimplePCNetwork::Clamp | ( | const std::vector< float > & | input | ) |
Clamps the input to the first layer, necessary for prediction.
| input | reference to input vector |
| void Deep::SimplePCNetwork::Compile | ( | ) |
Loads all layers into one contiguous block of memory (MemoryArena for DEVICE_CPU, DeviceMemoryArena for DEVICE_GPU).
|
inlinenoexcept |
Returns the batch size for the network's layers.
|
inlinenoexcept |
Returns which device this network's layers run on.
|
inlinenoexcept |
Returns every layer in the network, in the order they were added.
|
inlinenoexcept |
Returns every layer in the network, in the order they were added.
|
inlinenoexcept |
Returns the network's terminal (final) layer.
|
delete |
| std::vector< float > Deep::SimplePCNetwork::Predict | ( | const std::vector< float > & | x, |
| int | inferenceSteps ) |
Runs a forward prediction pass (clamp, settle, read).
| x | The batched input data |
| inferenceSteps | The number of relaxation iterations |
| 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.
| x | Flattened input batch. |
| inferenceSteps | Number of settling steps. |
|
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.
| void Deep::SimplePCNetwork::RandomizeWeights | ( | std::mt19937 & | rng | ) |
Randomizes the weights of each layer.
| rng | The classic Mersenne Twister |
| void Deep::SimplePCNetwork::RandomizeWeights | ( | std::mt19937 & | rng, |
| const char * | distribution ) |
Randomizes the weights of each layer.
| rng | The classic Mersenne Twister |
| distribution | A string representation of (normal, uniform) distribution. For example: "normal(0, 1)" for a standard normal. |
|
noexcept |
Resets each layer's state without touching learned weights.
|
noexcept |
Sets mu-cache threshold on every layer – see SimplePCLayer::SetMuCacheThreshold() for semantics. Safe to call any time after Compile().
|
inlinenoexcept |
Sets the optimizer used for weight updates on every layer.
| o | The optimizer type to apply. |
| 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).
| x | The batched input data |
| y | The batched target data |
| inferenceSteps | The number of relaxation iterations |
| 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.
| void Deep::SimplePCNetwork::UpdateState | ( | ) |
Updates each layer's state.
| void Deep::SimplePCNetwork::UpdateWeights | ( | ) |
Updates each layer's weights.