-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.py
423 lines (332 loc) · 16 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os.path
import yaml
import htm2d.environment
import htm2d.agent
from htm2d.agent import Direction
import numpy as np
import time
import matplotlib.pyplot as plt
import random
from utilities import (
plotBinaryMap,
isNotebook,
plotEnvironment,
) # auxiliary functions from utilities.py
from htm.bindings.algorithms import SpatialPooler, TemporalMemory
from htm.bindings.sdr import SDR, Metrics
from htm.encoders.rdse import RDSE, RDSE_Parameters
from htm.encoders.grid_cell_encoder import GridCellEncoder
from htm.algorithms.anomaly import Anomaly
PLOT_GRAPHS = False
PLOT_ENV = False
DISABLE_PANDA = False
# Panda vis
if not DISABLE_PANDA:
from PandaVis.pandaComm.server import PandaServer
from PandaVis.pandaComm.dataExchange import ServerData, dataHTMObject, dataLayer, dataInput
_EXEC_DIR = os.path.dirname(os.path.abspath(__file__))
# go one folder up and then into the objects folder
_OBJECTS_DIR = os.path.join(_EXEC_DIR, os.path.pardir, "objects")
OBJECT_FILENAME = "a.yml" # what object to load
anomalyHistData = []
fig_layers = None
fig_graphs = None
fig_environment = None
fig_expect = None
firstStep = True
iterationNo = 0
if not DISABLE_PANDA:
pandaServer = PandaServer()
def SystemSetup(parameters, verbose=True):
global agent, sensorEncoder, env, sensorLayer_sp, sensorLayer_SDR_columns
global gridCellEncoder, locationlayer_SDR_cells
global sensorLayer_tm
if verbose:
import pprint
print("Parameters:")
pprint.pprint(parameters, indent=4)
print("")
# create environment and the agent
env = htm2d.environment.TwoDimensionalEnvironment(20, 20)
agent = htm2d.agent.Agent()
# load object from yml file
with open(os.path.join(_OBJECTS_DIR, OBJECT_FILENAME), "r") as stream:
try:
env.load_object(stream)
except yaml.YAMLError as exc:
print(exc)
# SENSOR LAYER --------------------------------------------------------------
# setup sensor encoder
sensorEncoderParams = RDSE_Parameters()
sensorEncoderParams.category = True
sensorEncoderParams.size = parameters["enc"]["size"]
sensorEncoderParams.sparsity = parameters["enc"]["sparsity"]
sensorEncoderParams.seed = parameters["enc"]["seed"]
sensorEncoder = RDSE(sensorEncoderParams)
# Create SpatialPooler
spParams = parameters["sensorLayer_sp"]
sensorLayer_sp = SpatialPooler(
inputDimensions=(sensorEncoder.size,),
columnDimensions=(spParams["columnCount"],),
potentialPct=spParams["potentialPct"],
potentialRadius=sensorEncoder.size,
globalInhibition=True,
localAreaDensity=spParams["localAreaDensity"],
synPermInactiveDec=spParams["synPermInactiveDec"],
synPermActiveInc=spParams["synPermActiveInc"],
synPermConnected=spParams["synPermConnected"],
boostStrength=spParams["boostStrength"],
wrapAround=True,
)
sp_info = Metrics(sensorLayer_sp.getColumnDimensions(), 999999999)
# Create an SDR to represent active columns, This will be populated by the
# compute method below. It must have the same dimensions as the Spatial Pooler.
sensorLayer_SDR_columns = SDR(spParams["columnCount"])
# LOCATION LAYER ------------------------------------------------------------
# Grid cell modules
locParams = parameters["locationLayer"]
gridCellEncoder = GridCellEncoder(
size=locParams["cellCount"],
sparsity=locParams["sparsity"],
periods=locParams["periods"],
seed=locParams["seed"],
)
locationlayer_SDR_cells = SDR(gridCellEncoder.dimensions)
tmParams = parameters["sensorLayer_tm"]
sensorLayer_tm = TemporalMemory(
columnDimensions=(spParams["columnCount"],),
cellsPerColumn=tmParams["cellsPerColumn"],
activationThreshold=tmParams["activationThreshold"],
initialPermanence=tmParams["initialPerm"],
connectedPermanence=spParams["synPermConnected"],
minThreshold=tmParams["minThreshold"],
maxNewSynapseCount=tmParams["newSynapseCount"],
permanenceIncrement=tmParams["permanenceInc"],
permanenceDecrement=tmParams["permanenceDec"],
predictedSegmentDecrement=0.0,
maxSegmentsPerCell=tmParams["maxSegmentsPerCell"],
maxSynapsesPerSegment=tmParams["maxSynapsesPerSegment"],
externalPredictiveInputs=locParams["cellCount"],
)
tm_info = Metrics([sensorLayer_tm.numberOfCells()], 999999999)
def SystemCalculate(feature, learning , predictiveCellsSDR_last):
global sensorLayer_sp, sensorLayer_tm, anomalyHistData, fig_layers, fig_graphs, fig_environment, rawAnomaly, firstStep, predictiveCellsSDR
# ENCODE DATA TO SDR--------------------------------------------------
# Convert sensed feature to int
sensedFeature = 1 if feature == "X" else 0
sensorSDR = sensorEncoder.encode(sensedFeature)
# ACTIVATE COLUMNS IN SENSORY LAYER ----------------------------------
# Execute Spatial Pooling algorithm on Sensory Layer with sensorSDR as proximal input
sensorLayer_sp.compute(sensorSDR, learning, sensorLayer_SDR_columns)
if not firstStep:
# and calculate anomaly - compare how much of active columns had some predictive cells
rawAnomaly = Anomaly.calculateRawAnomaly(sensorLayer_SDR_columns,
sensorLayer_tm.cellsToColumns(predictiveCellsSDR_last))
else:
rawAnomaly = 0
# SIMULATE LOCATION LAYER --------------------------------------------
# Execute Location Layer - it is just GC encoder
gridCellEncoder.encode(agent.get_nextPosition(), locationlayer_SDR_cells)
#
# Execute Temporal memory algorithm over the Sensory Layer, with mix of
# Location Layer activity and Sensory Layer activity as distal input
externalDistalInput = locationlayer_SDR_cells
if firstStep:
firstStep = False
sensorLayer_tm.activateCells(sensorLayer_SDR_columns, learning)
# activateDendrites calculates active segments
sensorLayer_tm.activateDendrites(learn=learning, externalPredictiveInputsActive=externalDistalInput,
externalPredictiveInputsWinners=externalDistalInput)
# predictive cells are calculated directly from active segments
predictiveCellsSDR = sensorLayer_tm.getPredictiveCells()
# --------------------- VIS ------------------------------
# do not update if we are running GOTO iteration command
if not DISABLE_PANDA and (not pandaServer.gotoIteration or (pandaServer.gotoIteration and pandaServer.gotoIteration_no == iterationNo)):
# ------------------HTMpandaVis----------------------
# fill up values
serverData.iterationNo = iterationNo
serverData.HTMObjects["HTM1"].inputs["FeatureSensor"].stringValue = "Feature: {:.2f}".format(sensedFeature)
serverData.HTMObjects["HTM1"].inputs["FeatureSensor"].bits = sensorSDR.sparse
serverData.HTMObjects["HTM1"].inputs["FeatureSensor"].count = sensorSDR.size
serverData.HTMObjects["HTM1"].inputs["LocationLayer"].stringValue = str(agent.get_position())
serverData.HTMObjects["HTM1"].inputs["LocationLayer"].bits = locationlayer_SDR_cells.sparse
serverData.HTMObjects["HTM1"].inputs["LocationLayer"].count = locationlayer_SDR_cells.size
serverData.HTMObjects["HTM1"].layers["SensoryLayer"].activeColumns = sensorLayer_SDR_columns.sparse
serverData.HTMObjects["HTM1"].layers["SensoryLayer"].winnerCells = sensorLayer_tm.getWinnerCells().sparse
serverData.HTMObjects["HTM1"].layers["SensoryLayer"].activeCells = sensorLayer_tm.getActiveCells().sparse
serverData.HTMObjects["HTM1"].layers["SensoryLayer"].predictiveCells = predictiveCellsSDR.sparse
# print("ACTIVECOLS:"+str(serverData.HTMObjects["HTM1"].layers["SensoryLayer"].activeColumns ))
# print("WINNERCELLS:"+str(serverData.HTMObjects["HTM1"].layers["SensoryLayer"].winnerCells))
# print("ACTIVECELLS:" + str(serverData.HTMObjects["HTM1"].layers["SensoryLayer"].activeCells))
# print("PREDICTCELLS:"+str(serverData.HTMObjects["HTM1"].layers["SensoryLayer"].predictiveCells))
pandaServer.serverData = serverData
pandaServer.spatialPoolers["HTM1"] = sensorLayer_sp
pandaServer.temporalMemories["HTM1"] = sensorLayer_tm
pandaServer.NewStateDataReady()
print("Position:" + str(agent.get_position()))
print("Feature:" + str(sensedFeature))
print("Anomaly score:" + str(rawAnomaly))
anomalyHistData += [rawAnomaly]
# ------------------HTMpandaVis----------------------
if PLOT_ENV and not pandaServer.gotoIteration:
# Plotting and visualising environment-------------------------------------------
if (
fig_environment == None or isNotebook()
): # create figure only if it doesn't exist yet or we are in interactive console
fig_environment, _ = plt.subplots(nrows=1, ncols=1, figsize=(6, 4))
else:
fig_environment.axes[0].clear()
plotEnvironment(fig_environment.axes[0], "Environment", env, agent.get_position())
fig_environment.canvas.draw()
plt.show(block=False)
plt.pause(0.001) # delay is needed for proper redraw
if not DISABLE_PANDA:
print("One step finished")
pandaServer.BlockExecution()
print("Proceeding one step...")
if PLOT_GRAPHS and not pandaServer.gotoIteration:
# ---------------------------
if (
fig_graphs == None or isNotebook()
): # create figure only if it doesn't exist yet or we are in interactive console
fig_graphs, _ = plt.subplots(nrows=1, ncols=1, figsize=(5, 2))
else:
fig_graphs.axes[0].clear()
fig_graphs.axes[0].set_title("Anomaly score")
fig_graphs.axes[0].plot(anomalyHistData)
fig_graphs.canvas.draw()
#if agent.get_position() != [3, 4]: # HACK ALERT! Ignore at this pos (after reset)
# anomalyHistData += [sensorLayer_tm.anomaly]
def BuildPandaSystem(modelParams):
global serverData
serverData = ServerData()
serverData.HTMObjects["HTM1"] = dataHTMObject()
serverData.HTMObjects["HTM1"].inputs["FeatureSensor"] = dataInput()
serverData.HTMObjects["HTM1"].layers["SensoryLayer"] = dataLayer(
modelParams["sensorLayer_sp"]["columnCount"],
modelParams["sensorLayer_tm"]["cellsPerColumn"],
)
serverData.HTMObjects["HTM1"].layers["SensoryLayer"].proximalInputs = ["FeatureSensor"]
serverData.HTMObjects["HTM1"].layers["SensoryLayer"].distalInputs = ["LocationLayer"]
serverData.HTMObjects["HTM1"].inputs["LocationLayer"] = dataInput() # for now, Location layer is just position encoder
if __name__ == "__main__":
# load model parameters from file
f = open("modelParams.cfg", "r").read()
modelParams = eval(f)
if not DISABLE_PANDA:
# set up pandaVis
pandaServer.Start()
BuildPandaSystem(modelParams)
# set up system
SystemSetup(modelParams)
firstStep = True
# put agent in the environment
agent.set_env(env, 1, 1, 1, 1) # is on [1,1] and will go to [1,1]
agentDir = Direction.RIGHT
random.seed(1)
# for x in range(2000):
# print("Iteration:" + str(iterationNo))
# SystemCalculate(agent.get_feature(Direction.UP))
#
# # find direction that is not behind border of environment
# agentDir = Direction(random.randrange(0, 4))
# while agent.isBorderInThisDir(agentDir):
# agentDir = Direction(random.randrange(0, 4))
#
# agent.moveDir(agentDir)
#
# if PLOT_ENV or PLOT_GRAPHS:
# time.sleep(0.01)
# iterationNo += 1
# iterationNo = 0
# for i in range(10):
# for x in range(1, 19):
# for y in range(1, 19):
# print("Iteration:" + str(iterationNo))
#
# if iterationNo <= 246:
# pandaServer.runOneStep = True
# SystemCalculate(agent.get_feature(Direction.UP))
#
# if iterationNo == 245:
# print(serverData.HTMObjects["HTM1"].layers["SensoryLayer"].activeColumns)
# print(serverData.HTMObjects["HTM1"].layers["SensoryLayer"].winnerCells)
# print(serverData.HTMObjects["HTM1"].layers["SensoryLayer"].predictiveCells)
# if iterationNo == 246:
# print(serverData.HTMObjects["HTM1"].layers["SensoryLayer"].activeColumns)
# print(serverData.HTMObjects["HTM1"].layers["SensoryLayer"].winnerCells)
# print(serverData.HTMObjects["HTM1"].layers["SensoryLayer"].predictiveCells)
#
# agent.move(x, y)
#
# iterationNo += 1
iterationNo = 0
# for i in range(100000):
# for x in range(1, 19):
# for y in range(1, 19):
# print("Iteration:" + str(iterationNo))
# SystemCalculate(agent.get_feature(Direction.UP))
#
# agent.nextMove(x, y) # this tells agent where he will make movement next time & it will make previously requested movement
#
# iterationNo += 1
predictiveCellsSDR_last = SDR( modelParams["sensorLayer_sp"]["columnCount"]*modelParams["sensorLayer_tm"]["cellsPerColumn"])
for i in range(20):
for x in range(1, 19):
for y in range(1, 19):
print("Iteration:" + str(iterationNo))
SystemCalculate(agent.get_feature(Direction.UP),learning=True, predictiveCellsSDR_last = predictiveCellsSDR_last)
predictiveCellsSDR_last = predictiveCellsSDR
agent.nextMove(x, y) # this tells agent where he will make movement next time & it will make previously requested movement
iterationNo += 1
expectedObject = [x[:] for x in [[0] * 20] * 20]
A = [x[:] for x in [[0] * 20] * 20]
B = [x[:] for x in [[0] * 20] * 20]
predSDR1 = SDR(predictiveCellsSDR)
predSDR2 = SDR(predictiveCellsSDR)
# calculate what kind of object will system expect
for x in range(0,20):
for y in range(1,20):# for sensor UP !
agent.nextMove(x, y)
SystemCalculate("X", learning=False, predictiveCellsSDR_last = predSDR1)
predSDR1 = predictiveCellsSDR
print("active:" + str(sensorLayer_SDR_columns.sparse))
print("predictive:"+ str(predictiveCellsSDR))
scoreWithFeature = rawAnomaly
SystemCalculate(" ", learning=False, predictiveCellsSDR_last = predSDR2)
predSDR2 = predictiveCellsSDR
print("active:" + str(sensorLayer_SDR_columns.sparse))
print("predictive:" + str(predictiveCellsSDR))
scoreWithoutFeature = rawAnomaly
A[x][y] = scoreWithFeature
B[x][y] = scoreWithoutFeature
expectedObject[x][y] = 1 if scoreWithFeature > scoreWithoutFeature else 0
print(A)
print(B)
print(expectedObject)
# Plotting and visualising environment-------------------------------------------
if (
fig_expect == None or isNotebook()
): # create figure only if it doesn't exist yet or we are in interactive console
fig_expect, _ = plt.subplots(nrows=1, ncols=1, figsize=(6, 4))
else:
fig_expect.axes[0].clear()
plotBinaryMap(fig_expect.axes[0], "Expectation", expectedObject)
fig_expect.canvas.draw()
plt.show(block=False)
plt.pause(20) # delay is needed for proper redraw
# for x in range(2000):
# for i in range(5):
# print("Iteration:" + str(iterationNo))
# SystemCalculate()
# agent.moveDir(agentDir)
# if agent.get_position() == [3, 4]:
# sensorLayer_tm.reset()
# print("reset!")
# time.sleep(0.01)
# iterationNo += 1
# agentDir = Direction.RIGHT if agentDir == Direction.LEFT else Direction.LEFT
if not DISABLE_PANDA:
pandaServer.MainThreadQuitted()