-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtemporalMemory_test.go
485 lines (383 loc) · 13.5 KB
/
temporalMemory_test.go
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package htm
import (
//"fmt"
"github.com/stretchr/testify/assert"
"sort"
"testing"
)
func TestPickCellsToLearnOnAvoidDuplicates(t *testing.T) {
tmp := NewTemporalMemoryParams()
tmp.MaxNewSynapseCount = 1000
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
connections.CreateSynapse(0, 23, 0.6)
winnerCells := []int{233, 144}
// Ensure that no additional (duplicate) cells were picked
assert.Equal(t, winnerCells, tm.pickCellsToLearnOn(2, 0, winnerCells, connections))
}
func TestPickCellsToLearnOn(t *testing.T) {
tmp := NewTemporalMemoryParams()
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
winnerCells := []int{4, 47, 58, 93}
result := tm.pickCellsToLearnOn(100, 0, winnerCells, connections)
sort.Ints(result)
assert.Equal(t, []int{4, 47, 58, 93}, result)
assert.Equal(t, []int{}, tm.pickCellsToLearnOn(0, 0, winnerCells, connections))
assert.Equal(t, []int{4, 58}, tm.pickCellsToLearnOn(2, 0, winnerCells, connections))
}
func TestAdaptSegmentToMin(t *testing.T) {
tmp := NewTemporalMemoryParams()
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
connections.CreateSynapse(0, 23, 0.1)
tm.adaptSegment(0, []int{}, connections)
assert.Equal(t, 0.0, connections.DataForSynapse(0).Permanence)
// // Now permanence should be at min
tm.adaptSegment(0, []int{}, connections)
assert.Equal(t, 0.0, connections.DataForSynapse(0).Permanence)
}
func TestAdaptSegmentToMax(t *testing.T) {
tmp := NewTemporalMemoryParams()
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
connections.CreateSynapse(0, 23, 0.9)
tm.adaptSegment(0, []int{0}, connections)
assert.Equal(t, 1.0, connections.DataForSynapse(0).Permanence)
// Now permanence should be at max
tm.adaptSegment(0, []int{0}, connections)
assert.Equal(t, 1.0, connections.DataForSynapse(0).Permanence)
}
func TestLeastUsedCell(t *testing.T) {
tmp := NewTemporalMemoryParams()
tmp.ColumnDimensions = []int{2}
tmp.CellsPerColumn = 2
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
connections.CreateSynapse(0, 3, 0.3)
for i := 0; i < 100; i++ {
assert.Equal(t, 1, tm.getLeastUsedCell(0, connections))
}
}
func TestAdaptSegment(t *testing.T) {
tmp := NewTemporalMemoryParams()
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
connections.CreateSynapse(0, 23, 0.6)
connections.CreateSynapse(0, 37, 0.4)
connections.CreateSynapse(0, 477, 0.9)
tm.adaptSegment(0, []int{0, 1}, connections)
assert.Equal(t, 0.7, connections.DataForSynapse(0).Permanence)
assert.Equal(t, 0.5, connections.DataForSynapse(1).Permanence)
assert.Equal(t, 0.8, connections.DataForSynapse(2).Permanence)
}
func TestGetConnectedActiveSynapsesForSegment(t *testing.T) {
tmp := NewTemporalMemoryParams()
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
connections.CreateSynapse(0, 23, 0.6)
connections.CreateSynapse(0, 37, 0.4)
connections.CreateSynapse(0, 477, 0.9)
connections.CreateSegment(1)
connections.CreateSynapse(1, 733, 0.7)
connections.CreateSegment(8)
connections.CreateSynapse(2, 486, 0.9)
activeSynapsesForSegment := map[int][]int{
0: {0, 1},
1: {3},
}
assert.Equal(t, []int{0}, tm.getConnectedActiveSynapsesForSegment(0,
activeSynapsesForSegment,
0.5,
connections))
assert.Equal(t, []int{3}, tm.getConnectedActiveSynapsesForSegment(1,
activeSynapsesForSegment,
0.5,
connections))
}
func TestComputeActiveSynapsesNoActivity(t *testing.T) {
tmp := NewTemporalMemoryParams()
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
connections.CreateSynapse(0, 23, 0.6)
connections.CreateSynapse(0, 37, 0.4)
connections.CreateSynapse(0, 477, 0.9)
connections.CreateSegment(1)
connections.CreateSynapse(1, 733, 0.7)
connections.CreateSegment(8)
connections.CreateSynapse(2, 486, 0.9)
activeCells := []int{}
assert.Equal(t, map[int][]int{}, tm.computeActiveSynapses(activeCells, connections))
}
func TestGetBestMatchingSegment(t *testing.T) {
tmp := NewTemporalMemoryParams()
tmp.MinThreshold = 1
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
connections.CreateSynapse(0, 23, 0.6)
connections.CreateSynapse(0, 37, 0.4)
connections.CreateSynapse(0, 477, 0.9)
connections.CreateSegment(0)
connections.CreateSynapse(1, 49, 0.9)
connections.CreateSynapse(1, 3, 0.8)
connections.CreateSegment(1)
connections.CreateSynapse(2, 733, 0.7)
connections.CreateSegment(8)
connections.CreateSynapse(3, 486, 0.9)
activeSynapsesForSegment := map[int][]int{
0: []int{0, 1},
1: []int{3},
2: []int{5},
}
bestCell, connectedSyns := tm.getBestMatchingSegment(0, activeSynapsesForSegment, connections)
assert.Equal(t, 0, bestCell)
assert.Equal(t, []int{0, 1}, connectedSyns)
bestCell, connectedSyns = tm.getBestMatchingSegment(1, activeSynapsesForSegment, connections)
assert.Equal(t, 2, bestCell)
assert.Equal(t, []int{5}, connectedSyns)
bestCell, connectedSyns = tm.getBestMatchingSegment(8, activeSynapsesForSegment, connections)
assert.Equal(t, -1, bestCell)
assert.Equal(t, []int(nil), connectedSyns)
bestCell, connectedSyns = tm.getBestMatchingSegment(100, activeSynapsesForSegment, connections)
assert.Equal(t, -1, bestCell)
assert.Equal(t, []int(nil), connectedSyns)
}
func TestGetBestMatchingCellFewestSegments(t *testing.T) {
tmp := NewTemporalMemoryParams()
tmp.ColumnDimensions = []int{2}
tmp.CellsPerColumn = 2
tmp.MinThreshold = 1
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
connections.CreateSynapse(0, 3, 0.3)
activeSynapsesForSegment := map[int][]int{}
for i := 0; i < 100; i++ {
// Never pick cell 0, always pick cell 1
cell, _ := tm.getBestMatchingCell(0, activeSynapsesForSegment, connections)
assert.Equal(t, 1, cell)
}
}
func TestGetBestMatchingCell(t *testing.T) {
tmp := NewTemporalMemoryParams()
tmp.MinThreshold = 1
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
connections.CreateSynapse(0, 23, 0.6)
connections.CreateSynapse(0, 37, 0.4)
connections.CreateSynapse(0, 477, 0.9)
connections.CreateSegment(0)
connections.CreateSynapse(1, 49, 0.9)
connections.CreateSynapse(1, 3, 0.8)
connections.CreateSegment(1)
connections.CreateSynapse(2, 733, 0.7)
connections.CreateSegment(108)
connections.CreateSynapse(3, 486, 0.9)
activeSynapsesForSegment := map[int][]int{
0: []int{0, 1},
1: []int{3},
2: []int{5},
}
bestCell, bestSeg := tm.getBestMatchingCell(0, activeSynapsesForSegment, connections)
assert.Equal(t, 0, bestCell)
assert.Equal(t, 0, bestSeg)
//randomly picked
bestCell, bestSeg = tm.getBestMatchingCell(3, activeSynapsesForSegment, connections)
assert.Equal(t, 99, bestCell) //random
assert.Equal(t, -1, bestSeg)
//randomly picked
bestCell, bestSeg = tm.getBestMatchingCell(999, activeSynapsesForSegment, connections)
assert.Equal(t, 31979, bestCell) //random
assert.Equal(t, -1, bestSeg)
}
func TestComputeActiveSynapses(t *testing.T) {
tmp := NewTemporalMemoryParams()
//tmp.MinThreshold = 1
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
connections.CreateSynapse(0, 23, 0.6)
connections.CreateSynapse(0, 37, 0.4)
connections.CreateSynapse(0, 477, 0.9)
connections.CreateSegment(1)
connections.CreateSynapse(1, 733, 0.7)
connections.CreateSegment(8)
connections.CreateSynapse(2, 486, 0.9)
activeCells := []int{23, 37, 733, 4973}
expected := map[int][]int{
0: []int{0, 1},
1: []int{3},
}
assert.Equal(t, expected, tm.computeActiveSynapses(activeCells, connections))
}
func TestComputePredictiveCells(t *testing.T) {
tmp := NewTemporalMemoryParams()
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
connections.CreateSynapse(0, 23, 0.6)
connections.CreateSynapse(0, 37, 0.5)
connections.CreateSynapse(0, 477, 0.9)
connections.CreateSegment(1)
connections.CreateSynapse(1, 733, 0.7)
connections.CreateSynapse(1, 733, 0.4)
connections.CreateSegment(1)
connections.CreateSynapse(2, 974, 0.9)
connections.CreateSegment(8)
connections.CreateSynapse(3, 486, 0.9)
connections.CreateSegment(100)
activeSynapsesForSegment := map[int][]int{
0: []int{0, 1},
1: []int{3, 4},
2: []int{5},
}
activeSegments, predictiveCells := tm.computePredictiveCells(activeSynapsesForSegment, connections)
//TODO: numentas returns [0]
assert.Equal(t, []int(nil), activeSegments)
assert.Equal(t, []int(nil), predictiveCells)
}
func TestLearnOnSegments(t *testing.T) {
tmp := NewTemporalMemoryParams()
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
connections.CreateSynapse(0, 23, 0.6)
connections.CreateSynapse(0, 37, 0.4)
connections.CreateSynapse(0, 477, 0.9)
connections.CreateSegment(1)
connections.CreateSynapse(1, 733, 0.7)
connections.CreateSegment(8)
connections.CreateSynapse(2, 486, 0.9)
connections.CreateSegment(100)
prevActiveSegments := []int{0, 2}
learningSegments := []int{1, 3}
prevActiveSynapsesForSegment := map[int][]int{
0: []int{0, 1},
1: []int{3},
}
winnerCells := []int{0}
prevWinnerCells := []int{10, 11, 12, 13, 14}
tm.learnOnSegments(prevActiveSegments,
learningSegments,
prevActiveSynapsesForSegment,
winnerCells,
prevWinnerCells,
connections)
//Check segment 0
assert.Equal(t, 0.7, connections.DataForSynapse(0).Permanence)
assert.Equal(t, 0.5, connections.DataForSynapse(1).Permanence)
assert.Equal(t, 0.8, connections.DataForSynapse(2).Permanence)
//Check segment 1
assert.InEpsilon(t, 0.8, connections.DataForSynapse(3).Permanence, 0.1)
assert.Equal(t, 2, len(connections.synapsesForSegment[1]))
//Check segment 2
assert.Equal(t, 0.9, connections.DataForSynapse(4).Permanence)
assert.Equal(t, 1, len(connections.synapsesForSegment[2]))
// Check segment 3
assert.Equal(t, 1, len(connections.synapsesForSegment[3]))
}
func TestBurstColumnsEmpty(t *testing.T) {
tmp := NewTemporalMemoryParams()
tm := NewTemporalMemory(tmp)
connections := tm.Connections
activeColumns := []int{}
predictedColumns := []int{}
prevActiveSynapsesForSegment := make(map[int][]int)
activeCells, winnerCells, learningSegments := tm.burstColumns(activeColumns,
predictedColumns,
prevActiveSynapsesForSegment,
connections)
assert.Equal(t, []int(nil), activeCells)
assert.Equal(t, []int(nil), winnerCells)
assert.Equal(t, []int(nil), learningSegments)
}
func TestBurstColumns(t *testing.T) {
tmp := NewTemporalMemoryParams()
tmp.CellsPerColumn = 4
tmp.MinThreshold = 1
tm := NewTemporalMemory(tmp)
connections := tm.Connections
connections.CreateSegment(0)
connections.CreateSynapse(0, 23, 0.6)
connections.CreateSynapse(0, 37, 0.4)
connections.CreateSynapse(0, 477, 0.9)
connections.CreateSegment(0)
connections.CreateSynapse(1, 49, 0.9)
connections.CreateSynapse(1, 3, 0.8)
connections.CreateSegment(1)
connections.CreateSynapse(2, 733, 0.7)
connections.CreateSegment(108)
connections.CreateSynapse(3, 486, 0.9)
activeColumns := []int{0, 1, 26}
predictedColumns := []int{26}
prevActiveSynapsesForSegment := map[int][]int{
0: []int{0, 1},
1: []int{3},
2: []int{5},
}
activeCells, winnerCells, learningSegments := tm.burstColumns(activeColumns,
predictedColumns,
prevActiveSynapsesForSegment,
connections)
assert.Equal(t, []int{0, 1, 2, 3, 4, 5, 6, 7}, activeCells)
assert.Equal(t, []int{0, 5}, winnerCells) //5 is randomly chosen cell
assert.Equal(t, []int{0, 4}, learningSegments) //4 is new segment created
//Check that new segment was added to winner cell (4) in column 1
assert.Equal(t, []int{4}, connections.segmentsForCell[5])
}
func TestActivateCorrectlyPredictiveCellsEmpty(t *testing.T) {
tmp := NewTemporalMemoryParams()
tmp.CellsPerColumn = 4
tmp.MinThreshold = 1
tm := NewTemporalMemory(tmp)
connections := tm.Connections
prevPredictiveCells := []int{}
activeColumns := []int{}
activeCells, winnerCells, predictedColumns := tm.activateCorrectlyPredictiveCells(prevPredictiveCells,
activeColumns,
connections)
assert.Equal(t, []int(nil), activeCells)
assert.Equal(t, []int(nil), winnerCells)
assert.Equal(t, []int(nil), predictedColumns)
// No previous predictive cells
prevPredictiveCells = []int{}
activeColumns = []int{32, 47, 823}
activeCells, winnerCells, predictedColumns = tm.activateCorrectlyPredictiveCells(prevPredictiveCells,
activeColumns,
connections)
assert.Equal(t, []int(nil), activeCells)
assert.Equal(t, []int(nil), winnerCells)
assert.Equal(t, []int(nil), predictedColumns)
// No active columns
prevPredictiveCells = []int{0, 237, 1026, 26337, 26339, 55536}
activeColumns = []int{}
activeCells, winnerCells, predictedColumns = tm.activateCorrectlyPredictiveCells(prevPredictiveCells,
activeColumns,
connections)
assert.Equal(t, []int(nil), activeCells)
assert.Equal(t, []int(nil), winnerCells)
assert.Equal(t, []int(nil), predictedColumns)
}
func TestActivateCorrectlyPredictiveCells(t *testing.T) {
tmp := NewTemporalMemoryParams()
tm := NewTemporalMemory(tmp)
connections := tm.Connections
prevPredictiveCells := []int{0, 237, 1026, 26337, 26339, 55536}
activeColumns := []int{32, 47, 823}
activeCells, winnerCells, predictedColumns := tm.activateCorrectlyPredictiveCells(prevPredictiveCells,
activeColumns,
connections)
assert.Equal(t, []int{1026, 26337, 26339}, activeCells)
assert.Equal(t, []int{1026, 26337, 26339}, winnerCells)
assert.Equal(t, []int{32, 823}, predictedColumns)
}