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

Predictive Coding layer without precision weighting – the simpler, unweighted-energy counterpart to DiscriminativePCLayer. More...

#include <SimplePCLayer.h>

Inheritance diagram for Deep::SimplePCLayer:
Deep::Layer

Public Member Functions

 SimplePCLayer (size_t size, size_t nextSize, size_t 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, IComputeBackend *backend=nullptr)
 Constructor for a SimplePCLayer.
 SimplePCLayer (size_t size, size_t nextSize, size_t batchSize=1, float learningRate=1e-6f, float inferenceRate=0.1f, float lmbda=1e-2f, ActivationType aType=ActivationType::RELU, ActivationType dType=ActivationType::dRELU, IComputeBackend *backend=nullptr)
 Constructor for a SimplePCLayer, using a named ActivationType instead of raw function pointers.
float CalculateState (bool needEnergy=true) noexcept
 Calculates the total network energy state, and this layer's outgoing prediction (mu).
float CalculateState () noexcept override
 Calculates the internal state of the layer.
void UpdateState () noexcept override
 Computes the state derivatives for inference.
void UpdateWeights () noexcept override
 Computes weight updates via gradient descent, with L2 weight decay.
void Flush () noexcept override
 No-op; exists for Layer interface conformance.
void ClampState (const std::vector< float > &inputData) noexcept
 Clamps this layer's beliefs to externally-provided data, preventing them from being updated by UpdateState().
void UnclampState () noexcept
 Releases a previous ClampState() call, allowing this layer's beliefs to update normally again.
bool IsClamped () const noexcept
 Whether ClampState() is currently active on this layer.
float * GetBeliefs () noexcept override
 Returns this layer's belief buffer.
