-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathsequence_simulations.py
400 lines (330 loc) · 12.9 KB
/
sequence_simulations.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
# ----------------------------------------------------------------------
# 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 numpy
import csv
from optparse import OptionParser
import sys
import os
import datetime
from prettytable import PrettyTable
from htmresearch.algorithms.faulty_temporal_memory import FaultyTemporalMemory
from nupic.algorithms.monitor_mixin.temporal_memory_monitor_mixin import (
TemporalMemoryMonitorMixin)
class MonitoredTemporalMemory(TemporalMemoryMonitorMixin,
FaultyTemporalMemory): pass
#########################################################################
#
# Sequence generation routines
def letterSequence(letters, w=40):
"""
Return a list of input vectors corresponding to sequence of letters.
The vector for each letter has w contiguous bits ON and represented as a
sequence of non-zero indices.
"""
sequence = []
for letter in letters:
i = ord(letter) - ord('A')
sequence.append(set(range(i*w,(i+1)*w)))
return sequence
def getRandomVector(w=40, n=2048):
"Return a list of w random indices out of a vector of n elements"
return set(numpy.random.permutation(n)[0:w])
def getHighOrderSequenceChunk(it, switchover=1000, w=40, n=2048):
"""
Given an iteration index, returns a list of vectors to be appended to the
input stream, as well as a string label identifying the sequence. This
version generates a bunch of high order sequences. The first element always
provides sufficient context to predict the rest of the elements.
After switchover iterations, it will generate a different set of sequences.
"""
if it%10==3:
s = numpy.random.randint(5)
if it <= switchover:
if s==0:
label="XABCDE"
elif s==1:
label="YCBEAF"
elif s==2:
label="GHIJKL"
elif s==3:
label="WABCMN"
else:
label="ZDBCAE"
else:
if s==0:
label="XCBEAF"
elif s==1:
label="YABCDE"
elif s==2:
label="GABCMN"
elif s==3:
label="WHIJKL"
else:
label="ZDHICF"
vecs = letterSequence(label)
else:
vecs= [getRandomVector(w, n)]
label="."
return vecs,label
def addNoise(vecs, percent=0.1, n=2048):
"""
Add noise to the given sequence of vectors and return the modified sequence.
A percentage of the on bits are shuffled to other locations.
"""
noisyVecs = []
for vec in vecs:
nv = vec.copy()
for idx in vec:
if numpy.random.random() <= percent:
nv.discard(idx)
nv.add(numpy.random.randint(n))
noisyVecs.append(nv)
return noisyVecs
#########################################################################
#
# Core experiment routines
def computePredictionAccuracy(pac, pic):
"""
Given a temporal memory instance return the prediction accuracy. The accuracy
is computed as 1 - (#correctly predicted cols / # predicted cols). The
accuracy is 0 if there were no predicted columns.
"""
pcols = float(pac + pic)
if pcols == 0:
return 0.0
else:
return (pac / pcols)
def createReport(tm, options, sequenceString, numSegments, numSynapses):
"""
Create CSV file with detailed trace of predictions, missed predictions,
accuracy, segment/synapse growth, etc.
"""
pac = tm.mmGetTracePredictedActiveColumns()
pic = tm.mmGetTracePredictedInactiveColumns()
upac = tm.mmGetTraceUnpredictedActiveColumns()
resultsFilename = os.path.join("results", options.name+".csv")
with open(resultsFilename,"wb") as resultsFile:
csvWriter = csv.writer(resultsFile)
accuracies = numpy.zeros(len(pac.data))
smoothedAccuracies = []
am = 0
csvWriter.writerow(["time", "element", "pac", "pic", "upac", "a",
"am", "accuracy", "sum", "nSegs", "nSyns"])
for i,j in enumerate(pac.data):
if i>0:
# Compute instantaneous and average accuracy.
a = computePredictionAccuracy(len(j), len(pic.data[i]))
# We compute an exponential plus a windowed average to get curve
# looking nice and smooth for the paper.
am = 0.99*am + 0.01*a
accuracies[i] = am
i0 = max(0, i-60+1)
accuracy = numpy.mean(accuracies[i0:i+1])
smoothedAccuracies.append(accuracy)
row=[i, sequenceString[i], len(j), len(pic.data[i]),
len(upac.data[i]), a, am,
accuracy,
numpy.sum(accuracies[i0:i+1]),
numSegments[i], numSynapses[i]]
csvWriter.writerow(row)
return smoothedAccuracies
def killCells(i, options, tm):
"""
Kill cells as appropriate
"""
# Kill cells if called for
if options.simulation == "killer":
if i == options.switchover:
print "i=",i,"Killing cells for the first time!"
tm.killCells(percent = options.noise)
if i == options.secondKill:
print "i=",i,"Killing cells again up to",options.secondNoise
tm.killCells(percent = options.secondNoise)
elif options.simulation == "killingMeSoftly" and (i%100 == 0):
steps = (options.secondKill - options.switchover)/100
nsteps = (options.secondNoise - options.noise)/steps
noise = options.noise + nsteps*(i-options.switchover)/100
if i in xrange(options.switchover, options.secondKill+1):
print "i=",i,"Killing cells!"
tm.killCells(percent = noise)
def runExperiment1(options):
if not os.path.exists("results/"):
os.makedirs("results/")
outFilename = os.path.join("results", options.name+".out")
with open(outFilename,"wb") as outputFile:
startTime = datetime.datetime.now()
print >>outputFile, "Start time=", startTime.isoformat(' ')
numpy.random.seed(options.seed)
tm = MonitoredTemporalMemory(minThreshold=15,
activationThreshold=15,
maxNewSynapseCount=40,
cellsPerColumn=options.cells,
predictedSegmentDecrement = 0.01,
columnDimensions=(2048,),
initialPermanence=0.21,
connectedPermanence=0.50,
permanenceIncrement=0.10,
permanenceDecrement=0.10,
seed=42,
)
printOptions(options, tm, outputFile)
# Run the simulation using the given parameters
sequenceString = ""
numSegments = []
numSynapses = []
i=0
print "total number of iterations: ", options.iterations
while i < options.iterations:
if i%100==0:
print "i=",i,"segments=",tm.connections.numSegments(),
print "synapses=",tm.connections.numSynapses()
sys.stdout.flush()
learn=True
if options.simulation == "normal":
vecs,label = getHighOrderSequenceChunk(i, options.switchover)
# Train with noisy data and then test with clean
elif options.simulation == "noisy":
vecs,label = getHighOrderSequenceChunk(i, i+1)
if i >= options.switchover:
options.noise = 0.0
learn= False
vecs = addNoise(vecs, percent = options.noise)
# Train with clean data and then test with noisy
elif options.simulation == "clean_noise":
noise = 0.0
vecs, label = getHighOrderSequenceChunk(i, i+1)
if i >= options.switchover:
noise = options.noise
learn= False
vecs = addNoise(vecs, percent = noise)
# Train with clean data and then kill of a certain percentage of cells
elif options.simulation in ["killer", "killingMeSoftly"]:
vecs,label = getHighOrderSequenceChunk(i, i+1)
else:
raise Exception("Unknown simulation: " + options.simulation)
# Train on the next sequence chunk
for xi, vec in enumerate(vecs):
killCells(i, options, tm)
tm.compute(vec, learn=learn)
numSegments.append(tm.connections.numSegments())
numSynapses.append(tm.connections.numSynapses())
i += 1
sequenceString += label
accuracies = createReport(tm, options, sequenceString, numSegments, numSynapses)
print >>outputFile, "End time=",datetime.datetime.now().isoformat(' ')
print >>outputFile, "Duration=",str(datetime.datetime.now()-startTime)
return accuracies
#########################################################################
#
# Debugging routines
def printSegment(tm, segment, connections):
cell = connections.cellForSegment(segment)
synapses = connections.synapsesForSegment(segment)
print "segment id=",segment
print " cell=",cell
print " col =",tm.columnForCell(cell)
print " synapses=",
for synapse in synapses:
synapseData = connections.dataForSynapse(synapse)
permanence = synapseData.permanence
presynapticCell = synapseData.presynapticCell
print "%d:%g" % (presynapticCell,permanence),
print
def printTemporalMemory(tm, outFile):
"""
Given an instance of TemporalMemory, print out the relevant parameters
"""
table = PrettyTable(["Parameter name", "Value", ])
table.add_row(["columnDimensions", tm.getColumnDimensions()])
table.add_row(["cellsPerColumn", tm.getCellsPerColumn()])
table.add_row(["activationThreshold", tm.getActivationThreshold()])
table.add_row(["minThreshold", tm.getMinThreshold()])
table.add_row(["maxNewSynapseCount", tm.getMaxNewSynapseCount()])
table.add_row(["permanenceIncrement", tm.getPermanenceIncrement()])
table.add_row(["permanenceDecrement", tm.getPermanenceDecrement()])
table.add_row(["initialPermanence", tm.getInitialPermanence()])
table.add_row(["connectedPermanence", tm.getConnectedPermanence()])
table.add_row(["predictedSegmentDecrement", tm.getPredictedSegmentDecrement()])
print >>outFile, table.get_string().encode("utf-8")
def printOptions(options, tm, outFile):
"""
Pretty print the set of options
"""
print >>outFile, "TM parameters:"
printTemporalMemory(tm, outFile)
print >>outFile, "Experiment parameters:"
for k,v in options.__dict__.iteritems():
print >>outFile, " %s : %s" % (k,str(v))
outFile.flush()
helpString = (
"\n%prog [options] [uid]"
"\n%prog --help"
"\n"
"\nRuns sequence simulations with artificial data."
)
# All the command line options
parser = OptionParser(helpString)
parser.add_option("--name",
help="Name of experiment. Outputs will be written to"
"results/name.csv & results/name.out (default: %default)",
dest="name",
default="temp")
parser.add_option("--iterations",
help="Number of iterations to run for. [default: %default]",
default=9000,
type=int)
parser.add_option("--seed",
help="Random seed to use. [default: %default]",
default=42,
type=int)
parser.add_option("--noise",
help="Percent noise for noisy simulations. [default: "
"%default]",
default=0.1,
type=float)
parser.add_option("--secondNoise",
help="Percent noise for second kill. [default: "
"%default]",
default=0.5,
type=float)
parser.add_option("--switchover",
help="Number of iterations after which to change "
"statistics. [default: %default]",
default=3500,
type=int)
parser.add_option("--secondKill",
help="Number of iterations after which to kill again. "
"[default: %default]",
default=50000,
type=int)
parser.add_option("--cells",
help="Number of per column. [default: %default]",
default=32,
type=int)
parser.add_option("--simulation",
help="Which simulation to run: 'normal', 'noisy', "
"'clean_noise', 'killer', 'killingMeSoftly' (default: "
"%default)",
default="normal",
type=str)
if __name__ == '__main__':
options, args = parser.parse_args(sys.argv[1:])
runExperiment1(options)