Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' TM '''
- tm = TemporalMemory(
- columnDimensions=(2048, ),
- cellsPerColumn=32,
- activationThreshold=16,
- initialPermanence=0.21,
- connectedPermanence=0.5,
- minThreshold=12,
- maxNewSynapseCount=20,
- permanenceIncrement=0.1,
- permanenceDecrement=0.1,
- predictedSegmentDecrement=0.0,
- maxSegmentsPerCell=128,
- maxSynapsesPerSegment=32,
- seed=1960
- )
- ''' SP '''
- sp = SpatialPooler(
- inputDimensions=(encodingWidth),
- columnDimensions=(2048),
- potentialPct=0.85,
- globalInhibition=True,
- localAreaDensity=-1.0,
- numActiveColumnsPerInhArea=40.0,
- synPermInactiveDec=0.005,
- synPermActiveInc=0.04,
- synPermConnected=0.1,
- boostStrength=3.0,
- seed=1956,
- wrapAround=False
- )
- ''' Category ENC '''
- cat_enc = CategoryEncoder(w,list(set(TEST_SEQ_1)))
- ''' Main Function '''
- predictiveCells = list()
- TM_predCols = list()
- AnomScores = list()
- precisionRatios = list()
- for inputVal in TEST_SEQ_1:
- ### Get Category Encoding
- encoding = cat_enc.encode(inputVal)
- ### Convert encoding bits to column indices
- encoding_indices = []
- for i in range(len(encoding)):
- if list(encoding)[i] == 1:
- encoding_indices.append(i)
- ### TM compute
- tm.compute(encoding_indices,learn=True)
- activeCells = tm.getActiveCells()
- ### Get active & correctly predicted columns
- activeCols = list(set([tm.columnForCell(cell) for cell in activeCells]))
- correctly_predictedCols = [col for col in activeCols if col in TM_predCols]
- ### Get Anomaly Score & Precisoin
- ANOM = 1.0 - (len(correctly_predictedCols)/float(len(activeCols)))
- AnomScores.append(ANOM)
- precisionRatio = len(TM_predCols) / float(w) #len(activeCells) / float(len(activeCols))
- precisionRatios.append(precisionRatio)
- ### Get Predictive Cells & Columns
- predictiveCells = tm.getPredictiveCells()
- TM_predCols = list(set([tm.columnForCell(cell) for cell in predictiveCells]))
- ### Print to screen: Input, Anomaly Score, Precision
- if print_screen:
- print('INPUT: ',inputVal,' -- ','ANOM: ',ANOM,' -- ','Precision: ',precisionRatio)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement