Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename TemporalMemory extra to externalPredictiveInputs #519

Merged
merged 3 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions bindings/py/cpp_src/bindings/algorithms/py_TemporalMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ Argument checkInputs
Whether to check that the activeColumns are sorted without
duplicates. Disable this for a small speed boost.

Argument extra
Argument externalPredictiveInputs
Number of external predictive inputs. These inputs are used in addition to
the cells which are part of this TemporalMemory. The TemporalMemory
requires all external inputs be identified by an index in the
range [0, extra).
range [0, externalPredictiveInputs).
)"
, py::arg("columnDimensions")
, py::arg("cellsPerColumn") = 32
Expand All @@ -141,7 +141,7 @@ Argument extra
, py::arg("maxSegmentsPerCell") = 255
, py::arg("maxSynapsesPerSegment") = 255
, py::arg("checkInputs") = true
, py::arg("extra") = 0u
, py::arg("externalPredictiveInputs") = 0u
);

py_HTM.def("printParameters",
Expand Down Expand Up @@ -197,8 +197,8 @@ dendrite segments. Grow and reinforce synapses.)"
py::arg("learn") = true);

py_HTM.def("compute", [](HTM_t& self, const SDR &activeColumns, bool learn,
const SDR &extraActive, const SDR &extraWinners)
{ self.compute(activeColumns, learn, extraActive, extraWinners); },
const SDR &externalPredictiveInputsActive, const SDR &externalPredictiveInputsWinners)
{ self.compute(activeColumns, learn, externalPredictiveInputsActive, externalPredictiveInputsWinners); },
R"(Perform one time step of the Temporal Memory algorithm.

This method calls activateDendrites, then calls activateCells. Using
Expand All @@ -211,20 +211,20 @@ Argument activeColumns
Argument learn
Whether or not learning is enabled.

Argument extraActive
Argument externalPredictiveInputsActive
(optional) SDR of active external predictive inputs.
External inputs must be cell indexes in the range [0, extra).
TM must be set up with the 'extra' constructor parameter for this use.
External inputs must be cell indexes in the range [0, externalPredictiveInputs).
TM must be set up with the 'externalPredictiveInputs' constructor parameter for this use.

Argument extraWinners
Argument externalPredictiveInputsWinners
(optional) SDR of winning external predictive inputs. When learning, only these
inputs are considered active.
ExtraWinners must be a subset of extraActive.
externalPredictiveInputsWinners must be a subset of externalPredictiveInputsActive.
)",
py::arg("activeColumns"),
py::arg("learn") = true,
py::arg("extraActive"),
py::arg("extraWinners"));
py::arg("externalPredictiveInputsActive"),
py::arg("externalPredictiveInputsWinners"));

py_HTM.def("reset", &HTM_t::reset,
R"(Indicates the start of a new sequence.
Expand All @@ -240,14 +240,14 @@ Resets sequence state of the TM.)");
});

py_HTM.def("activateDendrites", [](HTM_t &self, bool learn) {
SDR extra({ self.extra });
self.activateDendrites(learn, extra, extra);
SDR externalPredictiveInputs({ self.externalPredictiveInputs });
self.activateDendrites(learn, externalPredictiveInputs, externalPredictiveInputs);
},
py::arg("learn"));

py_HTM.def("activateDendrites",
[](HTM_t &self, bool learn,const SDR &extraActive, const SDR &extraWinners)
{ self.activateDendrites(learn, extraActive, extraWinners); },
[](HTM_t &self, bool learn,const SDR &externalPredictiveInputsActive, const SDR &externalPredictiveInputsWinners)
{ self.activateDendrites(learn, externalPredictiveInputsActive, externalPredictiveInputsWinners); },
R"(Calculate dendrite segment activity, using the current active cells. Call
this method before calling getPredictiveCells, getActiveSegments, or
getMatchingSegments. In each time step, only the first call to this
Expand All @@ -258,18 +258,18 @@ Argument learn
If true, segment activations will be recorded. This information is
used during segment cleanup.

Argument extraActive
Argument externalPredictiveInputsActive
(optional) SDR of active external predictive inputs.

Argument extraWinners
Argument externalPredictiveInputsWinners
(optional) SDR of winning external predictive inputs. When learning, only
these inputs are considered active.
ExtraWinners must be a subset of extraActive.
externalPredictiveInputsWinners must be a subset of externalPredictiveInputsActive.

See TM.compute() for details of the parameters.)",
py::arg("learn"),
py::arg("extraActive"),
py::arg("extraWinners"));
py::arg("externalPredictiveInputsActive"),
py::arg("externalPredictiveInputsWinners"));

