Temporal Memory

class nupic::algorithms::temporal_memory::TemporalMemory

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.

Inherits from nupic::Serializable< TemporalMemoryProto >

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)

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

Notes:

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.

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.

Return
Integer version number.

void seed_(UInt64 seed)

This only updates _rng to a new Random using seed.

Return
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.

Parameters
  • 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.

Parameters
  • 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.

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

vector<CellIdx> cellsForColumn(Int column)

Returns the indices of cells that belong to a column.

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

UInt numberOfCells(void)

Returns the number of cells in this layer.

Return
(int) Number of cells

vector<CellIdx> getActiveCells() const

Returns the indices of the active cells.

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

vector<CellIdx> getPredictiveCells() const

Returns the indices of the predictive cells.

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

vector<CellIdx> getWinnerCells() const

Returns the indices of the winner cells.

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

vector<CellIdx> getMatchingCells() const

Returns the indices of the matching cells.

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

vector<UInt> getColumnDimensions() const

Returns the dimensions of the columns in the region.

Return
Integer number of column dimension

UInt numberOfColumns() const

Returns the total number of columns.

Return
Integer number of column numbers

UInt getCellsPerColumn() const

Returns the number of cells per column.

Return
Integer number of cells per column

UInt getActivationThreshold() const

Returns the activation threshold.

Return
Integer number of the activation threshold

Permanence getInitialPermanence() const

Returns the initial permanence.

Return
Initial permanence

Permanence getConnectedPermanence() const

Returns the connected permanance.

Return
Returns the connected permanance

UInt getMinThreshold() const

Returns the minimum threshold.

Return
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.

Return
Integer number of maximum new synapse count

Permanence getPermanenceIncrement() const

Returns the permanence increment.

Return
Returns the Permanence increment

Permanence getPermanenceDecrement() const

Returns the permanence decrement.

Return
Returns the Permanence decrement

Permanence getPredictedSegmentDecrement() const

Returns the predicted Segment decrement.

Return
Returns the segment decrement

bool _validateCell(CellIdx cell)

Raises an error if cell index is invalid.

Parameters
  • cell: Cell index

virtual void save(ostream &outStream) const

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

Parameters
  • fd: A valid file descriptor.

virtual void load(istream &inStream)

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

Parameters
  • 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.

Return
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.

Return
(int) Column index
Parameters
  • 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.