Anomaly Detection

Now, I’m implementing Anomaly Detection. In the API document, I found;

class nupic.algorithms.anomaly.Anomaly(slidingWindowSize=None, mode=‘pure’, binaryAnomalyThreshold=None)

compute(activeColumns, predictedColumns, inputValue=None, timestamp=None)
Compute the anomaly score as the percent of active columns not predicted.

activeColumns – array of active column indices
predictedColumns – array of columns indices predicted in this step (used for anomaly in step T+1)

in Sequence Memory, I find only getPredictiveCells() but not getPredectedColumns etc…

How can I get predictedColumns? I have to compute predictedColumns from getPredictiveCells()?

First, get the predictive cells, then get the column for each cell using columnForCell.

1 Like

Several relevant threads, but posting in this one because it seems be most relevant.

Using columnForCell seems to only work for a single input. You can get all the columns using mapCellsToColumns().keys()

I’ve posted the TM and anomaly section of my code (based on the algorithms quickstart example) below. Only problem is that it doesn’t match the OPF results with the same (I think) parameters and input. The OPF anomaly score calculation is a little more opaque since it is under the hood, so to speak.

Can someone confirm that this is the right way of using Anomaly.compute()? Or any thoughts on why results might be different for OPF vs this code?

  # Execute Temporal Memory algorithm over active mini-columns.
  if stillLearning==True:
    tm.compute(spActiveColumnIndices, learn=True)
    tm.compute(spActiveColumnIndices, learn=False)
  tmActiveCells = tm.getActiveCells()
  tmPredictiveCells = tm.getPredictiveCells()
  tmActiveColumnIndices = tm.mapCellsToColumns(tmActiveCells).keys()
  this_tmPredictiveColumnIndices = tm.mapCellsToColumns(tmPredictiveCells).keys()
  if counter==1:
    # For the first iteration, there are no T-1 predictions
    last_tmPredictiveColumnIndices = this_tmPredictiveColumnIndices
  # Get the anomaly score, likelihood, and log-likelihood for this input
  # Make sure to use the shifted predictive cells since the predictive
  # cells for T should be compared to the active cells for T+1
  anomalyScore = anom.compute(tmActiveColumnIndices,last_tmPredictiveColumnIndices,timestamp=counter)
  anomalyLikelihood = likelihoodHelper.anomalyProbability(sdr,anomalyScore,counter)
  anomalyLogLikelihood = AnomalyLikelihood.computeLogLikelihood(anomalyLikelihood)      
  # Store the current predictive cells to use in the next timestep
  last_tmPredictiveColumnIndices = this_tmPredictiveColumnIndices
1 Like

Yes, this is correct use.

Note to self, in community we use cellsToColumns , investigate if it works the same and probably use this one.

1 Like

Thanks for the reply!

Pulling the thread more on the discrepancy between OPF and algorithms API scripts: The natural thing to do is compare the SP and TM outputs for each method to see where they diverge. I couldn’t find a way to get the SP and TM outputs from the OPF model, and obviously this is on purpose since OPF is supposed to abstract these away.

I could always modify the nupic core code to spit the active/predicted columns out, but I am wondering if there is another way.

1 Like

There are 2 ways,

  • I’m guessing the framework (OPF?) gives you activation of {active + predictive columns}?
    • if so, active are also SP’s output; you can get predictive as TM’s output - active;
  • or use Py/C++ code where you can have TM::getActiveCells(), TM::getPredictiveCells(); and TM::cellsToColumns
1 Like

back to the bottom line, calling anomaly isn’t exactly user-friendly, and is indeed prone to making some hidden, conceptual mistakes.

I’m therefore working on TM::anomalyScore, anomaly refactored directly to call from a running TM.

It’ll be done soonish, next week max.

1 Like

From the NuPIC Usage FAQ

How do I extract details about the HTM’s internal cellular state?

I have two classes that show you exactly how I accessed these from NuPIC’s SP and TM instances for HTM School: