Skip to content

Instantly share code, notes, and snippets.

@Baquetron
Created July 13, 2017 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Baquetron/79a791e487af977165935b2aa0c2abf7 to your computer and use it in GitHub Desktop.
Save Baquetron/79a791e487af977165935b2aa0c2abf7 to your computer and use it in GitHub Desktop.
SDR anomaly detector example
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2013, Numenta, Inc. Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with this program. If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------
import csv
import numpy as np
import os
#Encoder
from nupic.encoders.pass_through import PassThroughEncoder
#SpatialPooler
from nupic.algorithms.spatial_pooler import SpatialPooler
#Temporal Memory
from nupic.algorithms.temporal_memory import TemporalMemory
#Anomaly calculation
from nupic.algorithms.anomaly import Anomaly
iters = 100
def runMuestra(num):
_REF_DIR = "HOME/nupic-PFG/algorithms/encoder_prueba"
_INPUT_FILE_PATH = "data_input/test_b.csv"
#_PARAMS_PATH
# Create an array to represent active columns, all initially zero. This
# will be populated by the compute method below. It must have the same
# dimensions as the Spatial Pooler.
activeColumns = np.zeros(2048)
#Array initialization
predictedColumns = np.zeros(2048);
predictedColumnsIndices = np.nonzero(predictedColumns)[0]
#Has to be review in order to set it automatically
SDR_thru=PassThroughEncoder(n=13)
status_width=SDR_thru.getWidth()
statusBits_temp=np.zeros((status_width), dtype=np.int)
statusBits=np.zeros((status_width), dtype=np.int)
sp = SpatialPooler(
# How large the input encoding will be.
inputDimensions=(status_width),
# How many mini-columns will be in the Spatial Pooler.
columnDimensions=(2048),
# What percent of the columns's receptive field is available for potential
# synapses?
potentialPct=0.85,
# This means that the input space has no topology.
globalInhibition=True,
localAreaDensity=-1.0,
# Roughly 2%, giving that there is only one inhibition area because we have
# turned on globalInhibition (40 / 2048 = 0.0195)
numActiveColumnsPerInhArea=40.0,
# How quickly synapses grow and degrade.
synPermInactiveDec=0.005,
synPermActiveInc=0.04,
synPermConnected=0.1,
# boostStrength controls the strength of boosting. Boosting encourages
# efficient usage of SP columns.
boostStrength=1.0,
# Random number generator seed.
seed=1956,
# Determines if inputs at the beginning and end of an input dimension should
# be considered neighbors when mapping columns to inputs.
wrapAround=False
)
tm = TemporalMemory(
# Must be the same dimensions as the SP
columnDimensions=(2048, ),
# How many cells in each mini-column.
cellsPerColumn=32,
# A segment is active if it has >= activationThreshold connected synapses
# that are active due to infActiveState
activationThreshold=16,
initialPermanence=0.21,
connectedPermanence=0.5,
# Minimum number of active synapses for a segment to be considered during
# search for the best-matching segments.
minThreshold=12,
# The max number of synapses added to a segment during learning
maxNewSynapseCount=20,
permanenceIncrement=0.1,
permanenceDecrement=0.1,
predictedSegmentDecrement=0.0,
maxSegmentsPerCell=128,
maxSynapsesPerSegment=32,
seed=1960
)
#Activate anomaly_engine and define anomaly mode
aly = Anomaly(slidingWindowSize=None, mode='pure', binaryAnomalyThreshold=None)
for number in range (1,num):
count=0
with open(_INPUT_FILE_PATH, "rb") as fin:
csvReader=csv.reader(fin)
for count, record in enumerate(csvReader):
#We put the vector string member into a chart var called status_str
for i in range (0,13):
statusBits_temp[i]=int(record[i])
#Does not make anything but we can check array length is okey and if it meets fixed w if demanded
SDR_thru.encodeIntoArray(statusBits_temp,statusBits)
#Print complete encoding to the console as a binary representation.
#print(statusBits.astype('int16'))
# Execute Spatial Pooling algorithm over input space.
sp.compute(statusBits, True, activeColumns)
activeColumnIndices = np.nonzero(activeColumns)[0]
#print activeColumnIndices
# Execute Temporal Memory algorithm over active mini-columns.
tm.compute(activeColumnIndices, learn=True)
activeCells = tm.getActiveCells()
#print activeCells
#To compute anomaly score
scores=aly.compute(activeColumnIndices,predictedColumns)
#print "iter: "+ str(number) + " " + "Step: " + str(count) + " " + "anomaly: " + str(scores) + " " + "SDR: " + str(statusBits)
#print "Actual indices"
#print activeColumnIndices
#print "Predcited indices"
#print predictedColumnsIndices
predictedColumnsIndices=activeColumnIndices
print "iter: "+ str(number) + " " + "anomaly: " + str(scores)
print "Actual indices"
print activeColumnIndices
if __name__ == "__main__":
runMuestra(iters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment