Deepity
Ultra-fast, SIMD-accelerated Predictive Coding
Loading...
Searching...
No Matches
ConvPCLayer.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdlib>
8#include <map>
9#include <memory>
10#include <random>
11#include <stdexcept>
12#include <vector>
13
40
41namespace Deep {
42class PCNDiagnostics;
43
46class ConvPCLayer : public Layer {
47public:
68 ConvPCLayer(int inChannels, int outChannels, int inHeight, int inWidth,
69 int kernelH, int kernelW, int strideH = 1, int strideW = 1,
70 int padH = 0, int padW = 0, int batchSize = 1,
71 float learningRate = 1e-6f, float inferenceRate = 0.1f,
72 float precisionRate = 0.01f, float lmbda = 1e-2f,
75
80 float CalculateState() noexcept override;
83 void UpdateState() noexcept override;
87 void UpdateWeights() noexcept override;
90 void UpdatePrecision() noexcept;
91
93 void Flush() noexcept override {}
94
99 void ClampState(const std::vector<float> &inputData) noexcept;
102 void UnclampState() noexcept;
103
107 float *GetBeliefs() noexcept override { return z; }
111 const float *GetErrors() const noexcept override { return e; }
112
114 const float *GetMu() const noexcept { return mu; }
115
117 size_t GetInputSize() const noexcept override {
118 return (size_t)inChannels * inHeight * inWidth;
119 }
120
122 size_t GetOutputSize() const noexcept override {
123 return outChannels > 0 ? (size_t)outChannels * outHeight * outWidth : 0;
124 }
125
127 size_t GetBatchSize() const noexcept override { return batchSize; }
128
131 const float *GetWeights() const noexcept { return W; }
134 float *GetWeights() noexcept { return W; }
137 const float *GetBiases() const noexcept { return b; }
140 float *GetBiases() noexcept { return b; }
143 const float *GetPrecisions() const noexcept { return p; }
144
147 float GetLearningRate() const noexcept { return lr; }
150 float GetInferenceRate() const noexcept { return ir; }
153 float GetPrecisionRate() const noexcept { return pr; }
156 float GetLambda() const noexcept { return lmbda; }
157
160 void SetLearningRate(float lr) noexcept { this->lr = lr; }
163 void SetInferenceRate(float ir) noexcept { this->ir = ir; }
166 void SetPrecisionRate(float pr) noexcept { this->pr = pr; }
169 void SetLambda(float l) noexcept { this->lmbda = l; }
170
174 void SetLayerAbove(ConvPCLayer *above) noexcept { layerAbove = above; }
178 void SetLayerBelow(ConvPCLayer *below) noexcept { layerBelow = below; }
179
182 void ResetState() noexcept;
183
186 void RandomizeWeights(std::mt19937 &twister) noexcept;
187
188 std::map<std::string, TensorDescriptor> GetStateDict() const override;
189
193 void ResyncLogPrecision() noexcept;
194
196 void ComputeMuOnly() noexcept;
197
201 return To_AType(activation);
202 }
203
206 return To_AType(activationDerivative);
207 }
208
210 int GetInChannels() const noexcept { return inChannels; }
212 int GetOutChannels() const noexcept { return outChannels; }
214 int GetInHeight() const noexcept { return inHeight; }
216 int GetInWidth() const noexcept { return inWidth; }
218 int GetOutHeight() const noexcept { return outHeight; }
220 int GetOutWidth() const noexcept { return outWidth; }
222 int GetKernelH() const noexcept { return kernelH; }
224 int GetKernelW() const noexcept { return kernelW; }
225
230 size_t GetRequiredFloats() const noexcept;
234 void BindMemory(MemoryArena &arena);
235
236private:
237 std::unique_ptr<MemoryArena> localArena;
238
241 int inChannels, outChannels;
242 int inHeight, inWidth;
243 int outHeight, outWidth;
244 int kernelH, kernelW;
245 int strideH, strideW;
246 int padH, padW;
247 int batchSize;
249
252 float *W = nullptr;
253 float *b = nullptr;
254
255 float *z = nullptr;
257 float *e = nullptr;
259 float *dz_dt = nullptr;
261 float *p = nullptr;
262 float *log_p = nullptr;
264
266 float *mu = nullptr;
267
274 float *colBuffer = nullptr;
275
279 float *feedbackScratch = nullptr;
280
284 float *bottom_up_cols = nullptr;
285
290 float *colsRepacked = nullptr;
293 float *lgRepacked = nullptr;
296 float *muRepacked = nullptr;
298
300 float *cachedMu = nullptr;
302 bool muCacheValid = false;
303
304 float lr, ir, pr, lmbda;
305 bool isClamped = false;
306
307 ConvPCLayer *layerAbove;
308 ConvPCLayer *layerBelow;
309 ActivationFn activation;
310 DerivativeFn activationDerivative;
311 ActivationType activationType;
312
313 friend class PCNDiagnostics;
314};
315
316} // namespace Deep
317
Defines the activation functions of a predictive coding model.
Standalone im2col / col2im utilities: convolution expressed as matrix multiplication,...
Virtual Layer.
A 64-byte-aligned bump-pointer allocator over a single heap-allocated block, used to pack every layer...
Convolutional Predictive Coding layer (im2col-based). See the file-level warning above before enablin...
Definition ConvPCLayer.h:46
const float * GetMu() const noexcept
Returns this layer's top-down prediction buffer.
Definition ConvPCLayer.h:114
const float * GetErrors() const noexcept override
Returns this layer's prediction-error buffer.
Definition ConvPCLayer.h:111
float * GetBiases() noexcept
Returns this layer's bias buffer.
Definition ConvPCLayer.h:140
void UpdateWeights() noexcept override
Hebbian/gradient weight update.
Definition ConvPCLayer.cpp:331
void SetPrecisionRate(float pr) noexcept
Sets the learning rate used for precision updates.
Definition ConvPCLayer.h:166
const float * GetPrecisions() const noexcept
Returns this layer's precision buffer.
Definition ConvPCLayer.h:143
void SetLayerAbove(ConvPCLayer *above) noexcept
Sets the layer immediately above this one in the network.
Definition ConvPCLayer.h:174
float GetLambda() const noexcept
Returns the weight-decay (L2 regularization) coefficient.
Definition ConvPCLayer.h:156
int GetKernelH() const noexcept
Returns the convolution kernel height.
Definition ConvPCLayer.h:222
void SetLambda(float l) noexcept
Sets the weight-decay (L2 regularization) coefficient.
Definition ConvPCLayer.h:169
void ResyncLogPrecision() noexcept
Rebuilds log_p from p – required after a checkpoint load that only persists p (mirrors Discriminative...
Definition ConvPCLayer.cpp:452
void SetLayerBelow(ConvPCLayer *below) noexcept
Sets the layer immediately below this one in the network.
Definition ConvPCLayer.h:178
int GetOutHeight() const noexcept
Returns the output feature-map height.
Definition ConvPCLayer.h:218
friend class PCNDiagnostics
Definition ConvPCLayer.h:313
int GetInHeight() const noexcept
Returns the input feature-map height.
Definition ConvPCLayer.h:214
float GetPrecisionRate() const noexcept
Returns the learning rate used for precision updates.
Definition ConvPCLayer.h:153
void UpdatePrecision() noexcept
Updates this layer's precision estimate from current prediction errors, using the configured precisio...
Definition ConvPCLayer.cpp:409
ActivationType GetActivationType() const noexcept
Returns this layer's configured activation type.
Definition ConvPCLayer.h:200
size_t GetOutputSize() const noexcept override
Flattened element count of this layer's OUTGOING prediction (outChannels*outH*outW),...
Definition ConvPCLayer.h:122
float GetLearningRate() const noexcept
Returns the learning rate used for weight updates.
Definition ConvPCLayer.h:147
int GetOutWidth() const noexcept
Returns the output feature-map width.
Definition ConvPCLayer.h:220
size_t GetBatchSize() const noexcept override
Returns the batch size this layer was constructed with.
Definition ConvPCLayer.h:127
int GetInWidth() const noexcept
Returns the input feature-map width.
Definition ConvPCLayer.h:216
int GetOutChannels() const noexcept
Returns the number of output channels (0 for a terminal layer).
Definition ConvPCLayer.h:212
void UnclampState() noexcept
Releases a previous ClampState() call, allowing this layer's beliefs to update normally again.
Definition ConvPCLayer.cpp:447
void SetLearningRate(float lr) noexcept
Sets the learning rate used for weight updates.
Definition ConvPCLayer.h:160
const float * GetBiases() const noexcept
Returns this layer's bias buffer.
Definition ConvPCLayer.h:137
std::map< std::string, TensorDescriptor > GetStateDict() const override
Returns a dictionary of this.
Definition ConvPCLayer.cpp:76
void ClampState(const std::vector< float > &inputData) noexcept
Clamps this layer's beliefs to externally-provided data, preventing them from being updated by Update...
Definition ConvPCLayer.cpp:438
float * GetWeights() noexcept
Returns this layer's weight buffer.
Definition ConvPCLayer.h:134
float CalculateState() noexcept override
Calculate energy/prediction errors for this layer.
Definition ConvPCLayer.cpp:170
size_t GetInputSize() const noexcept override
Flattened element count per batch item (inChannels*H*W).
Definition ConvPCLayer.h:117
ActivationType GetDerivativeType() const noexcept
Returns this layer's configured activation-derivative type.
Definition ConvPCLayer.h:205
void ResetState() noexcept
Resets this layer's beliefs/errors back to their initial values, without touching learned weights.
Definition ConvPCLayer.cpp:432
void RandomizeWeights(std::mt19937 &twister) noexcept
Randomizes this layer's weights (and biases) in place.
Definition ConvPCLayer.cpp:146
float GetInferenceRate() const noexcept
Returns the learning rate used for internal-state updates.
Definition ConvPCLayer.h:150
const float * GetWeights() const noexcept
Returns this layer's weight buffer.
Definition ConvPCLayer.h:131
int GetInChannels() const noexcept
Returns the number of input channels.
Definition ConvPCLayer.h:210
void SetInferenceRate(float ir) noexcept
Sets the learning rate used for internal-state updates.
Definition ConvPCLayer.h:163
size_t GetRequiredFloats() const noexcept
Computes the total number of floats this layer requires from a MemoryArena (weights,...
Definition ConvPCLayer.cpp:43
int GetKernelW() const noexcept
Returns the convolution kernel width.
Definition ConvPCLayer.h:224
void BindMemory(MemoryArena &arena)
Binds this layer's weight/state/scratch buffers into the supplied arena. Must be called before any ot...
Definition ConvPCLayer.cpp:83
ConvPCLayer(int inChannels, int outChannels, int inHeight, int inWidth, int kernelH, int kernelW, int strideH=1, int strideW=1, int padH=0, int padW=0, int batchSize=1, float learningRate=1e-6f, float inferenceRate=0.1f, float precisionRate=0.01f, float lmbda=1e-2f, ActivationType aType=ActivationType::RELU, ActivationType dType=ActivationType::dRELU)
Constructor for a convolutional PC layer.
Definition ConvPCLayer.cpp:15
float * GetBeliefs() noexcept override
Returns this layer's belief buffer.
Definition ConvPCLayer.h:107
void ComputeMuOnly() noexcept
Fast-path forward projection that skips state initialization.
Definition ConvPCLayer.cpp:213
void UpdateState() noexcept override
Update latent beliefs (z/r) via inference gradient.
Definition ConvPCLayer.cpp:265
void Flush() noexcept override
No-op; exists for Layer interface conformance.
Definition ConvPCLayer.h:93
A deepity layer virtual class.
Definition Layer.h:40
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
void(*)(float *, size_t, bool) DerivativeFn
Pointer to an in-place activation derivative: f(buf, n, activated).
Definition ActivationDispatch.h:20
void(*)(float *, size_t) ActivationFn
Pointer to an in-place activation function: f(buf, n).
Definition ActivationDispatch.h:18
A named, shaped view into a layer's weight/bias data, for ModelIO's save/load (see Layer::GetStateDic...
Definition Layer.h:34