const float * GetMu () const noexcept
 Returns a prediction buffer exposed specifically for forward-projection initialization (seeding the next layer's z from a genuine, current-weights forward pass, rather than zero-init). nullptr for a terminal layer (nextSize=0).
const float * GetErrors () const noexcept override
 Returns this layer's prediction-error buffer.
size_t GetInputSize () const noexcept override
 Returns this layer's own belief size.
size_t GetOutputSize () const noexcept override
 Returns this layer's outgoing prediction size (0 for a terminal layer).
size_t GetBatchSize () const noexcept override
 Returns the batch size this layer was constructed with.
const float * GetWeights () const noexcept
 Returns this layer's weight buffer.
float * GetWeights () noexcept
 Returns this layer's weight buffer.
const float * GetBiases () const noexcept
 Returns this layer's bias buffer.
float * GetBiases () noexcept
 Returns this layer's bias buffer.
float GetLearningRate () const noexcept
 Returns the learning rate used for weight updates.
float GetInferenceRate () const noexcept
 Returns the inference rate (Euler integration step size).
float GetLambda () const noexcept
 Returns the weight-decay (L2 regularization) coefficient.
void SetLearningRate (float lr) noexcept
 Sets the learning rate used for weight updates.
void SetInferenceRate (float ir) noexcept
 Sets the inference rate (Euler integration step size).
void SetLambda (float l) noexcept
 Sets the weight-decay (L2 regularization) coefficient.
void SetOptimizer (const OptimizerType o) noexcept
 Selects the optimizer used for weight updates (SGD or Adam). Adam's moment buffers are lazily allocated on first use.
void SetMuCacheThreshold (float threshold) noexcept
 Sets the mu-cache staleness threshold: mu is recomputed only when ||z - z_at_last_recompute|| / (||z_at_last_recompute||.
float GetMuCacheThreshold () const noexcept
 Returns the current mu-cache staleness threshold – see SetMuCacheThreshold() above.
void ComputeMuOnly () noexcept
 Computes only mu (forward prediction), skipping error/energy entirely. Extracted from CalculateState() for callers (like ProjectForward()) that don't need the discarded error/energy values. Computes zF=phi(z) internally and uses it for the GEMM, matching the activate-before-transform convention (mu=W@phi(z)+b).
void SetLayerAbove (SimplePCLayer *above) noexcept
 Sets the layer immediately above this one in the network.
void SetLayerBelow (SimplePCLayer *below) noexcept
 Sets the layer immediately below this one in the network.
void ResetState () noexcept
 Resets this layer's beliefs/errors back to their initial values, without touching learned weights.
const SimplePCLayer & GetLayerAbove () const noexcept
 Returns the layer immediately above this one.
const SimplePCLayer & GetLayerBelow () const noexcept
 Returns the layer immediately below this one.
void RandomizeWeights (std::mt19937 &twister, const char *distribution="normal") noexcept
 Randomizes this layer's weights (and biases) in place.
ActivationType GetActivationType () const noexcept
 Returns this layer's configured activation type.
ActivationType GetDerivativeType () const noexcept
 Returns this layer's configured activation-derivative type.
size_t GetRequiredFloats () const noexcept
 Computes the total number of floats this layer requires from a MemoryArena (weights, biases, beliefs, errors, scratch buffers, and, if Adam is selected, its moment buffers).
template<typename ArenaT>
void BindMemory (ArenaT &arena)
 Binds this layer's weight/state/scratch buffers into the supplied arena. Must be called before any other operation. Templated so either MemoryArena (CPU) or DeviceMemoryArena (GPU) can be bound, resolved entirely at compile time – see the .cpp's explicit instantiations for the two concrete types actually used.
Public Member Functions inherited from Deep::Layer
virtual ~Layer ()=default
virtual std::map< std::string, TensorDescriptor > GetStateDict () const
 Returns a dictionary of this.

Friends

class SimplePCNDiagnostics

Additional Inherited Members

Protected Attributes inherited from Deep::Layer
size_t size
 Size of input.
size_t nextSize
 Size of output.

Detailed Description

Predictive Coding layer without precision weighting – the simpler, unweighted-energy counterpart to DiscriminativePCLayer.

Constructor & Destructor Documentation

◆ SimplePCLayer() [1/2]

Deep::SimplePCLayer::SimplePCLayer ( size_t size,
size_t nextSize,
size_t 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,
IComputeBackend * backend = nullptr )

Constructor for a SimplePCLayer.

Parameters
sizeSize of this layer's own belief (z)
nextSizeSize of the layer above's belief (this layer's outgoing prediction target); 0 marks a terminal layer
batchSizeBatch size
learningRateLearning rate for weight updates
inferenceRateInference rate (Euler integration step size)
lmbdaWeight decay (L2 regularization) coefficient
actActivation function
dActDerivative of activation function
backendCompute backend to route all math through. Defaults to nullptr, in which case this layer constructs and owns its own CPUBackend internally – existing callers don't need to change anything. Pass a real backend (owned elsewhere, e.g. by the network) to run this layer on GPU.

◆ SimplePCLayer() [2/2]

Deep::SimplePCLayer::SimplePCLayer ( size_t size,
size_t nextSize,
size_t batchSize = 1,
float learningRate = 1e-6f,
float inferenceRate = 0.1f,
float lmbda = 1e-2f,
ActivationType aType = ActivationType::RELU,
ActivationType dType = ActivationType::dRELU,
IComputeBackend * backend = nullptr )

Constructor for a SimplePCLayer, using a named ActivationType instead of raw function pointers.

Parameters
sizeSize of this layer's own belief (z)
nextSizeSize of the layer above's belief (this layer's outgoing prediction target); 0 marks a terminal layer
batchSizeBatch size
learningRateLearning rate for weight updates
inferenceRateInference rate (Euler integration step size)
lmbdaWeight decay (L2 regularization) coefficient
aTypeActivation type
dTypeActivation derivative type
backendCompute backend to route all math through. See the other constructor's doc for the default-nullptr behavior.

Member Function Documentation

◆ BindMemory()

template<typename ArenaT>
template void Deep::SimplePCLayer::BindMemory< DeviceMemoryArena > ( ArenaT & arena)

Binds this layer's weight/state/scratch buffers into the supplied arena. Must be called before any other operation. Templated so either MemoryArena (CPU) or DeviceMemoryArena (GPU) can be bound, resolved entirely at compile time – see the .cpp's explicit instantiations for the two concrete types actually used.

Parameters
arenaThe arena to bind into.

◆ CalculateState() [1/2]

float Deep::SimplePCLayer::CalculateState ( )
inlineoverridevirtualnoexcept

Calculates the internal state of the layer.

Returns
energy used

Implements Deep::Layer.

◆ CalculateState() [2/2]

float Deep::SimplePCLayer::CalculateState ( bool needEnergy = true)
noexcept

Calculates the total network energy state, and this layer's outgoing prediction (mu).

\[E = \sum_l 1/2 ||z^{(l)} - \mu^{(l)}||^2 \]

mu is now computed as mu = W @ phi(z) + b – activation applied to z BEFORE the linear transform, matching ngc-learn's documented convention exactly (was previously mu = phi(W@z+b), activation AFTER the transform). (No precision weighting – see file-level note.)

