From 4d3d1f41c5c2b9e0b25b6bf63f238063cf388975 Mon Sep 17 00:00:00 2001 From: Marek Otahal Date: Mon, 7 Oct 2019 13:13:00 +0200 Subject: [PATCH] Revert "Example: use anomaly likelihood from TM" This reverts commit 9c5bd93e7bb8f6fde27beb8bb94ca0f2bc4f8a3d. --- py/htm/examples/hotgym.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/py/htm/examples/hotgym.py b/py/htm/examples/hotgym.py index 60e9b99ce4..fb1c749b7d 100644 --- a/py/htm/examples/hotgym.py +++ b/py/htm/examples/hotgym.py @@ -5,12 +5,12 @@ import random import math -import htm from htm.bindings.sdr import SDR, Metrics from htm.encoders.rdse import RDSE, RDSE_Parameters from htm.encoders.date import DateEncoder from htm.bindings.algorithms import SpatialPooler from htm.bindings.algorithms import TemporalMemory +from htm.algorithms.anomaly_likelihood import AnomalyLikelihood from htm.bindings.algorithms import Predictor _EXAMPLE_DIR = os.path.dirname(os.path.abspath(__file__)) @@ -109,17 +109,25 @@ def main(parameters=default_parameters, argv=None, verbose=True): permanenceDecrement = tmParams["permanenceDec"], predictedSegmentDecrement = 0.0, maxSegmentsPerCell = tmParams["maxSegmentsPerCell"], - maxSynapsesPerSegment = tmParams["maxSynapsesPerSegment"], - anomalyMode = htm.bindings.algorithms.ANMode.LIKELIHOOD, + maxSynapsesPerSegment = tmParams["maxSynapsesPerSegment"] ) tm_info = Metrics( [tm.numberOfCells()], 999999999 ) - + + # setup likelihood, these settings are used in NAB + anParams = parameters["anomaly"]["likelihood"] + probationaryPeriod = int(math.floor(float(anParams["probationaryPct"])*len(records))) + learningPeriod = int(math.floor(probationaryPeriod / 2.0)) + anomaly_history = AnomalyLikelihood(learningPeriod= learningPeriod, + estimationSamples= probationaryPeriod - learningPeriod, + reestimationPeriod= anParams["reestimationPeriod"]) + predictor = Predictor( steps=[1, 5], alpha=parameters["predictor"]['sdrc_alpha'] ) predictor_resolution = 1 # Iterate through every datum in the dataset, record the inputs & outputs. inputs = [] anomaly = [] + anomalyProb = [] predictions = {1: [], 5: []} for count, record in enumerate(records): @@ -157,7 +165,10 @@ def main(parameters=default_parameters, argv=None, verbose=True): predictions[n].append( np.argmax( pdf[n] ) * predictor_resolution ) else: predictions[n].append(float('nan')) + + anomalyLikelihood = anomaly_history.anomalyProbability( consumption, tm.anomaly ) anomaly.append( tm.anomaly ) + anomalyProb.append( anomalyLikelihood ) predictor.learn(count, tm.getActiveCells(), int(consumption / predictor_resolution))