Deepity
Ultra-fast, SIMD-accelerated Predictive Coding
Loading...
Searching...
No Matches
GaussSeidelPCNetwork.h
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include <memory>
4#include <random>
8
32
33namespace Deep
34{
40 {
41 public:
44 explicit GaussSeidelPCNetwork(int batchSize) noexcept;
47
53
62 void AddLayer(int size, int nextSize, float lr, float ir, float lmbda,
63 void (*act)(float *, size_t), void (*dAct)(float *, size_t, bool));
73 void AddLayer(int size, int nextSize, float lr, float ir, float lmbda,
74 ActivationType aType, ActivationType dType);
75
78 void RandomizeWeights(std::mt19937 &rng);
80 void ResetState() noexcept;
83 void Clamp(const std::vector<float> &input);
84
90 float Step() noexcept;
91
95 void UpdateWeights() noexcept;
96
102 void ProjectForward() noexcept;
103
107 {
108 for (auto &layer : layers)
109 layer->SetOptimizer(o);
110 }
111
115 void SetLearningRate(float lr) noexcept
116 {
117 for (auto &layer : layers)
118 layer->SetLearningRate(lr);
119 }
120
123 GaussSeidelPCLayer *GetTerminalLayer() noexcept { return layers.back().get(); }
126 std::vector<std::unique_ptr<GaussSeidelPCLayer>> &GetLayers() noexcept { return layers; }
129 const std::vector<std::unique_ptr<GaussSeidelPCLayer>> &GetLayers() const noexcept { return layers; }
132 int GetBatchSize() const noexcept { return batchSize; }
133
137 float TrainStep(const std::vector<float> &x, const std::vector<float> &y, int inferenceSteps);
138
141 float TrainStepWithProjection(const std::vector<float> &x, const std::vector<float> &y, int inferenceSteps);
142
145 std::vector<float> Predict(const std::vector<float> &x, int inferenceSteps);
146
148 void Compile();
149
150 private:
152 std::vector<std::unique_ptr<GaussSeidelPCLayer>> layers;
154 std::unique_ptr<MemoryArena> arena;
156 int batchSize;
157 };
158}
Defines the activation functions of a predictive coding model.
A PC layer whose settling dynamics follow a Gauss-Seidel (sequential-sweep) update,...
A 64-byte-aligned bump-pointer allocator over a single heap-allocated block, used to pack every layer...
PC layer with Gauss-Seidel (sequential-sweep) settling dynamics, in place of the usual simultaneous u...
Definition GaussSeidelPCLayer.h:85
void ResetState() noexcept
Resets each layer's state without touching learned weights.
Definition GaussSeidelPCNetwork.cpp:40
float TrainStepWithProjection(const std::vector< float > &x, const std::vector< float > &y, int inferenceSteps)
Same as TrainStep(), but with ProjectForward() called once after clamping the input,...
Definition GaussSeidelPCNetwork.cpp:113
GaussSeidelPCNetwork(int batchSize) noexcept
Constructs an empty network with a predetermined batch size.
Definition GaussSeidelPCNetwork.cpp:6
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.
Definition GaussSeidelPCNetwork.cpp:8
void UpdateWeights() noexcept
Updates every non-terminal layer's weights. Called ONCE after the full settling loop completes,...
Definition GaussSeidelPCNetwork.cpp:77
const std::vector< std::unique_ptr< GaussSeidelPCLayer > > & GetLayers() const noexcept
Returns every layer in the network, in the order they were added.
Definition GaussSeidelPCNetwork.h:129
GaussSeidelPCLayer * GetTerminalLayer() noexcept
Returns the network's terminal (final) layer.
Definition GaussSeidelPCNetwork.h:123
void SetOptimizer(OptimizerType o) noexcept
Sets the optimizer used for weight updates, on every layer.
Definition GaussSeidelPCNetwork.h:106
void ProjectForward() noexcept
Seeds every hidden layer's z from a genuine forward pass through current weights (ComputePrediction()...
Definition GaussSeidelPCNetwork.cpp:83
std::vector< float > Predict(const std::vector< float > &x, int inferenceSteps)
Forward prediction: clamp input, settle (no target clamped), read the terminal's beliefs.
Definition GaussSeidelPCNetwork.cpp:140
float Step() noexcept
Runs ONE full Gauss-Seidel settling step (all three sweeps, in order) across every layer.
Definition GaussSeidelPCNetwork.cpp:51
void SetLearningRate(float lr) noexcept
Sets the learning rate used for weight updates, on every layer.
Definition GaussSeidelPCNetwork.h:115
~GaussSeidelPCNetwork()=default
Default destructor.
void Clamp(const std::vector< float > &input)
Clamps the input to the first layer.
Definition GaussSeidelPCNetwork.cpp:46
std::vector< std::unique_ptr< GaussSeidelPCLayer > > & GetLayers() noexcept
Returns every layer in the network, in the order they were added.
Definition GaussSeidelPCNetwork.h:126
float TrainStep(const std::vector< float > &x, const std::vector< float > &y, int inferenceSteps)
Full train step: reset, clamp input+target, settle for inferenceSteps, update weights once,...
Definition GaussSeidelPCNetwork.cpp:97
void Compile()
Loads all layers into one contiguous block of memory.
Definition GaussSeidelPCNetwork.cpp:162
GaussSeidelPCNetwork & operator=(const GaussSeidelPCNetwork &)=delete
Copy assignment is disabled; see the copy constructor.
int GetBatchSize() const noexcept
Returns the batch size for the network's layers.
Definition GaussSeidelPCNetwork.h:132
GaussSeidelPCNetwork(const GaussSeidelPCNetwork &)=delete
Copy construction is disabled; a network owns its layers' memory arena and is not meant to be duplica...
void RandomizeWeights(std::mt19937 &rng)
Randomizes the weights of each layer.
Definition GaussSeidelPCNetwork.cpp:34
This file exists to provide both Tensor.h and IComputeBackend.h with device types without introducing...
Definition Backend.cpp:10
ActivationType
Identifies an activation function or its derivative, for dispatch tables and layer/backend APIs that ...
Definition ActivationType.h:25
OptimizerType
Selects which weight-update rule a layer applies during UpdateWeights().
Definition AdamOptimizer.h:52