Temporal Memory

class TemporalMemory : public nupic::Serializable<TemporalMemoryProto>

Temporal Memory implementation in C++.

Example usage:

SpatialPooler sp(inputDimensions, columnDimensions, <parameters>);
TemporalMemory tm(columnDimensions, <parameters>);

while (true) {
   <get input vector, streaming spatiotemporal information>
   sp.compute(inputVector, learn, activeColumns)
   tm.compute(number of activeColumns, activeColumns, learn)
   <do something with the tm, e.g. classify tm.getActiveCells()>

The public API uses C arrays, not std::vectors, as inputs. C arrays are a good lowest common denominator. You can get a C array from a vector, but you can’t get a vector from a C array without copying it. This is important, for example, when using numpy arrays. The only way to convert a numpy array into a std::vector is to copy it, but you can access a numpy array’s internal C array directly.

Public Functions

TemporalMemory(vector<UInt> columnDimensions, UInt cellsPerColumn = 32, UInt activationThreshold = 13, Permanence initialPermanence = 0.21, Permanence connectedPermanence = 0.50, UInt minThreshold = 10, UInt maxNewSynapseCount = 20, Permanence permanenceIncrement = 0.10, Permanence permanenceDecrement = 0.10, Permanence predictedSegmentDecrement = 0.0, Int seed = 42, UInt maxSegmentsPerCell = 255, UInt maxSynapsesPerSegment = 255, bool checkInputs = true)

Initialize the temporal memory (TM) using the given parameters.


  • columnDimensions: Dimensions of the column space
  • cellsPerColumn: Number of cells per column
  • activationThreshold: If the number of active connected synapses on a segment is at least this threshold, the segment is said to be active.
  • initialPermanence: Initial permanence of a new synapse.
  • connectedPermanence: If the permanence value for a synapse is greater than this value, it is said to be connected.
  • minThreshold: If the number of potential synapses active on a segment is at least this threshold, it is said to be “matching” and is eligible for learning.
  • maxNewSynapseCount: The maximum number of synapses added to a segment during learning.
  • permanenceIncrement: Amount by which permanences of synapses are incremented during learning.
  • permanenceDecrement: Amount by which permanences of synapses are decremented during learning.
  • predictedSegmentDecrement: Amount by which segments are punished for incorrect predictions.
  • seed: Seed for the random number generator.
  • maxSegmentsPerCell: The maximum number of segments per cell.
  • maxSynapsesPerSegment: The maximum number of synapses per segment.
  • checkInputs: Whether to check that the activeColumns are sorted without duplicates. Disable this for a small speed boost.

predictedSegmentDecrement: A good value is just a bit larger than (the column-level sparsity * permanenceIncrement). So, if column-level sparsity is 2% and permanenceIncrement is 0.01, this parameter should be something like 4% * 0.01 = 0.0004).

virtual UInt version() const

Get the version number of for the TM implementation.

Integer version number.

void seed_(UInt64 seed)

This only updates _rng to a new Random using seed.

Integer version number.

virtual void reset()

Indicates the start of a new sequence.

Resets sequence state of the TM.

void activateCells(size_t activeColumnsSize, const UInt activeColumns[], bool learn = true)

Calculate the active cells, using the current active columns and dendrite segments.

Grow and reinforce synapses.

  • activeColumnsSize: Size of activeColumns.
  • activeColumns: A sorted list of active column indices.
  • learn: If true, reinforce / punish / grow synapses.

void activateDendrites(bool learn = true)

Calculate dendrite segment activity, using the current active cells.

  • learn: If true, segment activations will be recorded. This information is used during segment cleanup.

virtual void compute(size_t activeColumnsSize, const UInt activeColumns[], bool learn = true)

Perform one time step of the Temporal Memory algorithm.

This method calls activateCells, then calls activateDendrites. Using the TemporalMemory via its compute method ensures that you’ll always be able to call getPredictiveCells to get predictions for the next time step.

  • activeColumnsSize: Number of active columns.
  • activeColumns: Sorted list of indices of active columns.
  • learn: Whether or not learning is enabled.

Segment createSegment(CellIdx cell)

Create a segment on the specified cell.

This method calls createSegment on the underlying connections, and it does some extra bookkeeping. Unit tests should call this method, and not connections.createSegment().

Segment The created segment.
  • cell: Cell to add a segment to.

vector<CellIdx> cellsForColumn(Int column)

Returns the indices of cells that belong to a column.

(vector<CellIdx>) Cell indices
  • column: Column index

UInt numberOfCells(void)

Returns the number of cells in this layer.

(int) Number of cells

vector<CellIdx> getActiveCells() const

Returns the indices of the active cells.

(std::vector<CellIdx>) Vector of indices of active cells.

vector<CellIdx> getPredictiveCells() const

Returns the indices of the predictive cells.

(std::vector<CellIdx>) Vector of indices of predictive cells.

vector<CellIdx> getWinnerCells() const

Returns the indices of the winner cells.

(std::vector<CellIdx>) Vector of indices of winner cells.

vector<UInt> getColumnDimensions() const

Returns the dimensions of the columns in the region.

Integer number of column dimension

UInt numberOfColumns() const

Returns the total number of columns.

Integer number of column numbers

UInt getCellsPerColumn() const

Returns the number of cells per column.

Integer number of cells per column

UInt getActivationThreshold() const

Returns the activation threshold.

Integer number of the activation threshold

Permanence getInitialPermanence() const

Returns the initial permanence.

Initial permanence

Permanence getConnectedPermanence() const

Returns the connected permanance.

Returns the connected permanance

UInt getMinThreshold() const

Returns the minimum threshold.

Integer number of minimum threshold

UInt getMaxNewSynapseCount() const

Returns the maximum number of synapses that can be added to a segment in a single time step.

Integer number of maximum new synapse count

bool getCheckInputs() const

Get and set the checkInputs parameter.

Permanence getPermanenceIncrement() const

Returns the permanence increment.

Returns the Permanence increment

Permanence getPermanenceDecrement() const

Returns the permanence decrement.

Returns the Permanence decrement

Permanence getPredictedSegmentDecrement() const

Returns the predicted Segment decrement.

Returns the segment decrement

UInt getMaxSegmentsPerCell() const

Returns the maxSegmentsPerCell.

Max segments per cell

UInt getMaxSynapsesPerSegment() const

Returns the maxSynapsesPerSegment.

Max synapses per segment

bool _validateCell(CellIdx cell)

Raises an error if cell index is invalid.

  • cell: Cell index

virtual void save(ostream &outStream) const

Save (serialize) the current state of the spatial pooler to the specified file.

  • fd: A valid file descriptor.

virtual void load(istream &inStream)

Load (deserialize) and initialize the spatial pooler from the specified input stream.

  • inStream: A valid istream.

virtual UInt persistentSize() const

Returns the number of bytes that a save operation would result in.

Note: this method is currently somewhat inefficient as it just does a full save into an ostream and counts the resulting size.

Integer number of bytes

void printParameters()

Print the main TM creation parameters.

Int columnForCell(CellIdx cell)

Returns the index of the column that a cell belongs to.

(int) Column index
  • cell: Cell index

void printState(vector<UInt> &state)

Print the given UInt array in a nice format.

void printState(vector<Real> &state)

Print the given Real array in a nice format.