Skip to content

Commit 90971c1

Browse files
committed
TM getLeastUsedCells_() return 1st least used cell
from those "least". Not random. Slight optimization
1 parent 491598d commit 90971c1

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

src/examples/hello/HelloSPTP.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,12 @@ EPOCHS = 2; // make test faster in Debug
213213

214214
SDR goldTM({COLS});
215215
const SDR_sparse_t deterministicTM{
216-
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
216+
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
217217
};
218218
goldTM.setSparse(deterministicTM);
219219

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

223223
#ifdef _ARCH_DETERMINISTIC
224224
if(e+1 == 5000) {

src/htm/algorithms/TemporalMemory.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,16 @@ void TemporalMemory::initialize(
141141
reset();
142142
}
143143

144-
CellIdx TemporalMemory::getLeastUsedCell_(const CellIdx column) {
144+
CellIdx TemporalMemory::getLeastUsedCell_(const CellIdx column) const {
145145
if(cellsPerColumn_ == 1) return column;
146146

147147
vector<CellIdx> cells = cellsForColumn(column);
148-
149-
//TODO: decide if we need to choose randomly from the "least used" cells, or if 1st is fine.
150-
//In that case the line below is not needed, and this method can become const, deterministic results in tests need to be updated
151-
//un/comment line below:
152-
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
148+
//Note: from the found "least used cells" (if there are more), choose just 1st, not randomly
149+
// or un/comment line below:
150+
//rng_.shuffle(cells.begin(), cells.end()); //as min_element selects 1st minimal element, and we want to randomly choose 1 from the minimals.
153151

154152
const auto compareByNumSegments = [&](const CellIdx a, const CellIdx b) {
155-
if(connections.numSegments(a) == connections.numSegments(b))
156-
return a < b; //TODO rm?
153+
if(connections.numSegments(a) == connections.numSegments(b)) return a < b; //TODO rm?
157154
else return connections.numSegments(a) < connections.numSegments(b);
158155
};
159156
return *std::min_element(cells.begin(), cells.end(), compareByNumSegments);
@@ -171,12 +168,13 @@ void TemporalMemory::growSynapses_(
171168
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
172169

173170
// Pick nActual cells randomly.
174-
rng_.shuffle(candidates.begin(), candidates.end()); //TODO use sample()
171+
rng_.shuffle(candidates.begin(), candidates.end());
175172
for (const auto syn : candidates) {
176173
// #COND: this loop finishes two folds: a) we ran out of candidates (above), b) we grew the desired number of new synapses (below)
177174
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.
178175
connections_.createSynapse(segment, syn, initialPermanence_); //TODO createSynapse consider creating a vector of new synapses at once?
179176
}
177+
NTA_ASSERT(connections.numSynapses(segment) == nActual);
180178
}
181179

182180

src/htm/algorithms/TemporalMemory.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ class TemporalMemory : public Serializable
660660
const SynapseIdx nDesiredNewSynapses,
661661
const vector<CellIdx> &prevWinnerCells);
662662

663-
CellIdx getLeastUsedCell_(const CellIdx column);
663+
CellIdx getLeastUsedCell_(const CellIdx column) const;
664664

665665
void calculateAnomalyScore_(const SDR &activeColumns);
666666

0 commit comments

Comments
 (0)