Advertisement
Guest User

Untitled

a guest
Jun 8th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. ''' TM '''
  2. tm = TemporalMemory(
  3.   columnDimensions=(2048, ),
  4.   cellsPerColumn=32,
  5.   activationThreshold=16,
  6.   initialPermanence=0.21,
  7.   connectedPermanence=0.5,
  8.   minThreshold=12,
  9.   maxNewSynapseCount=20,
  10.   permanenceIncrement=0.1,
  11.   permanenceDecrement=0.1,
  12.   predictedSegmentDecrement=0.0,
  13.   maxSegmentsPerCell=128,
  14.   maxSynapsesPerSegment=32,
  15.   seed=1960
  16. )
  17. ''' SP '''
  18. sp = SpatialPooler(
  19.   inputDimensions=(encodingWidth),
  20.   columnDimensions=(2048),
  21.   potentialPct=0.85,
  22.   globalInhibition=True,
  23.   localAreaDensity=-1.0,
  24.   numActiveColumnsPerInhArea=40.0,
  25.   synPermInactiveDec=0.005,
  26.   synPermActiveInc=0.04,
  27.   synPermConnected=0.1,
  28.   boostStrength=3.0,
  29.   seed=1956,
  30.   wrapAround=False
  31. )
  32. ''' Category ENC '''
  33. cat_enc = CategoryEncoder(w,list(set(TEST_SEQ_1)))
  34.  
  35.  
  36. ''' Main Function '''
  37. predictiveCells = list()
  38. TM_predCols = list()
  39. AnomScores = list()
  40. precisionRatios = list()
  41.  
  42. for inputVal in TEST_SEQ_1:
  43.     ### Get Category Encoding
  44.     encoding =  cat_enc.encode(inputVal)
  45.    
  46.    ### Convert encoding bits to column indices
  47.     encoding_indices = []
  48.     for i in range(len(encoding)):
  49.         if list(encoding)[i] == 1:
  50.             encoding_indices.append(i)
  51.    
  52.     ### TM compute    
  53.     tm.compute(encoding_indices,learn=True)
  54.     activeCells = tm.getActiveCells()
  55.    
  56.     ### Get active & correctly predicted columns
  57.     activeCols = list(set([tm.columnForCell(cell) for cell in activeCells]))
  58.     correctly_predictedCols = [col for col in activeCols if col in TM_predCols]
  59.    
  60.     ### Get Anomaly Score & Precisoin
  61.     ANOM = 1.0 - (len(correctly_predictedCols)/float(len(activeCols)))
  62.     AnomScores.append(ANOM)
  63.     precisionRatio = len(TM_predCols) / float(w)  #len(activeCells) / float(len(activeCols))  
  64.     precisionRatios.append(precisionRatio)
  65.    
  66.     ### Get Predictive Cells & Columns
  67.     predictiveCells = tm.getPredictiveCells()
  68.     TM_predCols = list(set([tm.columnForCell(cell) for cell in predictiveCells]))
  69.  
  70.     ### Print to screen: Input, Anomaly Score, Precision
  71.     if print_screen:
  72.         print('INPUT: ',inputVal,' -- ','ANOM: ',ANOM,' -- ','Precision: ',precisionRatio)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement