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

TM Connections integration #537

Merged
merged 44 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
103e8fc
destroySynapse: const arg
breznak Jul 1, 2019
b6a103a
TM: fix when custom minPermanence
breznak Jul 1, 2019
cc476a9
COnnections: adaptSegment: deduplicate code for timeseries
breznak Jul 2, 2019
21c315c
Connections: adaptSegment fix hardcoded minPermanence
breznak Jul 2, 2019
cdcf5df
TM:adaptSegment make similar to connections'
breznak Jul 2, 2019
dd45567
TM:adaptSegment uses connections', WIP
breznak Jul 2, 2019
34401b4
Connections:adaptSegment allow to prune zero synapses
breznak Jul 2, 2019
8607710
TM: adaptSegment use SDR for prevActiveCells to match Connections
breznak Jul 2, 2019
f6f029e
Connections:adaptSegment remove empty segments
breznak Jul 2, 2019
14871c9
SegmentData has simple default constructor
breznak Jul 2, 2019
a25040c
Connections:createSegment can check for maxSegmentsPerCell
breznak Jul 2, 2019
9648ae0
TM: move lastUsedIterationForSegment to SegmentData.lastUsed
breznak Jul 2, 2019
e20c267
TM uses Connections' createSegment
breznak Jul 2, 2019
24b2c36
Connections: add const + fix tests
breznak Jul 2, 2019
abb8f96
comment
breznak Jul 2, 2019
cbf0d38
Hotgym: print connections stats for SP, TM
breznak Jul 3, 2019
dac487a
Connections: print stats on usage of pruned Segs, Syns
breznak Jul 3, 2019
47eb418
Hotgym: add Metrics for SDRs from SP, TM, Input
breznak Jul 3, 2019
cccc3e5
Connection:createSegment fix check for maxSegmentsPerCell
breznak Jul 5, 2019
8a1e871
COnnections:createSegment make sort deterministic
breznak Jul 5, 2019
e8fb520
Connections: make sort statements deterministic
breznak Jul 5, 2019
37e7a13
COnnections: make createSegment maxSegments default to disabled
breznak Jul 5, 2019
023ed97
Merge branch 'master_community' into tm_conn
breznak Jul 6, 2019
629db2f
Merge branch 'master_community' into tm_conn
breznak Jul 6, 2019
d7ba1a9
Merge branch 'master_community' into tm_conn
breznak Jul 9, 2019
60a384e
TM: optimize growSynapses
breznak Jul 9, 2019
0188242
Merge branch 'master_community' into tm_conn
breznak Jul 10, 2019
c95c177
Connections: guard adaptSegment remove segment with pruneZeroSynapses
breznak Jul 10, 2019
4672daf
Connections: move iteration from TM
breznak Jul 10, 2019
95b8b6d
Conn: segment ordinals are never same for a != b
breznak Jul 10, 2019
b5498f3
COnnections: prune segment if it never can connect again
breznak Jul 10, 2019
6e07f06
TM code comments
breznak Jul 10, 2019
184ad2c
TM:getLeastUsedCells improve doc
breznak Jul 11, 2019
36ed0ca
TM: add explanation for synapses only on new, unsynapsed segments
breznak Jul 12, 2019
be38786
Merge branch 'master_community' into tm_conn
breznak Jul 12, 2019
d20c9e8
Merge branch 'master' into tm_conn
breznak Jul 13, 2019
2f16fb3
Merge branch 'master_community' into tm_conn
breznak Jul 13, 2019
5fe3697
Connections: some more const
breznak Jul 13, 2019
425f12c
Merge branch 'tm_conn' of https://github.com/htm-community/nupic.core…
breznak Jul 13, 2019
d3186ed
update determ output results
breznak Jul 14, 2019
fc38d89
Conn: synapseOrdinals_ moved to SynapseData
breznak Jul 14, 2019
5b368d8
Conn: move segment ordinals (id) to SegmentData
breznak Jul 13, 2019
065b467
Merge branch 'master_community' into tm_conn
breznak Jul 18, 2019
24983cf
fixup Connections:destroySegment use find
breznak Jul 18, 2019
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
28 changes: 19 additions & 9 deletions bindings/py/cpp_src/bindings/algorithms/py_Connections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ R"(Compatibility Warning: This classes API is unstable and may change without wa
[](const Connections &self) { return self.getConnectedThreshold(); });

py_Connections.def("createSegment", &Connections::createSegment,
py::arg("cell"));
py::arg("cell"),
py::arg("maxSegmentsPerCell") = 0
);

py_Connections.def("destroySegment", &Connections::destroySegment);

py_Connections.def("iteration", &Connections::iteration);

