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

#include <DiscriminativePCLayer.h>

Inheritance diagram for Deep::DiscriminativePCLayer:
Deep::Layer

Public Member Functions

 DiscriminativePCLayer (int size, int nextSize, int batchSize=1, float learningRate=1e-6, float inferenceRate=0.1f, float precisionRate=0.01f, float lmbda=1e-2f, void(*act)(float *, size_t)=relu, void(*dAct)(float *, size_t, bool)=dRelu)
 Constructor for a Deepity Layer.
 DiscriminativePCLayer (int size, int nextSize, int batchSize=1, float learningRate=1e-6, float inferenceRate=0.1f, float precisionRate=0.01f, float lmbda=1e-2f, ActivationType aType=ActivationType::RELU, ActivationType dType=ActivationType::dRELU)
 Constructor for a Deepity Layer, using a named ActivationType instead of raw function pointers.
float CalculateState () noexcept override
 Calculates the total network energy state.
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 UpdatePrecision () noexcept
 Updates this layer's precision weighting based on the current prediction error.
void Flush () noexcept override
 Does nothing; exists for class extension.
void ResyncLogPrecision () noexcept
 Recomputes log of precision if it falls behind.
void ClampState (const std::vector< float > &inputData) noexcept
 Clamps the layer to the input data.
void UnclampState () noexcept
 Unclamps the layer to the input data.
float * GetBeliefs () noexcept override
 Returns beliefs.
const float * GetErrors () const noexcept override
 Returns errors.
const float * GetMu () const noexcept
 Returns this layer's outgoing prediction buffer.
size_t GetInputSize () const noexcept override
 Returns size (input size).
size_t GetOutputSize () const noexcept override
 Returns nextSize (output size).
size_t GetBatchSize () const noexcept override
 Returns batchSize.
const float * GetWeights () const noexcept
 Returns a read-only version of the stored weights.
float * GetWeights () noexcept
 Returns a mutable version of the stored weights.
const float * GetBiases () const noexcept
 Returns a read-only version of the stored biases.
float * GetBiases () noexcept
 Returns a mutable version of the stored biases.
const float * GetPrecisions () const
 Returns a read-only version of the stored precisions.
float GetLearningRate () const noexcept
 Returns the learning rate used for weight updates.
float GetInferenceRate () const noexcept
 Returns the inference rate used for state updates.
float GetPrecisionRate () const noexcept
 Returns the learning rate used for precision updates.
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 used for state updates.
void SetPrecisionRate (float pr) noexcept
 Sets the learning rate used for precision updates.
void SetLambda (float l) noexcept
 Sets the weight-decay (L2 regularization) coefficient.
void SetOptimizer (OptimizerType o) noexcept
 Selects the optimizer used for weight updates.
void SetLayerAbove (DiscriminativePCLayer *above) noexcept
 Ties this layer to one above it.
void SetLayerBelow (DiscriminativePCLayer *below) noexcept
 Ties this layer to one below it.
void ResetState () noexcept
 Resets 'z' (beliefs) to 0.
const DiscriminativePCLayer & GetLayerAbove () const noexcept
 Gets const pointer to layer above.
const DiscriminativePCLayer & GetLayerBelow () const noexcept
 Gets const pointer to layer below.
void RandomizeWeights (std::mt19937 &twister) noexcept
 Makes the weights W randomized to [0.0, 0.1] using an OpenMP-parallelized uniform real distribution.
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
 Calculates exact float count required by this layer's architecture.
void BindMemory (MemoryArena &arena)
 Binds all tensor pointers to the contiguous memory block.
void ComputeMuOnly () noexcept
 Computes only mu (forward prediction), skipping error and energy computation entirely. Useful for callers that only need a forward pass through current weights (e.g. forward projection initialization) without the cost of the discarded error/energy values.
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 PCNDiagnostics

Additional Inherited Members

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

Detailed Description

Represents a single layer in a Predictive Coding Network.

  • To perform inference (prediction):
  1. Clamp the input data to the bottom layer's latent state (z).
  2. Enter a continuous loop across all layers, calling CalculateState() to compute the local prediction errors (e).
  3. Call UpdateState() to adjust the latent states (z) based on those errors.
  4. Repeat steps 2 and 3 until the states settle into an equilibrium (the energy is minimized and dz/dt approaches zero).
  5. Read the final predictions from the latent states of the desired layers.
  6. (For learning): Call UpdateWeights() on all layers simultaneously once the states have fully settled.

Constructor & Destructor Documentation

◆ DiscriminativePCLayer() [1/2]

Deep::DiscriminativePCLayer::DiscriminativePCLayer ( int size,
int nextSize,
int batchSize = 1,
float learningRate = 1e-6,
float inferenceRate = 0.1f,
float precisionRate = 0.01f,
float lmbda = 1e-2f,
void(* act )(float *, size_t) = relu,
void(* dAct )(float *, size_t, bool) = dRelu )

Constructor for a Deepity Layer.

Parameters
sizeSize of layer
nextSizeSize of next layer (used for row size of W)
batchSizeBatch size (default to 1 for simplicity)
learningRateLearning rate to update weights
inferenceRateLearning rate to update state
precisionRatePrecition weight to update precision
lmbdaWeight decay (L2 regularization) coefficient
actActivation function
dActDerivative of Activation function

◆ DiscriminativePCLayer() [2/2]

Deep::DiscriminativePCLayer::DiscriminativePCLayer ( int size,
int nextSize,
int batchSize = 1,
float learningRate = 1e-6,
float inferenceRate = 0.1f,
float precisionRate = 0.01f,
float lmbda = 1e-2f,
ActivationType aType = ActivationType::RELU,
ActivationType dType = ActivationType::dRELU )

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

Parameters
sizeSize of layer
nextSizeSize of next layer (used for row size of W)
batchSizeBatch size (default to 1 for simplicity)
learningRateLearning rate to update weights
inferenceRateLearning rate to update state
precisionRatePrecition weight to update precision
lmbdaWeight decay (L2 regularization) coefficient
aTypeActivation type
dTypeActivation derivative type

Member Function Documentation

◆ BindMemory()

void Deep::DiscriminativePCLayer::BindMemory ( MemoryArena & arena)

Binds all tensor pointers to the contiguous memory block.

◆ CalculateState()

float Deep::DiscriminativePCLayer::CalculateState ( )
overridevirtualnoexcept

Calculates the total network energy state.

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

Returns
This layer's energy contribution at the current state.

Implements Deep::Layer.

◆ ClampState()

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

Clamps the layer to the input data.

Parameters
inputDataInput

◆ ComputeMuOnly()

void Deep::DiscriminativePCLayer::ComputeMuOnly ( )
noexcept

Computes only mu (forward prediction), skipping error and energy computation entirely. Useful for callers that only need a forward pass through current weights (e.g. forward projection initialization) without the cost of the discarded error/energy values.

◆ Flush()

void Deep::DiscriminativePCLayer::Flush ( )
inlineoverridevirtualnoexcept

Does nothing; exists for class extension.

Reimplemented from Deep::Layer.

◆ GetActivationType()

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

Returns this layer's configured activation type.

Returns
ActivationType

◆ GetBatchSize()

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

Returns batchSize.

Returns
size_t batchSize

Implements Deep::Layer.

◆ GetBeliefs()

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

Returns beliefs.

Returns
float *z

Implements Deep::Layer.

◆ GetBiases() [1/2]

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

Returns a read-only version of the stored biases.

Returns
const float *b

◆ GetBiases() [2/2]

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

Returns a mutable version of the stored biases.

Returns
float *b

◆ GetDerivativeType()

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

Returns this layer's configured activation-derivative type.

Returns
ActivationType

◆ GetErrors()

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

Returns errors.

Returns
float *e

Implements Deep::Layer.

◆ GetInferenceRate()

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

Returns the inference rate used for state updates.

Returns
float ir

◆ GetInputSize()

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

Returns size (input size).

Returns
size_t size

Implements Deep::Layer.

◆ GetLambda()

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

Returns the weight-decay (L2 regularization) coefficient.

Returns
float lmbda

◆ GetLayerAbove()

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

Gets const pointer to layer above.

Returns
layerAbove

◆ GetLayerBelow()

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

Gets const pointer to layer below.

Returns
layerBelow

◆ GetLearningRate()

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

Returns the learning rate used for weight updates.

Returns
float lr

◆ GetMu()

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

Returns this layer's outgoing prediction buffer.

Returns
const float *mu

◆ GetOutputSize()

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

Returns nextSize (output size).

Returns
size_t nextSize

Implements Deep::Layer.

◆ GetPrecisionRate()

float Deep::DiscriminativePCLayer::GetPrecisionRate ( ) const
inlinenoexcept

Returns the learning rate used for precision updates.

Returns
float pr

◆ GetPrecisions()

const float * Deep::DiscriminativePCLayer::GetPrecisions ( ) const
inline

Returns a read-only version of the stored precisions.

Returns
const float *p

◆ GetRequiredFloats()

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

Calculates exact float count required by this layer's architecture.

◆ GetWeights() [1/2]

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

Returns a read-only version of the stored weights.

Returns
const float *W

◆ GetWeights() [2/2]

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

Returns a mutable version of the stored weights.

Returns
float *W

◆ RandomizeWeights()

void Deep::DiscriminativePCLayer::RandomizeWeights ( std::mt19937 & twister)
noexcept

Makes the weights W randomized to [0.0, 0.1] using an OpenMP-parallelized uniform real distribution.

Parameters
twisterThe classic Mersenne Twister

◆ ResetState()

void Deep::DiscriminativePCLayer::ResetState ( )
noexcept

Resets 'z' (beliefs) to 0.

◆ ResyncLogPrecision()

void Deep::DiscriminativePCLayer::ResyncLogPrecision ( )
noexcept

Recomputes log of precision if it falls behind.

◆ SetInferenceRate()

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

Sets the inference rate used for state updates.

Parameters
irThe new inference rate.

◆ SetLambda()

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

Sets the weight-decay (L2 regularization) coefficient.

Parameters
lThe new lambda value.

◆ SetLayerAbove()

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

Ties this layer to one above it.

Parameters
aboveDiscriminativePCLayer*

◆ SetLayerBelow()

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

Ties this layer to one below it.

Parameters
belowDiscriminativePCLayer*

◆ SetLearningRate()

void Deep::DiscriminativePCLayer::SetLearningRate ( float lr)
inlinenoexcept

Sets the learning rate used for weight updates.

Parameters
lrThe new learning rate.

◆ SetOptimizer()

void Deep::DiscriminativePCLayer::SetOptimizer ( OptimizerType o)
inlinenoexcept

Selects the optimizer used for weight updates.

Parameters
oThe optimizer type to use.

◆ SetPrecisionRate()

void Deep::DiscriminativePCLayer::SetPrecisionRate ( float pr)
inlinenoexcept

Sets the learning rate used for precision updates.

Parameters
prThe new precision rate.

◆ UnclampState()

void Deep::DiscriminativePCLayer::UnclampState ( )
noexcept

Unclamps the layer to the input data.

◆ UpdatePrecision()

void Deep::DiscriminativePCLayer::UpdatePrecision ( )
noexcept

Updates this layer's precision weighting based on the current prediction error.

◆ UpdateState()

void Deep::DiscriminativePCLayer::UpdateState ( )
overridevirtualnoexcept

Computes the state derivatives for inference.

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

Implements Deep::Layer.

◆ UpdateWeights()

void Deep::DiscriminativePCLayer::UpdateWeights ( )
overridevirtualnoexcept

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

\[W^{(l)} \leftarrow (1 - \lambda) W^{(l)} - \eta e^{(l)} (z^{(l+1)})^T \]

Implements Deep::Layer.

◆ PCNDiagnostics

friend class PCNDiagnostics
friend

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