2626
2727#include " HelloSPTP.hpp"
2828
29- #include " nupic/algorithms/Anomaly.hpp"
3029#include " nupic/algorithms/TemporalMemory.hpp"
3130#include " nupic/algorithms/SpatialPooler.hpp"
3231#include " nupic/encoders/RandomDistributedScalarEncoder.hpp"
32+ #include " nupic/algorithms/AnomalyLikelihood.hpp"
3333
3434#include " nupic/types/Sdr.hpp"
3535#include " nupic/utils/Random.hpp"
@@ -44,8 +44,7 @@ using Encoder = nupic::encoders::RandomDistributedScalarEncoder;
4444using EncoderParameters = nupic::encoders::RDSE_Parameters;
4545using nupic::algorithms::spatial_pooler::SpatialPooler;
4646using TM = nupic::algorithms::temporal_memory::TemporalMemory;
47- using nupic::algorithms::anomaly::Anomaly;
48- using nupic::algorithms::anomaly::AnomalyMode;
47+ using nupic::algorithms::anomaly::AnomalyLikelihood;
4948
5049
5150// work-load
@@ -63,7 +62,7 @@ Real64 BenchmarkHotgym::run(UInt EPOCHS, bool useSPlocal, bool useSPglobal, bool
6362 std::cout << " EPOCHS = " << EPOCHS << std::endl;
6463
6564
66- // initialize SP, TM, Anomaly, AnomalyLikelihood
65+ // initialize SP, TM, AnomalyLikelihood
6766 tInit.start ();
6867 EncoderParameters encParams;
6968 encParams.sparsity = 0 .2f ; // 20% of the encoding are active bits (1's)
@@ -80,8 +79,7 @@ Real64 BenchmarkHotgym::run(UInt EPOCHS, bool useSPlocal, bool useSPglobal, bool
8079
8180 TM tm (vector<UInt>{COLS}, CELLS);
8281
83- Anomaly an (5 , AnomalyMode::PURE);
84- Anomaly anLikelihood (5 , AnomalyMode::LIKELIHOOD);
82+ AnomalyLikelihood anLikelihood;
8583 tInit.stop ();
8684
8785 // data for processing input
@@ -90,8 +88,7 @@ Real64 BenchmarkHotgym::run(UInt EPOCHS, bool useSPlocal, bool useSPglobal, bool
9088 SDR outSPlocal (spLocal.getColumnDimensions ()); // for SPlocal
9189 SDR outSP (vector<UInt>{COLS});
9290 SDR outTM (spGlobal.getColumnDimensions ());
93- Real res = 0.0 ; // for anomaly:
94- SDR prevPred_ (outTM.dimensions ); // holds T-1 TM.predictive cells
91+ Real an = 0 .0f , anLikely = 0 .0f ; // for anomaly:
9592
9693 // Start a stopwatch timer
9794 printf (" starting: %d iterations." , EPOCHS);
@@ -125,7 +122,6 @@ Real64 BenchmarkHotgym::run(UInt EPOCHS, bool useSPlocal, bool useSPglobal, bool
125122 tSPglob.stop ();
126123 }
127124 outSP = outSPglobal; // toggle if local/global SP is used further down the chain (TM, Anomaly)
128- NTA_CHECK (outSP == outSPglobal);
129125
130126 // TM
131127 if (useTM) {
@@ -138,22 +134,19 @@ Real64 BenchmarkHotgym::run(UInt EPOCHS, bool useSPlocal, bool useSPglobal, bool
138134
139135
140136 // Anomaly (pure x likelihood)
141- tAn.start ();
142- res = an.compute (outSP /* active*/ , prevPred_ /* prev predicted*/ );
143- tAn.stop ();
144-
137+ an = tm.anomaly ;
145138 tAnLikelihood.start ();
146- anLikelihood.compute (outSP /* active */ , prevPred_ /* prev predicted */ );
139+ anLikelihood.anomalyProbability (an); // FIXME AnLikelihood is 0.0, probably not working correctly
147140 tAnLikelihood.stop ();
148141
149- prevPred_ = outTM; // to be used as predicted T-1
150142
151143 // print
152144 if (e == EPOCHS - 1 ) {
153145 tAll.stop ();
154146
155147 cout << " Epoch = " << e << endl;
156- cout << " Anomaly = " << res << endl;
148+ cout << " Anomaly = " << an << endl;
149+ cout << " Anomaly (Likelihood) = " << anLikely << endl;
157150 cout << " SP (g)= " << outSP << endl;
158151 cout << " SP (l)= " << outSPlocal <<endl;
159152 cout << " TM= " << outTM << endl;
@@ -164,7 +157,6 @@ Real64 BenchmarkHotgym::run(UInt EPOCHS, bool useSPlocal, bool useSPglobal, bool
164157 if (useSPlocal) cout << " SP (l):\t " << tSPloc.getElapsed ()*1 .0f << endl;
165158 if (useSPglobal) cout << " SP (g):\t " << tSPglob.getElapsed () << endl;
166159 if (useTM) cout << " TM:\t " << tTM.getElapsed () << endl;
167- cout << " AN:\t " << tAn.getElapsed () << endl;
168160 cout << " AN:\t " << tAnLikelihood.getElapsed () << endl;
169161
170162 // check deterministic SP, TM output
@@ -192,16 +184,16 @@ Real64 BenchmarkHotgym::run(UInt EPOCHS, bool useSPlocal, bool useSPglobal, bool
192184 };
193185 goldTM.setSparse (deterministicTM);
194186
195- const float goldAn = 0 .920001f ;
187+ const float goldAn = 0 .8f ;
196188
197189 if (EPOCHS == 5000 ) { // these hand-written values are only valid for EPOCHS = 5000 (default), but not for debug and custom runs.
198190 NTA_CHECK (input == goldEnc) << " Deterministic output of Encoder failed!\n " << input << " should be:\n " << goldEnc;
199191 NTA_CHECK (outSPglobal == goldSP) << " Deterministic output of SP (g) failed!\n " << outSP << " should be:\n " << goldSP;
200192 NTA_CHECK (outSPlocal == goldSPlocal) << " Deterministic output of SP (l) failed!\n " << outSPlocal << " should be:\n " << goldSPlocal;
201193#ifndef _MSC_VER // FIXME deterministic checks fail on Windows
202194 NTA_CHECK (outTM == goldTM) << " Deterministic output of TM failed!\n " << outTM << " should be:\n " << goldTM;
203- NTA_CHECK (static_cast <UInt>(res *10000 ) == static_cast <UInt>(goldAn *10000 )) // compare to 4 decimal places
204- << " Deterministic output of Anomaly failed! " << res << " should be: " << goldAn;
195+ NTA_CHECK (static_cast <UInt>(an *10000 . 0f ) == static_cast <UInt>(goldAn *10000 . 0f )) // compare to 4 decimal places
196+ << " Deterministic output of Anomaly failed! " << an << " should be: " << goldAn;
205197#endif
206198 }
207199
0 commit comments