Skip to content

Commit

Permalink
TM getLeastUsedCells_() return 1st least used cell
Browse files Browse the repository at this point in the history
from those "least". Not random. Slight optimization
  • Loading branch information
breznak committed Jun 4, 2020
1 parent 491598d commit 90971c1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/examples/hello/HelloSPTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ EPOCHS = 2; // make test faster in Debug

SDR goldTM({COLS});
const SDR_sparse_t deterministicTM{
72, 85, 102, 114, 122, 126, 287, 308, 337, 339, 542, 920, 939, 952, 1268, 1507, 1508, 1518, 1546, 1547, 1626, 1627, 1633, 1668, 1727, 1804, 1805, 1827, 1832, 1844, 1859, 1862, 1918, 1920, 1924, 1931, 1933, 1945, 1961, 1965, 1966, 1968, 1970, 1973, 1975, 1976, 1977, 1979, 1986, 1987, 1991, 1992, 1996, 1998, 2002, 2006, 2008, 2012, 2042, 2045
36, 85, 118, 263, 287, 303, 308, 322, 336, 337, 339, 370, 432, 1115, 1147, 1214, 1508, 1512, 1518, 1523, 1626, 1668, 1691, 1694, 1729, 1781, 1797, 1798, 1803, 1827, 1832, 1844, 1858, 1859, 1860, 1861, 1862, 1917, 1929, 1936, 1939, 1941, 1943, 1947, 1950, 1953, 1956, 1958, 1964, 1965, 1967, 1971, 1973, 1976, 1984, 1985, 1987, 1994, 1996, 1997, 1998, 1999, 2002,2006, 2012, 2027, 2040, 2042
};
goldTM.setSparse(deterministicTM);

const float goldAn = 0.637255f; //Note: this value is for a (randomly picked) datapoint, it does not have to improve (decrease) with better algorithms
const float goldAnAvg = 0.40804f; // ...the averaged value, on the other hand, should improve/decrease.
const float goldAn = 0.745098f; //Note: this value is for a (randomly picked) datapoint, it does not have to improve (decrease) with better algorithms
const float goldAnAvg = 0.408207f; // ...the averaged value, on the other hand, should improve/decrease.

#ifdef _ARCH_DETERMINISTIC
if(e+1 == 5000) {
Expand Down
16 changes: 7 additions & 9 deletions src/htm/algorithms/TemporalMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,16 @@ void TemporalMemory::initialize(
reset();
}

CellIdx TemporalMemory::getLeastUsedCell_(const CellIdx column) {
CellIdx TemporalMemory::getLeastUsedCell_(const CellIdx column) const {
if(cellsPerColumn_ == 1) return column;

vector<CellIdx> cells = cellsForColumn(column);

//TODO: decide if we need to choose randomly from the "least used" cells, or if 1st is fine.
//In that case the line below is not needed, and this method can become const, deterministic results in tests need to be updated
//un/comment line below:
rng_.shuffle(cells.begin(), cells.end()); //as min_element selects 1st minimal element, and we want to randomly choose 1 from the minimals. //TODO return the 1st
//Note: from the found "least used cells" (if there are more), choose just 1st, not randomly
// or un/comment line below:
//rng_.shuffle(cells.begin(), cells.end()); //as min_element selects 1st minimal element, and we want to randomly choose 1 from the minimals.

const auto compareByNumSegments = [&](const CellIdx a, const CellIdx b) {
if(connections.numSegments(a) == connections.numSegments(b))
return a < b; //TODO rm?
if(connections.numSegments(a) == connections.numSegments(b)) return a < b; //TODO rm?
else return connections.numSegments(a) < connections.numSegments(b);
};
return *std::min_element(cells.begin(), cells.end(), compareByNumSegments);
Expand All @@ -171,12 +168,13 @@ void TemporalMemory::growSynapses_(
const size_t nActual = std::min(static_cast<size_t>(nDesiredNewSynapses) + (size_t)connections.numSynapses(segment), (size_t)maxSynapsesPerSegment_); //even with the new additions, synapses fit to segment's limit

// Pick nActual cells randomly.
rng_.shuffle(candidates.begin(), candidates.end()); //TODO use sample()
rng_.shuffle(candidates.begin(), candidates.end());
for (const auto syn : candidates) {
// #COND: this loop finishes two folds: a) we ran out of candidates (above), b) we grew the desired number of new synapses (below)
if(connections.numSynapses(segment) == nActual) break; //this break is also used because conn.createSynapse() can "exit early" if a syn already exists, this IF handles that case too.
connections_.createSynapse(segment, syn, initialPermanence_); //TODO createSynapse consider creating a vector of new synapses at once?
}
NTA_ASSERT(connections.numSynapses(segment) == nActual);
}


Expand Down
2 changes: 1 addition & 1 deletion src/htm/algorithms/TemporalMemory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ class TemporalMemory : public Serializable
const SynapseIdx nDesiredNewSynapses,
const vector<CellIdx> &prevWinnerCells);

CellIdx getLeastUsedCell_(const CellIdx column);
CellIdx getLeastUsedCell_(const CellIdx column) const;

void calculateAnomalyScore_(const SDR &activeColumns);

Expand Down

0 comments on commit 90971c1

Please sign in to comment.