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.
Parameters:
activeColumns – array of active column indices predictedColumns – array of columns indices predicted in this step (used for anomaly in step T+1)
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)
else:
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
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.