Class TemporalMemoryLayer<T>
- Namespace
- AiDotNet.NeuralNetworks.Layers
- Assembly
- AiDotNet.dll
Represents a temporal memory layer that models sequence learning through hierarchical temporal memory concepts.
public class TemporalMemoryLayer<T> : LayerBase<T>, ILayer<T>, IJitCompilable<T>, IDiagnosticsProvider, IWeightLoadable<T>, IDisposable
Type Parameters
TThe numeric type used for calculations, typically float or double.
- Inheritance
-
LayerBase<T>TemporalMemoryLayer<T>
- Implements
-
ILayer<T>
- Inherited Members
Remarks
A temporal memory layer implements a simplified version of Hierarchical Temporal Memory (HTM) concepts to learn sequential patterns in data. It organizes cells into columns, where cells within the same column represent alternative contexts for the same input pattern. This allows the layer to maintain multiple predictions simultaneously and learn temporal patterns in the input data.
For Beginners: This layer helps the network remember and predict sequences of patterns.
Think of it like learning to anticipate what comes next in a song:
- The layer organizes memory cells into columns (like musical notes)
- Each column can have multiple cells (representing different contexts for the same note)
- When a note plays, the layer activates specific cells based on what came before
- Over time, it learns which notes typically follow others in different contexts
For example, in a melody, the note "C" might be followed by "D" in the verse but by "G" in the chorus. This layer helps the network learn such context-dependent sequences by remembering not just what happened, but the context in which it happened.
Constructors
TemporalMemoryLayer(int, int)
Initializes a new instance of the TemporalMemoryLayer<T> class.
public TemporalMemoryLayer(int columnCount, int cellsPerColumn)
Parameters
columnCountintThe number of columns in the layer.
cellsPerColumnintThe number of cells per column.
Remarks
This constructor creates a temporal memory layer with the specified number of columns and cells per column. It initializes all cell states to zero and sets up the layer for processing sequential data.
For Beginners: This constructor creates a new temporal memory layer.
The parameters you provide determine:
- columnCount: How many different patterns the layer can recognize
- cellsPerColumn: How many different contexts the layer can distinguish for each pattern
Together, these parameters define the memory capacity of the layer:
- More columns allow recognition of more distinct input patterns
- More cells per column allow finer discrimination of temporal contexts
For example, a layer with 1000 columns and 4 cells per column could recognize 1000 different patterns, each in 4 different temporal contexts.
Properties
PreviousState
Gets or sets the previous input state of the layer.
public Vector<T> PreviousState { get; set; }
Property Value
- Vector<T>
Remarks
This property stores the input vector from the previous time step, which is used to establish temporal context for learning sequential patterns.
For Beginners: This remembers what patterns were active in the previous step.
Previous state works like this:
- It stores which columns were active in the last time step
- This provides context for understanding the current input
- It helps the layer learn which patterns typically follow others
For example, when learning a melody, this would remember which notes were just played, helping the layer understand the musical context of the current note.
SupportsGpuExecution
Gets whether this layer has a GPU execution implementation for inference.
protected override bool SupportsGpuExecution { get; }
Property Value
Remarks
Override this to return true when the layer implements ForwardGpu(params IGpuTensor<T>[]). The actual CanExecuteOnGpu property combines this with engine availability.
For Beginners: This flag indicates if the layer has GPU code for the forward pass. Set this to true in derived classes that implement ForwardGpu.
SupportsJitCompilation
Gets a value indicating whether this layer supports JIT compilation.
public override bool SupportsJitCompilation { get; }
Property Value
- bool
Always
true. TemporalMemoryLayer uses a differentiable approximation for JIT.
Remarks
JIT compilation for TemporalMemory uses a simplified differentiable approximation of the HTM algorithm. The complex cell state tracking and prediction mechanisms are approximated with matrix projections and sigmoid activations, enabling gradient-based optimization while maintaining similar sparse activation patterns.
SupportsTraining
Gets a value indicating whether this layer supports training.
public override bool SupportsTraining { get; }
Property Value
- bool
truefor this layer, as it implements temporal learning mechanisms.
Remarks
This property indicates whether the temporal memory layer can be trained. Since this layer implements mechanisms for learning sequential patterns, it supports training.
For Beginners: This property tells you if the layer can learn from data.
A value of true means:
- The layer has internal states that can be adjusted during training
- It will improve its performance as it sees more data
- It participates in the learning process
For this layer, the value is always true because it needs to learn which sequences of patterns commonly occur in the input data.
Methods
Backward(Tensor<T>)
Performs the backward pass of the temporal memory layer.
public override Tensor<T> Backward(Tensor<T> outputGradient)
Parameters
outputGradientTensor<T>The gradient of the loss with respect to the layer's output.
Returns
- Tensor<T>
The gradient of the loss with respect to the layer's input.
Remarks
This method implements the backward pass of the temporal memory layer, which is used during training to propagate error gradients back through the network. It sums the gradients for all cells in each column to produce a gradient for each column in the input.
For Beginners: This method helps the layer understand how to adjust its behavior during training.
During the backward pass:
- The layer receives information about how its output should change (outputGradient)
- It calculates how each input column contributed to the output errors
- It creates an inputGradient to pass back to previous layers
The process works by:
- Summing up the gradients for all cells in each column
- This tells the layer how each column's activation affected the final result
While the main learning in this layer happens through the Learn method, this backward pass allows it to participate in the neural network's overall gradient-based learning.
ExportComputationGraph(List<ComputationNode<T>>)
Exports the layer's computation graph for JIT compilation.
public override ComputationNode<T> ExportComputationGraph(List<ComputationNode<T>> inputNodes)
Parameters
inputNodesList<ComputationNode<T>>List to populate with input computation nodes.
Returns
- ComputationNode<T>
The output computation node representing the layer's operation.
Remarks
This method constructs a computation graph representation of the layer's forward pass that can be JIT compiled for faster inference. All layers MUST implement this method to support JIT compilation.
For Beginners: JIT (Just-In-Time) compilation converts the layer's operations into optimized native code for 5-10x faster inference.
To support JIT compilation, a layer must:
- Implement this method to export its computation graph
- Set SupportsJitCompilation to true
- Use ComputationNode and TensorOperations to build the graph
All layers are required to implement this method, even if they set SupportsJitCompilation = false.
Forward(Tensor<T>)
Performs the forward pass of the temporal memory layer.
public override Tensor<T> Forward(Tensor<T> input)
Parameters
inputTensor<T>The input tensor to process.
Returns
- Tensor<T>
The output tensor representing cell activations.
Remarks
This method implements the forward pass of the temporal memory layer. It takes an input tensor where each element represents the activation of a column, and outputs a tensor where each element represents the activation of an individual cell. Only cells in active columns (where the input is 1) can be active in the output.
For Beginners: This method processes the input data through the temporal memory cells.
During the forward pass:
- The layer receives input indicating which columns should be active
- For each active column, it outputs the activation states of all cells in that column
- Inactive columns produce no output (all zeros)
This process:
- Transforms column-level input into cell-level output
- Preserves the context information stored in cell states
- Creates an output that represents both the input and its temporal context
For example, if the input activates the column for the letter "a", the output will reflect which specific "a" contexts are currently active based on past inputs.
ForwardGpu(params IGpuTensor<T>[])
Performs the forward pass of the layer on GPU.
public override IGpuTensor<T> ForwardGpu(params IGpuTensor<T>[] inputs)
Parameters
inputsIGpuTensor<T>[]The GPU-resident input tensor(s).
Returns
- IGpuTensor<T>
The GPU-resident output tensor.
Remarks
This method performs the layer's forward computation entirely on GPU. The input and output tensors remain in GPU memory, avoiding expensive CPU-GPU transfers.
For Beginners: This is like Forward() but runs on the graphics card.
The key difference:
- Forward() uses CPU tensors that may be copied to/from GPU
- ForwardGpu() keeps everything on GPU the whole time
Override this in derived classes that support GPU acceleration.
Exceptions
- NotSupportedException
Thrown when the layer does not support GPU execution.
GetParameters()
Gets all cell states of the layer as a single vector.
public override Vector<T> GetParameters()
Returns
- Vector<T>
A vector containing all cell states.
Remarks
This method retrieves all cell states in the temporal memory layer and combines them into a single vector. This is useful for saving the layer's state or for visualization purposes.
For Beginners: This method collects all the cell activation values into a single list.
The parameters:
- Are the current activation values of all cells in the layer
- Represent what the layer has learned about temporal sequences
- Are flattened into a single long list (vector)
This is useful for:
- Saving the layer's current state to disk
- Visualizing what patterns the layer has learned
- Transferring the learned state to another network
GetPredictions()
Gets the predicted columns based on the current cell states.
public Vector<T> GetPredictions()
Returns
- Vector<T>
A vector containing the predicted column activations.
Remarks
This method predicts which columns are likely to be active in the next time step based on the current activation patterns of cells. A column is predicted to be active if any of its cells have a high activation state, indicating that the pattern represented by that column is expected to occur based on the current context.
For Beginners: This method tells you what patterns the network expects to see next.
The prediction process works like this:
- The layer examines the current state of all cells
- For each column, it determines if any cells are strongly activated
- If yes, that column is predicted to be active in the next time step
- The result is a vector where 1 means "predicted active" and 0 means "not predicted"
Think of it like predicting the next note in a melody based on what you've heard so far. If you've heard a sequence like "do-re-mi" many times, when you hear "do-re", you would predict "mi" as the next note.
Learn(Vector<T>, Vector<T>)
Updates the cell states based on the current input and previous state.
public void Learn(Vector<T> input, Vector<T> previousState)
Parameters
inputVector<T>The current input vector.
previousStateVector<T>The previous input state.
Remarks
This method implements the learning mechanism of the temporal memory layer. It strengthens the activation of cells in active columns and weakens the activation of cells in inactive columns. After updating, the cell states are normalized so that they sum to 1 within each column.
For Beginners: This method is where the layer learns temporal patterns.
During learning:
For active columns (input = 1):
- Active cells get strengthened (their activation increases)
- This reinforces the cells that correctly predicted the current input
For inactive columns (input = 0):
- All cells get weakened (their activation decreases)
- This reduces false predictions
Finally, cell states within each column are normalized:
- This ensures the total activation in each column remains constant
- It creates a competition among cells in the same column
Over time, this process helps the layer learn which cells should be active in which temporal contexts, allowing it to make better predictions.
ResetState()
Resets the internal state of the layer.
public override void ResetState()
Remarks
This method resets the internal state of the temporal memory layer by setting all cell states and the previous state vector to zero. This is useful when starting to process a new, unrelated sequence.
For Beginners: This method clears the layer's memory to start fresh.
When resetting the state:
- All cell activations are set to zero
- The record of previous input is cleared
- The layer forgets all temporal context
This is important for:
- Processing a new, unrelated sequence
- Preventing information from one sequence affecting another
- Testing the layer with fresh inputs
Think of it like clearing your mind before starting a completely new task, so that memories from the previous task don't interfere.
SetParameters(Vector<T>)
Sets the trainable parameters of the layer from a single vector.
public override void SetParameters(Vector<T> parameters)
Parameters
parametersVector<T>A vector containing all parameters to set.
Remarks
This method sets the cell states from a flattened vector. This is useful for loading saved model states or for transferring learned patterns to another network.
Exceptions
- ArgumentException
Thrown when the parameters vector has incorrect length.
UpdateParameters(T)
Updates the parameters of the layer.
public override void UpdateParameters(T learningRate)
Parameters
learningRateTThe learning rate for parameter updates.
Remarks
This method is empty in the current implementation as the layer does not have traditional trainable parameters updated through gradient descent. Instead, learning occurs in the Learn method which directly updates cell states.
For Beginners: This method is included for compatibility but doesn't do anything in this layer.
The reason this method is empty:
- This layer doesn't use traditional gradient-based parameter updates
- Instead, it learns by directly modifying cell states in the Learn method
- This method is included only to satisfy the requirements of the LayerBase class
Think of it like this: while standard neural network layers learn through small adjustments based on error gradients, this layer learns through its specialized temporal learning algorithm implemented in the Learn method.