Parameters
needEnergyAsks for energy
Returns
This layer's energy contribution at the current state, if asked for.

◆ ClampState()

void Deep::SimplePCLayer::ClampState ( const std::vector< float > & inputData)
noexcept

Clamps this layer's beliefs to externally-provided data, preventing them from being updated by UpdateState().

Parameters
inputDataFlattened input data of length size.

◆ ComputeMuOnly()

void Deep::SimplePCLayer::ComputeMuOnly ( )
noexcept

Computes only mu (forward prediction), skipping error/energy entirely. Extracted from CalculateState() for callers (like ProjectForward()) that don't need the discarded error/energy values. Computes zF=phi(z) internally and uses it for the GEMM, matching the activate-before-transform convention (mu=W@phi(z)+b).

◆ Flush()

void Deep::SimplePCLayer::Flush ( )
inlineoverridevirtualnoexcept

No-op; exists for Layer interface conformance.

Reimplemented from Deep::Layer.

◆ GetActivationType()

ActivationType Deep::SimplePCLayer::GetActivationType ( ) const
inlinenoexcept

Returns this layer's configured activation type.

◆ GetBatchSize()

size_t Deep::SimplePCLayer::GetBatchSize ( ) const
inlineoverridevirtualnoexcept

Returns the batch size this layer was constructed with.

Implements Deep::Layer.

◆ GetBeliefs()

float * Deep::SimplePCLayer::GetBeliefs ( )
inlineoverridevirtualnoexcept

Returns this layer's belief buffer.

Returns
Pointer to this layer's size-length beliefs.

Implements Deep::Layer.

◆ GetBiases() [1/2]

const float * Deep::SimplePCLayer::GetBiases ( ) const
inlinenoexcept

Returns this layer's bias buffer.

◆ GetBiases() [2/2]

float * Deep::SimplePCLayer::GetBiases ( )
inlinenoexcept

Returns this layer's bias buffer.

◆ GetDerivativeType()

ActivationType Deep::SimplePCLayer::GetDerivativeType ( ) const
inlinenoexcept

Returns this layer's configured activation-derivative type.

◆ GetErrors()

const float * Deep::SimplePCLayer::GetErrors ( ) const
inlineoverridevirtualnoexcept

Returns this layer's prediction-error buffer.

Returns
Pointer to this layer's size-length errors.

Implements Deep::Layer.

◆ GetInferenceRate()

float Deep::SimplePCLayer::GetInferenceRate ( ) const
inlinenoexcept

Returns the inference rate (Euler integration step size).

◆ GetInputSize()

size_t Deep::SimplePCLayer::GetInputSize ( ) const
inlineoverridevirtualnoexcept

Returns this layer's own belief size.

Implements Deep::Layer.

◆ GetLambda()

float Deep::SimplePCLayer::GetLambda ( ) const
inlinenoexcept

Returns the weight-decay (L2 regularization) coefficient.

◆ GetLayerAbove()

const SimplePCLayer & Deep::SimplePCLayer::GetLayerAbove ( ) const
inlinenoexcept

Returns the layer immediately above this one.

Warning
Dereferences layerAbove without a null check; only valid if SetLayerAbove() was previously called with a non-null pointer.

◆ GetLayerBelow()

const SimplePCLayer & Deep::SimplePCLayer::GetLayerBelow ( ) const
inlinenoexcept

Returns the layer immediately below this one.

Warning
Dereferences layerBelow without a null check; only valid if SetLayerBelow() was previously called with a non-null pointer.

◆ GetLearningRate()

float Deep::SimplePCLayer::GetLearningRate ( ) const
inlinenoexcept

Returns the learning rate used for weight updates.

◆ GetMu()

const float * Deep::SimplePCLayer::GetMu ( ) const
inlinenoexcept