py_Connections.def("createSynapse", &Connections::createSynapse,
py::arg("segment"),
py::arg("presynaticCell"),
Expand Down Expand Up @@ -93,24 +97,26 @@ R"(Compatibility Warning: This classes API is unstable and may change without wa
py_Connections.def("reset", &Connections::reset);

py_Connections.def("computeActivity",
[](Connections &self, SDR &activePresynapticCells) {
[](Connections &self, SDR &activePresynapticCells, bool learn=true) {
// Allocate buffer to return & make a python destructor object for it.
auto activeConnectedSynapses =
new std::vector<SynapseIdx>( self.segmentFlatListLength(), 0u );
auto destructor = py::capsule( activeConnectedSynapses,
[](void *dataPtr) {
delete reinterpret_cast<std::vector<SynapseIdx>*>(dataPtr); });
// Call the C++ method.
self.computeActivity(*activeConnectedSynapses, activePresynapticCells.getSparse());
// Wrap vector in numpy array.

// Call the C++ method.
self.computeActivity(*activeConnectedSynapses, activePresynapticCells.getSparse(), learn);

// Wrap vector in numpy array.
return py::array(activeConnectedSynapses->size(),
activeConnectedSynapses->data(),
destructor);
},
R"(Returns numActiveConnectedSynapsesForSegment)");

py_Connections.def("computeActivityFull",
[](Connections &self, SDR &activePresynapticCells) {
[](Connections &self, SDR &activePresynapticCells, bool learn=true) {
// Allocate buffer to return & make a python destructor object for it.
auto activeConnectedSynapses =
new std::vector<SynapseIdx>( self.segmentFlatListLength(), 0u );
Expand All @@ -123,9 +129,13 @@ R"(Returns numActiveConnectedSynapsesForSegment)");
auto potentialDestructor = py::capsule( activePotentialSynapses,
[](void *dataPtr) {
delete reinterpret_cast<std::vector<SynapseIdx>*>(dataPtr); });
// Call the C++ method.
self.computeActivity(*activeConnectedSynapses, *activePotentialSynapses,
activePresynapticCells.getSparse());

// Call the C++ method.
self.computeActivity(*activeConnectedSynapses,
*activePotentialSynapses,
activePresynapticCells.getSparse(),
learn);

// Wrap vector in numpy array.
return py::make_tuple(
py::array(activeConnectedSynapses->size(),
Expand Down
27 changes: 24 additions & 3 deletions src/examples/hotgym/HelloSPTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "htm/types/Sdr.hpp"
#include "htm/utils/Random.hpp"
#include "htm/utils/MovingAverage.hpp"
#include "htm/utils/SdrMetrics.hpp"

namespace examples {

Expand Down Expand Up @@ -79,6 +80,13 @@ Real64 BenchmarkHotgym::run(UInt EPOCHS, bool useSPlocal, bool useSPglobal, bool
SDR outTM(spGlobal.getColumnDimensions());
Real an = 0.0f, anLikely = 0.0f; //for anomaly:
MovingAverage avgAnom10(1000); //chose the window large enough so there's (some) periodicity in the patter, so TM can learn something

//metrics
Metrics statsInput(input, 1000);
Metrics statsSPlocal(outSPlocal, 1000);
Metrics statsSPglobal(outSPglobal, 1000);
Metrics statsTM(outTM, 1000);

/*
* For example: fn = sin(x) -> periodic >= 2Pi ~ 6.3 && x+=0.01 -> 630 steps to 1st period -> window >= 630
*/
Expand Down Expand Up @@ -147,13 +155,26 @@ Real64 BenchmarkHotgym::run(UInt EPOCHS, bool useSPlocal, bool useSPglobal, bool
if (e == EPOCHS - 1) {
tAll.stop();

//print connections stats
cout << "\nInput :\n" << statsInput
<< "\nSP(local) " << spLocal.connections
<< "\nSP(local) " << statsSPlocal
<< "\nSP(global) " << spGlobal.connections
<< "\nSP(global) " << statsSPglobal
<< "\nTM " << tm.connections
<< "\nTM " << statsTM
<< "\n";

// output values
cout << "Epoch = " << e << endl;
cout << "Anomaly = " << an << endl;
cout << "Anomaly (avg) = " << avgAnom10.getCurrentAvg() << endl;
cout << "Anomaly (Likelihood) = " << anLikely << endl;
cout << "SP (g)= " << outSP << endl;
cout << "SP (l)= " << outSPlocal <<endl;
cout << "TM= " << outTM << endl;

//timers
cout << "==============TIMERS============" << endl;
cout << "Init:\t" << tInit.getElapsed() << endl;
cout << "Random:\t" << tRng.getElapsed() << endl;
Expand Down Expand Up @@ -184,12 +205,12 @@ Real64 BenchmarkHotgym::run(UInt EPOCHS, bool useSPlocal, bool useSPglobal, bool

SDR goldTM({COLS});
const SDR_sparse_t deterministicTM{
51, 62, 72, 77, 102, 155, 287, 306, 337, 340, 370, 493, 542, 952, 1089, 1110, 1115, 1193, 1463, 1488, 1507, 1518, 1547, 1626, 1668, 1694, 1781, 1803, 1805, 1827, 1841, 1858,1859, 1860, 1861, 1862, 1878, 1881, 1915, 1918, 1923, 1929, 1933, 1939, 1941, 1953, 1955, 1956, 1958, 1961, 1965, 1968, 1975, 1976, 1980, 1981, 1985, 1986, 1987, 1991, 1992, 1994, 1997, 2002, 2006, 2008, 2012, 2013, 2040, 2042
62, 77, 85, 322, 340, 432, 952, 1120, 1488, 1502, 1512, 1518, 1547, 1627, 1633, 1668, 1727, 1729, 1797, 1803, 1805, 1812, 1858, 1859, 1896, 1918, 1923, 1925, 1929, 1931, 1939, 1941, 1942, 1944, 1950, 1953, 1955, 1956, 1965, 1966, 1967, 1968, 1974, 1980, 1987, 1996, 2006, 2008, 2011, 2027, 2030, 2042, 2046
};
goldTM.setSparse(deterministicTM);

const float goldAn = 0.745098f;
const float goldAnAvg = 0.408286f;
const float goldAn = 0.627451f;
const float goldAnAvg = 0.407265f;

if(EPOCHS == 5000) { //these hand-written values are only valid for EPOCHS = 5000 (default), but not for debug and custom runs.
NTA_CHECK(input == goldEnc) << "Deterministic output of Encoder failed!\n" << input << "should be:\n" << goldEnc;
Expand Down
Loading