27
27
#include < htm/types/Sdr.hpp>
28
28
#include < htm/types/Serializable.hpp>
29
29
#include < htm/utils/Random.hpp>
30
+ #include < htm/algorithms/AnomalyLikelihood.hpp>
31
+
30
32
#include < vector>
31
33
32
34
@@ -35,6 +37,7 @@ namespace htm {
35
37
using namespace std ;
36
38
using namespace htm ;
37
39
40
+
38
41
/* *
39
42
* Temporal Memory implementation in C++.
40
43
*
@@ -53,6 +56,7 @@ using namespace htm;
53
56
class TemporalMemory : public Serializable
54
57
{
55
58
public:
59
+ enum class ANMode { DISABLED = 0 , RAW = 1 , LIKELIHOOD = 2 , LOGLIKELIHOOD = 3 };
56
60
TemporalMemory ();
57
61
58
62
/* *
@@ -124,6 +128,10 @@ class TemporalMemory : public Serializable
124
128
* TemporalMemory. If this is given (and greater than 0) then the active
125
129
* cells and winner cells of these external inputs must be given to methods
126
130
* TM.compute and TM.activateDendrites
131
+ *
132
+ * @param anomalyMode (optional, default `ANMode::RAW`)from enum ANMode, how is
133
+ * `TM.anomaly` computed. Options ANMode {DISABLED, RAW, LIKELIHOOD, LOGLIKELIHOOD}
134
+ *
127
135
*/
128
136
TemporalMemory (
129
137
vector<CellIdx> columnDimensions,
@@ -140,13 +148,15 @@ class TemporalMemory : public Serializable
140
148
SegmentIdx maxSegmentsPerCell = 255 ,
141
149
SynapseIdx maxSynapsesPerSegment = 255 ,
142
150
bool checkInputs = true ,
143
- UInt externalPredictiveInputs = 0 );
151
+ UInt externalPredictiveInputs = 0 ,
152
+ ANMode anomalyMode = ANMode::RAW
153
+ );
144
154
145
155
virtual void
146
156
initialize (
147
- vector<CellIdx> columnDimensions = {2048 },
148
- CellIdx cellsPerColumn = 32 ,
149
- SynapseIdx activationThreshold = 13 ,
157
+ vector<CellIdx> columnDimensions = {2048 },
158
+ CellIdx cellsPerColumn = 32 ,
159
+ SynapseIdx activationThreshold = 13 ,
150
160
Permanence initialPermanence = 0.21 ,
151
161
Permanence connectedPermanence = 0.50 ,
152
162
SynapseIdx minThreshold = 10 ,
@@ -158,7 +168,9 @@ class TemporalMemory : public Serializable
158
168
SegmentIdx maxSegmentsPerCell = 255 ,
159
169
SynapseIdx maxSynapsesPerSegment = 255 ,
160
170
bool checkInputs = true ,
161
- UInt externalPredictiveInputs = 0 );
171
+ UInt externalPredictiveInputs = 0 ,
172
+ ANMode anomalyMode = ANMode::RAW
173
+ );
162
174
163
175
virtual ~TemporalMemory ();
164
176
@@ -231,6 +243,10 @@ class TemporalMemory : public Serializable
231
243
* the TemporalMemory via its compute method ensures that you'll always
232
244
* be able to call getActiveCells at the end of the time step.
233
245
*
246
+ * Additionaly, this method computes anomaly for `TM.anomaly&`, if you
247
+ * use other learning methods (activateCells(), activateDendrites()) your
248
+ * anomaly scores will be off.
249
+ *
234
250
* @param activeColumns
235
251
* Sorted SDR of active columns.
236
252
*
@@ -467,7 +483,9 @@ class TemporalMemory : public Serializable
467
483
CEREAL_NVP (activeCells_),
468
484
CEREAL_NVP (winnerCells_),
469
485
CEREAL_NVP (segmentsValid_),
470
- CEREAL_NVP (anomaly_),
486
+ CEREAL_NVP (tmAnomaly_.anomaly_ ),
487
+ CEREAL_NVP (tmAnomaly_.mode_ ),
488
+ CEREAL_NVP (tmAnomaly_.anomalyLikelihood_ ),
471
489
CEREAL_NVP (connections));
472
490
473
491
cereal::size_type numActiveSegments = activeSegments_.size ();
@@ -522,7 +540,9 @@ class TemporalMemory : public Serializable
522
540
CEREAL_NVP (activeCells_),
523
541
CEREAL_NVP (winnerCells_),
524
542
CEREAL_NVP (segmentsValid_),
525
- CEREAL_NVP (anomaly_),
543
+ CEREAL_NVP (tmAnomaly_.anomaly_ ),
544
+ CEREAL_NVP (tmAnomaly_.mode_ ),
545
+ CEREAL_NVP (tmAnomaly_.anomalyLikelihood_ ),
526
546
CEREAL_NVP (connections));
527
547
528
548
numActiveConnectedSynapsesForSegment_.assign (connections.segmentFlatListLength (), 0 );
@@ -646,21 +666,34 @@ class TemporalMemory : public Serializable
646
666
vector<SynapseIdx> numActiveConnectedSynapsesForSegment_;
647
667
vector<SynapseIdx> numActivePotentialSynapsesForSegment_;
648
668
649
- Real anomaly_;
650
-
651
669
Random rng_;
652
670
671
+ /* *
672
+ * holds logic and data for TM's anomaly
673
+ */
674
+ struct anomaly_tm {
675
+ protected:
676
+ friend class TemporalMemory ;
677
+ Real anomaly_ = 0 .5f ; // default value
678
+ ANMode mode_ = ANMode::RAW;
679
+ AnomalyLikelihood anomalyLikelihood_; // TODO provide default/customizable params here
680
+ };
681
+
653
682
public:
654
683
Connections connections;
655
684
const UInt &externalPredictiveInputs = externalPredictiveInputs_;
685
+
686
+ anomaly_tm tmAnomaly_;
656
687
/*
657
688
* anomaly score computed for the current inputs
658
689
* (auto-updates after each call to TM::compute())
659
690
*
660
691
* @return a float value from computeRawAnomalyScore()
661
692
* from Anomaly.hpp
662
693
*/
663
- const Real &anomaly = anomaly_;
694
+ const Real &anomaly = tmAnomaly_.anomaly_; // this is position dependant, the struct anomaly_tm must be defined before this use,
695
+ // otherwise this surprisingly compiles, but a call to `tmAnomaly_.anomaly` segfaults!
696
+
664
697
};
665
698
666
699
} // namespace htm
0 commit comments