py_HTM.def("getPredictiveCells", [](const HTM_t& self)
{ return self.getPredictiveCells();},
Expand Down Expand Up @@ -329,10 +329,11 @@ R"(Returns the total number of mini-columns.)");
py_HTM.def_property_readonly("connections", [](const HTM_t &self)
{ return self.connections; },
R"(Internal Connections object. Danger!
Modifying this may detrimentially effect the TM.
Modifying this may detrimentally effect the TM.
The Connections class API is subject to change.)");

py_HTM.def_property_readonly("extra", [](const HTM_t &self) { return self.extra; },
py_HTM.def_property_readonly("externalPredictiveInputs", [](const HTM_t &self)
{ return self.externalPredictiveInputs; },
R"()");

py_HTM.def_property_readonly("anomaly", [](const HTM_t &self) { return self.anomaly; },
Expand Down
50 changes: 25 additions & 25 deletions src/htm/algorithms/TemporalMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ TemporalMemory::TemporalMemory(
SegmentIdx maxSegmentsPerCell,
SynapseIdx maxSynapsesPerSegment,
bool checkInputs,
UInt extra) {
UInt externalPredictiveInputs) {
initialize(columnDimensions, cellsPerColumn, activationThreshold,
initialPermanence, connectedPermanence, minThreshold,
maxNewSynapseCount, permanenceIncrement, permanenceDecrement,
predictedSegmentDecrement, seed, maxSegmentsPerCell,
maxSynapsesPerSegment, checkInputs, extra);
maxSynapsesPerSegment, checkInputs, externalPredictiveInputs);
}

TemporalMemory::~TemporalMemory() {}
Expand All @@ -96,7 +96,7 @@ void TemporalMemory::initialize(
SegmentIdx maxSegmentsPerCell,
SynapseIdx maxSynapsesPerSegment,
bool checkInputs,
UInt extra) {
UInt externalPredictiveInputs) {

// Validate all input parameters
NTA_CHECK(columnDimensions.size() > 0) << "Number of column dimensions must be greater than 0";
Expand Down Expand Up @@ -128,7 +128,7 @@ void TemporalMemory::initialize(
permanenceIncrement_ = permanenceIncrement;
permanenceDecrement_ = permanenceDecrement;
predictedSegmentDecrement_ = predictedSegmentDecrement;
extra_ = extra;
externalPredictiveInputs_ = externalPredictiveInputs;

// Initialize member variables
connections = Connections(static_cast<CellIdx>(numberOfColumns() * cellsPerColumn_), connectedPermanence_);
Expand Down Expand Up @@ -421,7 +421,7 @@ void TemporalMemory::activateCells(const SDR &activeColumns, const bool learn) {
}
auto &sparse = activeColumns.getSparse();

vector<bool> prevActiveCellsDense(numberOfCells() + extra_, false);
vector<bool> prevActiveCellsDense(numberOfCells() + externalPredictiveInputs_, false);
for (CellIdx cell : activeCells_) {
prevActiveCellsDense[cell] = true;
}
Expand Down Expand Up @@ -480,36 +480,36 @@ void TemporalMemory::activateCells(const SDR &activeColumns, const bool learn) {


void TemporalMemory::activateDendrites(const bool learn,
const SDR &extraActive,
const SDR &extraWinners)
const SDR &externalPredictiveInputsActive,
const SDR &externalPredictiveInputsWinners)
{
if( extra_ > 0 )
if( externalPredictiveInputs_ > 0 )
{
NTA_CHECK( extraActive.size == extra_ );
NTA_CHECK( extraWinners.size == extra_ );
NTA_CHECK( extraActive.dimensions == extraWinners.dimensions);
NTA_CHECK( externalPredictiveInputsActive.size == externalPredictiveInputs_ );
NTA_CHECK( externalPredictiveInputsWinners.size == externalPredictiveInputs_ );
NTA_CHECK( externalPredictiveInputsActive.dimensions == externalPredictiveInputsWinners.dimensions);
#ifdef NTA_ASSERTIONS_ON
SDR both(extraActive.dimensions);
both.intersection(extraActive, extraWinners);
NTA_ASSERT(both == extraWinners) << "ExtraWinners must be a subset of ExtraActive";
SDR both(externalPredictiveInputsActive.dimensions);
both.intersection(externalPredictiveInputsActive, externalPredictiveInputsWinners);
NTA_ASSERT(both == externalPredictiveInputsWinners) << "externalPredictiveInputsWinners must be a subset of externalPredictiveInputsActive";
#endif
}
else
{
NTA_CHECK( extraActive.getSum() == 0u && extraWinners.getSum() == 0u )
NTA_CHECK( externalPredictiveInputsActive.getSum() == 0u && externalPredictiveInputsWinners.getSum() == 0u )
<< "External predictive inputs must be declared to TM constructor!";
}


if( segmentsValid_ )
return;

for(const auto &active : extraActive.getSparse()) {
NTA_ASSERT( active < extra_ );
for(const auto &active : externalPredictiveInputsActive.getSparse()) {
NTA_ASSERT( active < externalPredictiveInputs_ );
activeCells_.push_back( static_cast<CellIdx>(active + numberOfCells()) );
}
for(const auto &winner : extraWinners.getSparse()) {
NTA_ASSERT( winner < extra_ );
for(const auto &winner : externalPredictiveInputsWinners.getSparse()) {
NTA_ASSERT( winner < externalPredictiveInputs_ );
winnerCells_.push_back( static_cast<CellIdx>(winner + numberOfCells()) );
}

Expand Down Expand Up @@ -553,10 +553,10 @@ void TemporalMemory::activateDendrites(const bool learn,

void TemporalMemory::compute(const SDR &activeColumns,
const bool learn,
const SDR &extraActive,
const SDR &extraWinners)
const SDR &externalPredictiveInputsActive,
const SDR &externalPredictiveInputsWinners)
{
activateDendrites(learn, extraActive, extraWinners);
activateDendrites(learn, externalPredictiveInputsActive, externalPredictiveInputsWinners);

// Update Anomaly Metric. The anomaly is the percent of active columns that
// were not predicted.
Expand All @@ -569,9 +569,9 @@ void TemporalMemory::compute(const SDR &activeColumns,
}

void TemporalMemory::compute(const SDR &activeColumns, const bool learn) {
SDR extraActive({ extra });
SDR extraWinners({ extra });
compute( activeColumns, learn, extraActive, extraWinners );
SDR externalPredictiveInputsActive({ externalPredictiveInputs_ });
SDR externalPredictiveInputsWinners({ externalPredictiveInputs_ });
compute( activeColumns, learn, externalPredictiveInputsActive, externalPredictiveInputsWinners );
}

void TemporalMemory::reset(void) {
Expand Down
52 changes: 26 additions & 26 deletions src/htm/algorithms/TemporalMemory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ class TemporalMemory : public Serializable
* duplicates. Disable this for a small speed boost.
* DEPRECATED: The SDR class now enforces these properties.
*
* @param extra
* @param externalPredictiveInputs
* Number of external predictive inputs. These inputs are used in addition to
* the cells which are part of this TemporalMemory. The TemporalMemory
* requires all external inputs be identified by an index in the
* range [0, extra).
breznak marked this conversation as resolved.
Show resolved Hide resolved
* range [0, externalPredictiveInputs).
*
* Notes:
*
Expand All @@ -141,7 +141,7 @@ class TemporalMemory : public Serializable
SegmentIdx maxSegmentsPerCell = 255,
SynapseIdx maxSynapsesPerSegment = 255,
bool checkInputs = true,
UInt extra = 0);
UInt externalPredictiveInputs = 0);

virtual void
initialize(
Expand All @@ -159,7 +159,7 @@ class TemporalMemory : public Serializable
SegmentIdx maxSegmentsPerCell = 255,
SynapseIdx maxSynapsesPerSegment = 255,
bool checkInputs = true,
UInt extra = 0);
UInt externalPredictiveInputs = 0);

virtual ~TemporalMemory();

Expand Down Expand Up @@ -204,27 +204,27 @@ class TemporalMemory : public Serializable
* If true, segment activations will be recorded. This information is
* used during segment cleanup.
*
* @param extraActive
* @param externalPredictiveInputsActive
* (optional) SDR of active external predictive inputs. External inputs must be cell
* indexes in the range [0, extra).
* indexes in the range [0, externalPredictiveInputs).
*
* @param extraWinners
* @param externalPredictiveInputsWinners
* (optional) SDR of winning external predictive inputs. When learning, only these
* inputs are considered active.
* ExtraWinners must be a subset of extraActive.
* External inputs must be cell indices in the range [0, extra).
* externalPredictiveInputsWinners must be a subset of externalPredictiveInputsActive.
* External inputs must be cell indices in the range [0, externalPredictiveInputs).
*
* See TM::compute() for details of the parameters.
*
*/
void activateDendrites(const bool learn,
const SDR &extraActive,
const SDR &extraWinners);
const SDR &externalPredictiveInputsActive,
const SDR &externalPredictiveInputsWinners);

inline void activateDendrites(const bool learn = true) {
const SDR extraActive(std::vector<UInt>{ extra });
const SDR extraWinners(std::vector<UInt>{extra });
activateDendrites(learn, extraActive, extraWinners);
const SDR externalPredictiveInputsActive(std::vector<UInt>{ externalPredictiveInputs });
const SDR externalPredictiveInputsWinners(std::vector<UInt>{externalPredictiveInputs });
activateDendrites(learn, externalPredictiveInputsActive, externalPredictiveInputsWinners);
}

/**
Expand All @@ -240,22 +240,22 @@ class TemporalMemory : public Serializable
* @param learn
* Whether or not learning is enabled.
*
* @param extraActive
* @param externalPredictiveInputsActive
* (optional) Vector of active external predictive inputs.
* External inputs must be cell indexes in the range [0, extra).
* TM must be set up with the 'extra' constructor parameter for this use.
* External inputs must be cell indexes in the range [0, externalPredictiveInputs).
* TM must be set up with the 'externalPredictiveInputs' constructor parameter for this use.
*
* @param extraWinners
* @param externalPredictiveInputsWinners
* (optional) Vector of winning external predictive inputs. When learning, only these
* inputs are considered active.
* ExtraWinners must be a subset of extraActive.
* External inputs must be cell indexes in the range [0, extra).
* externalPredictiveInputsWinners must be a subset of externalPredictiveInputsActive.
* External inputs must be cell indexes in the range [0, externalPredictiveInputs).
*
*/
virtual void compute(const SDR &activeColumns,
const bool learn,
const SDR &extraActive,
const SDR &extraWinners);
const SDR &externalPredictiveInputsActive,
const SDR &externalPredictiveInputsWinners);

virtual void compute(const SDR &activeColumns,
const bool learn = true);
Expand Down Expand Up @@ -464,7 +464,7 @@ class TemporalMemory : public Serializable
CEREAL_NVP(permanenceIncrement_),
CEREAL_NVP(permanenceDecrement_),
CEREAL_NVP(predictedSegmentDecrement_),
CEREAL_NVP(extra_),
CEREAL_NVP(externalPredictiveInputs_),
CEREAL_NVP(maxSegmentsPerCell_),
CEREAL_NVP(maxSynapsesPerSegment_),
CEREAL_NVP(iteration_),
Expand Down Expand Up @@ -520,7 +520,7 @@ class TemporalMemory : public Serializable
CEREAL_NVP(permanenceIncrement_),
CEREAL_NVP(permanenceDecrement_),
CEREAL_NVP(predictedSegmentDecrement_),
CEREAL_NVP(extra_),
CEREAL_NVP(externalPredictiveInputs_),
CEREAL_NVP(maxSegmentsPerCell_),
CEREAL_NVP(maxSynapsesPerSegment_),
CEREAL_NVP(iteration_),
Expand Down Expand Up @@ -622,7 +622,7 @@ class TemporalMemory : public Serializable
Permanence permanenceIncrement_;
Permanence permanenceDecrement_;
Permanence predictedSegmentDecrement_;
UInt extra_;
UInt externalPredictiveInputs_;
SegmentIdx maxSegmentsPerCell_;
SynapseIdx maxSynapsesPerSegment_;

Expand All @@ -644,7 +644,7 @@ class TemporalMemory : public Serializable

public:
Connections connections;
const UInt &extra = extra_;
const UInt &externalPredictiveInputs = externalPredictiveInputs_;
/*
* anomaly score computed for the current inputs
* (auto-updates after each call to TM::compute())
Expand Down
Loading