Returns a prediction buffer exposed specifically for forward-projection initialization (seeding the next layer's z from a genuine, current-weights forward pass, rather than zero-init). nullptr for a terminal layer (nextSize=0).

Returns
Outgoing prediction buffer

◆ GetMuCacheThreshold()

float Deep::SimplePCLayer::GetMuCacheThreshold ( ) const
inlinenoexcept

Returns the current mu-cache staleness threshold – see SetMuCacheThreshold() above.

◆ GetOutputSize()

size_t Deep::SimplePCLayer::GetOutputSize ( ) const
inlineoverridevirtualnoexcept

Returns this layer's outgoing prediction size (0 for a terminal layer).

Implements Deep::Layer.

◆ GetRequiredFloats()

size_t Deep::SimplePCLayer::GetRequiredFloats ( ) const
noexcept

Computes the total number of floats this layer requires from a MemoryArena (weights, biases, beliefs, errors, scratch buffers, and, if Adam is selected, its moment buffers).

Returns
The required float count.

◆ GetWeights() [1/2]

const float * Deep::SimplePCLayer::GetWeights ( ) const
inlinenoexcept

Returns this layer's weight buffer.

◆ GetWeights() [2/2]

float * Deep::SimplePCLayer::GetWeights ( )
inlinenoexcept

Returns this layer's weight buffer.

◆ IsClamped()

bool Deep::SimplePCLayer::IsClamped ( ) const
inlinenoexcept

Whether ClampState() is currently active on this layer.

◆ RandomizeWeights()

void Deep::SimplePCLayer::RandomizeWeights ( std::mt19937 & twister,
const char * distribution = "normal" )
noexcept

Randomizes this layer's weights (and biases) in place.

Parameters
twisterThe classic Mersenne Twister
distributionThe distribution of the randomization: (normal, uniform)

◆ ResetState()

void Deep::SimplePCLayer::ResetState ( )
noexcept

Resets this layer's beliefs/errors back to their initial values, without touching learned weights.

◆ SetInferenceRate()

void Deep::SimplePCLayer::SetInferenceRate ( float ir)
inlinenoexcept

Sets the inference rate (Euler integration step size).

Parameters
irThe new inference rate.

◆ SetLambda()

void Deep::SimplePCLayer::SetLambda ( float l)
inlinenoexcept

Sets the weight-decay (L2 regularization) coefficient.

Parameters
lThe new lambda value.

◆ SetLayerAbove()

void Deep::SimplePCLayer::SetLayerAbove ( SimplePCLayer * above)
inlinenoexcept

Sets the layer immediately above this one in the network.

Parameters
abovePointer to the layer above; may be nullptr for a terminal layer.

◆ SetLayerBelow()

void Deep::SimplePCLayer::SetLayerBelow ( SimplePCLayer * below)
inlinenoexcept

Sets the layer immediately below this one in the network.

Parameters
belowPointer to the layer below; may be nullptr for the input layer.

◆ SetLearningRate()

void Deep::SimplePCLayer::SetLearningRate ( float lr)
noexcept

Sets the learning rate used for weight updates.

Parameters
lrThe new learning rate.

◆ SetMuCacheThreshold()

void Deep::SimplePCLayer::SetMuCacheThreshold ( float threshold)
inlinenoexcept

Sets the mu-cache staleness threshold: mu is recomputed only when ||z - z_at_last_recompute|| / (||z_at_last_recompute||.

  • eps) exceeds this value. threshold=-1 disables caching entirely (default). threshold=0 reproduces the original, EXACT clamped-only behavior (a clamped layer's z never changes, so its ratio is always exactly 0, always below any threshold>=0). threshold>0 extends caching to UNCLAMPED layers too, as a genuine approximation – correctness there means "close enough for real training," not "bit-identical," and needs its own accuracy-impact validation, not just a trajectory diff.

◆ SetOptimizer()

void Deep::SimplePCLayer::SetOptimizer ( const OptimizerType o)
inlinenoexcept

Selects the optimizer used for weight updates (SGD or Adam). Adam's moment buffers are lazily allocated on first use.

Parameters
oThe optimizer type to use.

◆ UnclampState()

void Deep::SimplePCLayer::UnclampState ( )
noexcept

Releases a previous ClampState() call, allowing this layer's beliefs to update normally again.

◆ UpdateState()

void Deep::SimplePCLayer::UpdateState ( )
overridevirtualnoexcept

Computes the state derivatives for inference.

\[\frac{dz^{(l)}}{dt} = -e^{(l)} + \sigma'(z^{(l)}) \odot (W^{(l-1)})^T e^{(l-1)} \]

The derivative multiply now applies AFTER the W transform, using f'(z) (this layer's OWN state derivative), not f'(mu) applied before – matching the activate-before-transform convention.

Implements Deep::Layer.

◆ UpdateWeights()

void Deep::SimplePCLayer::UpdateWeights ( )
overridevirtualnoexcept

Computes weight updates via gradient descent, with L2 weight decay.

Implements Deep::Layer.

◆ SimplePCNDiagnostics

friend class SimplePCNDiagnostics
friend

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