diff --git a/src/htm/algorithms/TemporalMemory.cpp b/src/htm/algorithms/TemporalMemory.cpp index 38d7b7cb8c..655a3e6314 100644 --- a/src/htm/algorithms/TemporalMemory.cpp +++ b/src/htm/algorithms/TemporalMemory.cpp @@ -506,7 +506,8 @@ void TemporalMemory::compute(const SDR &activeColumns, const Real raw = computeRawAnomalyScore( activeColumns, cellsToColumns( getPredictiveCells() )); - tmAnomaly_.anomaly_ = tmAnomaly_.anomalyLikelihood_.anomalyProbability(raw); + tmAnomaly_.anomaly_ = 1.0f - tmAnomaly_.anomalyLikelihood_.anomalyProbability(raw); // AnomalyLikelihood returs a likelihood of the score (given past scores), + // and we want to detect unlikely scores, aka big changes -> 1.0-likelihood represents "anomaly" } break; case ANMode::LOGLIKELIHOOD: { @@ -515,7 +516,7 @@ void TemporalMemory::compute(const SDR &activeColumns, cellsToColumns( getPredictiveCells() )); const Real like = tmAnomaly_.anomalyLikelihood_.anomalyProbability(raw); const Real log = tmAnomaly_.anomalyLikelihood_.computeLogLikelihood(like); - tmAnomaly_.anomaly_ = log; + tmAnomaly_.anomaly_ = 1.0f - log; } break; // TODO: Update mean & standard deviation of anomaly here. }; diff --git a/src/htm/algorithms/TemporalMemory.hpp b/src/htm/algorithms/TemporalMemory.hpp index 4eedb4f8e2..fa8ab58a26 100644 --- a/src/htm/algorithms/TemporalMemory.hpp +++ b/src/htm/algorithms/TemporalMemory.hpp @@ -688,8 +688,23 @@ class TemporalMemory : public Serializable * anomaly score computed for the current inputs * (auto-updates after each call to TM::compute()) * - * @return a float value from computeRawAnomalyScore() - * from Anomaly.hpp + * Note, if you use manual learning methods for TM (activateDendrites, etc.) the scores + * will not be in sync (works only if `TM.compute` is used). + * + * Options `ANMode` {DISABLED, RAW, LIKELIHOOD, LOGLIKELIHOOD} passed during constructor + * of this class affect the computation of anomaly here. + * - "DISABLED": always returns 0.5f, no computation is performed (use if you don't care for anomaly + * and need maximum speed). + * - "RAW": return value from `computeRawAnomalyScore()` from `Anomaly.hpp`, default. + * - "LIKELIHOOD": Compute the probability that the current anomaly score represents + * an anomaly given the historical distribution of anomaly scores. The closer + * the number is to 1, the higher the chance it is an anomaly. + * Computed from `anomalyProbability()` in `AnomalyLikelihood.hpp`. + * - "LOGLIKELIHOOD": Compute a log scale representation of the likelihood value. This mode performs + * `computeLogLikelihood()` from `AnomalyLikelihood.hpp` + * + * @return a float value based on `ANMode` argument used in constructor of the TM class. + * By default return value from `computeRawAnomalyScore()` from `Anomaly.hpp` */ const Real &anomaly = tmAnomaly_.anomaly_; //this is position dependant, the struct anomaly_tm must be defined before this use, // otherwise this surprisingly compiles, but a call to `tmAnomaly_.anomaly` segfaults!