-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtm_test.py
112 lines (91 loc) · 4.08 KB
/
htm_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import numpy as np
from htm.bindings.sdr import SDR
from htm.algorithms import TemporalMemory as TM
from time import sleep
from console import fg, bg, utils
def formatCell(cellName, activeState, winnerState, predictedState):
styleFg = fg.white
styleBg = bg.black
style = None
if(activeState == 1):
styleFg = fg.green
if(winnerState == 1):
styleBg = bg.i22
if(predictedState == 1):
styleBg = bg.i241
style = styleFg + styleBg
if(style != None):
result = style(format(cellName,'2d'))
else:
result = format(cellName,'2d')
return result
def printHeader(step, sensorValue):
print('-' * dashMultiplyer)
print('| Cycle', format(cycle+1,'2d'), '| Step', format(step,'3d'), '| Value', format(sensorValue,'3d'), '| Anomaly:', format(tm.anomaly, '.1f'), '|')
print('-' * dashMultiplyer)
colHeader = '| Column | '
for colIdx in range(columns):
colHeader += format(colIdx,'2d') + ' | '
print(colHeader)
print('-' * dashMultiplyer)
def printConnectionDetails(tm):
for cell in range(columns * cellsPerColumn):
segments = tm.connections.segmentsForCell(cell)
for segment in segments:
num_synapses = tm.connections.numSynapses(segment)
for synapse in tm.connections.synapsesForSegment(segment):
presynCell = tm.connections.presynapticCellForSynapse(synapse)
permanence = tm.connections.permanenceForSynapse(synapse)
print('cell', format(cell,'2d'), 'segment', format(segment,'2d'), 'has synapse to cell', format(presynCell,'2d'), 'with permanence', format(permanence,'.2f'))
connected_synapses = tm.connections.numConnectedSynapses(segment)
print('cell', format(cell,'2d'), 'segment', format(segment,'2d'), 'has', connected_synapses, 'connected synapse(s)')
def process(cycleArray):
step = 1
for sensorValue in cycleArray:
sensorValueBits = inputSDR.dense
sensorValueBits = np.zeros(columns)
sensorValueBits[sensorValue] = 1
inputSDR.dense = sensorValueBits
tm.compute(inputSDR, learn = True)
activeCells = tm.getActiveCells()
tm.activateDendrites(True)
activeCellsDense = activeCells.dense
winnerCellsDense = tm.getWinnerCells().dense
predictedCellsDense = tm.getPredictiveCells().dense
utils.cls()
printHeader(step, sensorValue)
for rowIdx in range(cellsPerColumn):
rowData = activeCellsDense[:,rowIdx]
rowStr = '| Cell | '
for colI in range(rowData.size):
cellName = np.ravel_multi_index([colI, rowIdx], (columns, cellsPerColumn))
stateActive = activeCellsDense[colI,rowIdx]
stateWinner = winnerCellsDense[colI,rowIdx]
statePredicted = predictedCellsDense[colI,rowIdx]
rowStr += formatCell(cellName, stateActive, stateWinner, statePredicted) + ' | '
print(rowStr)
print(tm.connections)
printConnectionDetails(tm)
print()
step = step + 1
sleep(0.5)
dashMultiplyer = 50
cycleArray = [0, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1]
cycles = 4
columns = 8
inputSDR = SDR( columns )
cellsPerColumn = 1
tm = TM(columnDimensions = (inputSDR.size,),
cellsPerColumn = cellsPerColumn, # default: 32
minThreshold = 1, # default: 10
activationThreshold = 1, # default: 13
initialPermanence = 0.4, # default: 0.21
connectedPermanence = 0.5, # default: 0.5
permanenceIncrement = 0.1, # default: 0.1
permanenceDecrement = 0.1, # default: 0.1
predictedSegmentDecrement = 0.0, # default: 0.0
maxSegmentsPerCell = 1, # default: 255
maxSynapsesPerSegment = 1 # default: 255
)
for cycle in range(cycles):
process(cycleArray)