Deepity
Ultra-fast, SIMD-accelerated Predictive Coding
Loading...
Searching...
No Matches
GaussSeidelPCLayer.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <stdexcept>
5#include <random>
6#include <memory>
7#include <cstdlib>
12
76
77namespace Deep
78{
79 class GaussSeidelPCNDiagnostics;
80
85 {
86 public:
97 GaussSeidelPCLayer(int size, int nextSize, int batchSize = 1,
98 float learningRate = 1e-6f, float inferenceRate = 0.1f, float lmbda = 1e-2f,
99 void (*act)(float *, size_t) = relu,
100 void (*dAct)(float *, size_t, bool) = dRelu);
101
113 GaussSeidelPCLayer(int size, int nextSize, int batchSize = 1,
114 float learningRate = 1e-6f, float inferenceRate = 0.1f, float lmbda = 1e-2f,
116
123 float CalculateState() noexcept override
124 {
125 UpdateState();
126 return 0.0f;
127 }
128
131 void UpdateState() noexcept override;
132
137 void ComputePrediction() noexcept;
138
144 float ComputeError() noexcept;
145
149 void UpdateWeights() noexcept override;
150
152 void Flush() noexcept override {}
153
156 void ClampState(const std::vector<float> &inputData) noexcept;
158 void UnclampState() noexcept;
159
162 float *GetBeliefs() noexcept override { return z; }
165 const float *GetMu() const noexcept { return mu; }
168 const float *GetErrors() const noexcept override { return e; }
171 size_t GetInputSize() const noexcept override { return size; }
175 size_t GetOutputSize() const noexcept override { return nextSize; }
178 size_t GetBatchSize() const noexcept override { return batchSize; }
179
182 const float *GetWeights() const noexcept { return W; }
185 float *GetWeights() noexcept { return W; }
188 const float *GetBiases() const noexcept { return b; }
191 float *GetBiases() noexcept { return b; }
192
195 float GetLearningRate() const noexcept { return lr; }
198 float GetInferenceRate() const noexcept { return ir; }
201 float GetLambda() const noexcept { return lmbda; }
202
205 void SetLearningRate(float lr) noexcept { this->lr = lr; }
208 void SetInferenceRate(float ir) noexcept { this->ir = ir; }
211 void SetLambda(float l) noexcept { this->lmbda = l; }
214 void SetOptimizer(const OptimizerType o) noexcept { opt = o; }
215
219 void SetLayerAbove(GaussSeidelPCLayer *above) noexcept { layerAbove = above; }
223 void SetLayerBelow(GaussSeidelPCLayer *below) noexcept { layerBelow = below; }
224
227 void ResetState() noexcept;
228
233 const GaussSeidelPCLayer &GetLayerAbove() const noexcept { return *layerAbove; }
238 const GaussSeidelPCLayer &GetLayerBelow() const noexcept { return *layerBelow; }
239
243 void RandomizeWeights(std::mt19937 &twister) noexcept;
244
247 ActivationType GetActivationType() const noexcept { return To_AType(activation); }
250 ActivationType GetDerivativeType() const noexcept { return To_AType(activationDerivative); }
251
255 size_t GetRequiredFloats() const noexcept;
259 void BindMemory(MemoryArena &arena);
260
261 private:
263 std::unique_ptr<MemoryArena> localArena;
265 float *W;
267 float *b;
269 float *e;
271 float *z;
272
274 int batchSize;
275
276 float *mu; // this layer's OWN outgoing prediction -- stays
277 // CLEAN/activated at all times; never mutated
278 // into a derivative in place
279 float *muDeriv; // scratch buffer for mu's derivative, computed
280 // fresh whenever needed (UpdateState(),
281 // UpdateWeights()) -- copy mu here, derive in
282 // place, leaving the real mu untouched
284 float *dz_dt;
287 float *z_deriv;
289 float *bottom_up;
290 float *E; // feedback-alignment matrix -- SEPARATE from W, same
291 // shape, randomly initialized once, NEVER updated.
292 // Matches ngc-learn's REAL wiring: e2.dmu >> E2.inputs;
293 // E2.outputs >> z1.j, where E2 is an independently
294 // initialized StaticSynapse -- NOT W transposed. This
295 // is feedback alignment (Lillicrap et al.), not
296 // backprop-style transposed-weight feedback.
297
299 float lr;
301 float ir;
303 float lmbda;
305 bool isClamped = false;
306
308 GaussSeidelPCLayer *layerAbove;
310 GaussSeidelPCLayer *layerBelow;
312 ActivationFn activation;
314 DerivativeFn activationDerivative;
316 ActivationType activationType;
319
321 float *grad_W = nullptr;
323 float *grad_b = nullptr;
325 float *m_W = nullptr;
327 float *v_W = nullptr;
329 float *m_b = nullptr;
331 float *v_b = nullptr;
333 int t = 0;
334
336 };
337
338} // namespace Deep
Defines the activation functions of a predictive coding model.
Vectorized AdamW update, operating per-element over an array of weights (or biases) – NOT the single-...
Virtual Layer.
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
size_t GetOutputSize() const noexcept override
Returns this layer's outgoing prediction size (0 for a terminal layer).
Definition GaussSeidelPCLayer.h:175
void RandomizeWeights(std::mt19937 &twister) noexcept
Randomizes this layer's weights (and the E feedback-alignment matrix) in place.
Definition GaussSeidelPCLayer.cpp:250
float * GetBiases() noexcept
Returns a mutable version of the stored biases.
Definition GaussSeidelPCLayer.h:191
float * GetWeights() noexcept
Returns a mutable version of the stored weights.
Definition GaussSeidelPCLayer.h:185
float * GetBeliefs() noexcept override
Returns this layer's belief buffer.
Definition GaussSeidelPCLayer.h:162
void SetOptimizer(const OptimizerType o) noexcept
Selects the optimizer used for weight updates.
Definition GaussSeidelPCLayer.h:214
const float * GetWeights() const noexcept
Returns a read-only version of the stored weights.
Definition GaussSeidelPCLayer.h:182
const float * GetBiases() const noexcept
Returns a read-only version of the stored biases.
Definition GaussSeidelPCLayer.h:188
float CalculateState() noexcept override
Step 1 of a Gauss-Seidel timestep: updates z using mu/e_above HELD OVER from the end of the previous ...
Definition GaussSeidelPCLayer.h:123
GaussSeidelPCLayer(int size, int nextSize, int batchSize=1, float learningRate=1e-6f, float inferenceRate=0.1f, float lmbda=1e-2f, void(*act)(float *, size_t)=relu, void(*dAct)(float *, size_t, bool)=dRelu)
Constructor for a GaussSeidelPCLayer.
Definition GaussSeidelPCLayer.cpp:14
void SetInferenceRate(float ir) noexcept
Sets the inference rate (Euler integration step size).
Definition GaussSeidelPCLayer.h:208
float GetLearningRate() const noexcept
Returns the learning rate used for weight updates.
Definition GaussSeidelPCLayer.h:195
ActivationType GetDerivativeType() const noexcept
Returns this layer's configured activation-derivative type.
Definition GaussSeidelPCLayer.h:250
void ComputePrediction() noexcept
Step 2 of a Gauss-Seidel timestep: recomputes mu fresh, using the z JUST updated by UpdateState() in ...
Definition GaussSeidelPCLayer.cpp:117
void ClampState(const std::vector< float > &inputData) noexcept
Clamps this layer's beliefs to externally-provided data.
Definition GaussSeidelPCLayer.cpp:41
const GaussSeidelPCLayer & GetLayerBelow() const noexcept
Returns the layer immediately below this one.
Definition GaussSeidelPCLayer.h:238
void ResetState() noexcept
Resets beliefs/errors/predictions back to their initial values, without touching learned weights.
Definition GaussSeidelPCLayer.cpp:53
const float * GetMu() const noexcept
Returns this layer's outgoing prediction buffer.
Definition GaussSeidelPCLayer.h:165
friend class GaussSeidelPCNDiagnostics
Definition GaussSeidelPCLayer.h:335
void UpdateWeights() noexcept override
Computes weight updates via gradient descent, with L2 weight decay. Called once after the full settli...
Definition GaussSeidelPCLayer.cpp:185
const float * GetErrors() const noexcept override
Returns this layer's prediction-error buffer.
Definition GaussSeidelPCLayer.h:168
ActivationType GetActivationType() const noexcept
Returns this layer's configured activation type.
Definition GaussSeidelPCLayer.h:247
const GaussSeidelPCLayer & GetLayerAbove() const noexcept
Returns the layer immediately above this one.
Definition GaussSeidelPCLayer.h:233
void Flush() noexcept override
No-op; exists for Layer interface conformance.
Definition GaussSeidelPCLayer.h:152
float GetInferenceRate() const noexcept
Returns the inference rate (Euler integration step size).
Definition GaussSeidelPCLayer.h:198
void SetLambda(float l) noexcept
Sets the weight-decay (L2 regularization) coefficient.
Definition GaussSeidelPCLayer.h:211
void SetLayerBelow(GaussSeidelPCLayer *below) noexcept
Sets the layer immediately below this one in the network.
Definition GaussSeidelPCLayer.h:223
void BindMemory(MemoryArena &arena)
Binds this layer's weight/state/scratch buffers into the supplied arena. Must be called before any ot...
Definition GaussSeidelPCLayer.cpp:324
size_t GetInputSize() const noexcept override
Returns this layer's own belief size.
Definition GaussSeidelPCLayer.h:171
size_t GetRequiredFloats() const noexcept
Computes the total number of floats this layer requires from a MemoryArena.
Definition GaussSeidelPCLayer.cpp:294
float ComputeError() noexcept
Step 3 of a Gauss-Seidel timestep: recomputes e fresh, using this layer's own (just-updated) z as the...
Definition GaussSeidelPCLayer.cpp:160
void SetLearningRate(float lr) noexcept
Sets the learning rate used for weight updates.
Definition GaussSeidelPCLayer.h:205
float GetLambda() const noexcept
Returns the weight-decay (L2 regularization) coefficient.
Definition GaussSeidelPCLayer.h:201
void UpdateState() noexcept override
Step 1: the actual z-update. See class-level docs for the required three-sweep calling order.
Definition GaussSeidelPCLayer.cpp:67
void UnclampState() noexcept
Releases a previous ClampState() call.
Definition GaussSeidelPCLayer.cpp:48
size_t GetBatchSize() const noexcept override
Returns the batch size this layer was constructed with.
Definition GaussSeidelPCLayer.h:178
void SetLayerAbove(GaussSeidelPCLayer *above) noexcept
Sets the layer immediately above this one in the network.
Definition GaussSeidelPCLayer.h:219
A deepity layer virtual class.
Definition Layer.h:40
size_t nextSize
Size of output.
Definition Layer.h:76
size_t size
Size of input.
Definition Layer.h:74
A 64-byte-aligned bump-pointer allocator over a single contiguous block of host memory....
Definition MemoryArena.h:141
This file exists to provide both Tensor.h and IComputeBackend.h with device types without introducing...
Definition Backend.cpp:10
static ActivationType To_AType(ActivationFn fn)
Reverse lookup of To_Fn(): maps a function pointer back to its ActivationType, by identity comparison...
Definition ActivationDispatch.h:152
ActivationType
Identifies an activation function or its derivative, for dispatch tables and layer/backend APIs that ...
Definition ActivationType.h:25
@ RELU
Definition ActivationType.h:26
@ dRELU
Definition ActivationType.h:27
OptimizerType
Selects which weight-update rule a layer applies during UpdateWeights().
Definition AdamOptimizer.h:52
@ SGD
Plain stochastic gradient descent (this codebase's existing cblas-based update).
Definition AdamOptimizer.h:53
static void relu(float *, size_t) noexcept
See Relu.h.
void(*)(float *, size_t, bool) DerivativeFn
Pointer to an in-place activation derivative: f(buf, n, activated).
Definition ActivationDispatch.h:20
static void dRelu(float *, size_t, bool) noexcept
See Relu.h.
void(*)(float *, size_t) ActivationFn
Pointer to an in-place activation function: f(buf, n).
Definition ActivationDispatch.h:18