forked from MrBly/WalnutiQ
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TemporalPooler.java
581 lines (515 loc) · 25.5 KB
/
TemporalPooler.java
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package model.MARK_II.generalAlgorithm;
import com.google.gson.Gson;
import model.MARK_II.region.*;
import model.MARK_II.util.FileInputOutput;
import java.io.IOException;
import java.util.*;
/**
* Idea behind temporal pooling: SDRs that occur adjacent in time probably have
* a common underlying cause. Several times a second your eyes fixate on a
* different part of the image causing a complete change in input. Despite this
* changing input your perception is stable. Somewhere in higher region there
* must be neurons that remain active.
*
* Input into TemporalPooler: activeColumns of a Region at time t computed by
* SpatialPooler
*
* Output from TemporalPooler: boolean OR of the current active and predictive
* state for each neuron in the set of activeColumns of a Region.
*
* @author Quinn Liu ([email protected])
* @version April 27, 2014
*/
public class TemporalPooler extends Pooler {
private SpatialPooler spatialPooler;
private final int newSynapseCount;
private List<Neuron> currentLearningNeurons;
private SegmentUpdateList segmentUpdateList;
private Set<ColumnPosition> predictiveColumnsAtTForTPlus1;
private Set<ColumnPosition> predictiveColumnsAtTMinus1;
public TemporalPooler(SpatialPooler spatialPooler, int newSynapseCount) {
this.spatialPooler = spatialPooler;
super.region = spatialPooler.getRegion();
this.segmentUpdateList = new SegmentUpdateList();
this.newSynapseCount = newSynapseCount;
this.currentLearningNeurons = new ArrayList<Neuron>();
this.predictiveColumnsAtTForTPlus1 = new HashSet<ColumnPosition>();
this.predictiveColumnsAtTMinus1 = new HashSet<ColumnPosition>();
}
public void performPooling() {
Set<Column> activeColumns = this.spatialPooler.getActiveColumns();
if (super.getLearningState()) {
this.phaseOne(activeColumns);
this.phaseTwo();
this.phaseThree();
} else {
this.computeActiveStateOfAllNeuronsInActiveColumn(activeColumns);
this.computePredictiveStateOfAllNeurons();
}
}
public SpatialPooler getSpatialPooler() {
return this.spatialPooler;
}
public void nextTimeStep() {
Column[][] columns = super.region.getColumns();
for (int row = 0; row < super.region.getNumberOfRowsAlongRegionYAxis(); row++) {
for (int column = 0; column < super.region.getNumberOfColumnsAlongRegionXAxis(); column++) {
for (Neuron neuron : columns[row][column].getNeurons()) {
neuron.nextTimeStep();
for (DistalSegment distalSegment : neuron
.getDistalSegments()) {
distalSegment.nextTimeStep();
}
}
}
}
this.spatialPooler.getAlgorithmStatistics().nextTimeStep();
this.currentLearningNeurons.clear();
this.segmentUpdateList.clear();
this.predictiveColumnsAtTMinus1.addAll(this.predictiveColumnsAtTForTPlus1);
this.predictiveColumnsAtTForTPlus1.clear();
}
/**
* Compute the activeState for each Neuron in activeColumns. Then in each
* active Column a learning Neuron is chosen.
*/
void phaseOne(Set<Column> activeColumns) {
/// for c in activeColumns(t)
for (Column column : activeColumns) {
/// buPredicted = false
boolean bottomUpPredicted = false;
/// lcChosen = false
boolean learningCellChosen = false;
Neuron[] neurons = column.getNeurons();
/// for i = 0 to cellsPerColumn - 1
for (int i = 0; i < neurons.length; i++) {
/// predictiveState(c, i, t-1) == true then
if (neurons[i].getPreviousActiveState() == true) {
/// s = getActiveSegment(c, i, t-1, activeState)
DistalSegment bestSegment = neurons[i]
.getBestPreviousActiveSegment(this.spatialPooler.getAlgorithmStatistics());
/// if s.sequenceSegment == true then
if (bestSegment != null
&& bestSegment
.getSequenceStatePredictsFeedFowardInputOnNextStep()) {
/// buPredicted = true
bottomUpPredicted = true;
/// activeState(c, i, t) = 1
neurons[i].setActiveState(true);
/// if segmentActive(s, t-1, learnState) then
if (bestSegment.getPreviousActiveState()) {
/// lcChosen = true
learningCellChosen = true;
/// learnState(c, i, t) = 1
column.setLearningNeuronPosition(i);
this.currentLearningNeurons.add(neurons[i]);
}
}
}
}
/// if buPredicted == false then
if (bottomUpPredicted == false) {
/// for i = 0 to cellsPerColumn - 1
for (Neuron neuron : column.getNeurons()) {
/// activeState(c, i, t) = 1
neuron.setActiveState(true);
}
}
/// if lcChosen == false then
if (learningCellChosen == false) {
/// l,s = getBestMatchingCell(c, t-1)
int bestNeuronIndex = this.getBestMatchingNeuronIndex(column);
/// learnState(c, i, t) = 1
column.setLearningNeuronPosition(bestNeuronIndex);
this.currentLearningNeurons.add(column
.getNeuron(bestNeuronIndex));
DistalSegment segment = neurons[bestNeuronIndex]
.getBestPreviousActiveSegment(this.spatialPooler.getAlgorithmStatistics());
/// sUpdate = getSegmentActiveSynapses(c, i, s, t-1, true)
SegmentUpdate segmentUpdate = this.getSegmentActiveSynapses(
column.getCurrentPosition(), bestNeuronIndex, segment,
true, true);
/// sUpdate.sequenceSegment = true
segmentUpdate.setSequenceState(true);
segment.setSequenceState(true);
this.spatialPooler.getAlgorithmStatistics().getTP_sequenceSegmentsHistoryAndAdd(1);
/// segmentUpdateList.add(sUpdate)
this.segmentUpdateList.add(segmentUpdate);
}
}
this.spatialPooler.getAlgorithmStatistics().getTP_learningNeuronsHistoryAndAdd(this.currentLearningNeurons.size());
}
/**
* @param newSynapses Actually adding new Synapses to given segment object
* @return A segmentUpdate data structure containing a list of proposed
* changes to segment. Let activeSynapses be the list of active
* synapses where the originating cells have their activeState
* output = 1 at time step t. (This list is empty if s = -1 since
* the segment doesn't exist.) newSynapses is an optional argument
* that defaults to false. If newSynapses is true, then
* newSynapseCount - count(activeSynapses) synapses are added to
* activeSynapses. These synapses are randomly chosen from the set
* of cells that have learnState output = 1 at time step t.
*/
SegmentUpdate getSegmentActiveSynapses(ColumnPosition columnPosition,
int neuronIndex, Segment segment, boolean previousTimeStep,
boolean newSynapses) {
Set<Synapse<Cell>> activeSynapses = new HashSet<Synapse<Cell>>();
Set<Synapse<Cell>> deactiveSynapses = new HashSet<Synapse<Cell>>();
for (Synapse<Cell> synapse : segment.getSynapses()) {
if (previousTimeStep) {
if (synapse.getCell().getPreviousActiveState()) {
activeSynapses.add(synapse);
} else {
deactiveSynapses.add(synapse);
}
} else {
if (synapse.getCell().getActiveState()) {
activeSynapses.add(synapse);
} else {
deactiveSynapses.add(synapse);
}
}
}
if (newSynapses) {
activeSynapses = this
.addRandomlyChosenSynapsesFromCurrentLearningNeurons(
activeSynapses, segment, columnPosition);
}
return new SegmentUpdate(activeSynapses, deactiveSynapses,
columnPosition, neuronIndex);
}
Set<Synapse<Cell>> addRandomlyChosenSynapsesFromCurrentLearningNeurons(
Set<Synapse<Cell>> activeSynapses, Segment segment,
ColumnPosition columnPosition) {
if (this.currentLearningNeurons.size() == 0) {
throw new IllegalStateException(
"currentLearningNeurons in TemporalPooler class "
+ "addRandomlyChosenSynapsesFromCurrentLearningNeurons"
+ " method cannot be size 0");
}
int numberOfSynapsesToAdd = this.newSynapseCount
- activeSynapses.size();
List<Synapse<Cell>> potentialSynapsesToAdd = this
.generatePotentialSynapses(numberOfSynapsesToAdd,
columnPosition);
for (int i = 0; i < numberOfSynapsesToAdd; i++) {
activeSynapses.add(potentialSynapsesToAdd.get(i));
segment.addSynapse(potentialSynapsesToAdd.get(i));
}
return activeSynapses;
}
/**
* This method must never return an emtpy list.
*/
List<Synapse<Cell>> generatePotentialSynapses(int numberOfSynapsesToAdd,
ColumnPosition columnPosition) {
List<Synapse<Cell>> potentialSynapsesToAdd = new ArrayList<Synapse<Cell>>();
for (Neuron neuron : this.currentLearningNeurons) {
// it is okay if initally no learning neurons have any distal
// segments
for (DistalSegment distalSegment : neuron.getDistalSegments()) {
if (potentialSynapsesToAdd.size() >= numberOfSynapsesToAdd) {
break;
} else {
potentialSynapsesToAdd.addAll(distalSegment.getSynapses());
}
}
// it is possible potentialSynapsesToAdd.size() is still <
// numberOfSynapsesToAdd
if (potentialSynapsesToAdd.size() >= numberOfSynapsesToAdd) {
break;
}
}
// it is possible potentialSynapsesToAdd.size() is still <
// numberOfSynapsesToAdd and this is a problem if it is empty
// because then a neuron's segments will never have any new Synapses
if (numberOfSynapsesToAdd > potentialSynapsesToAdd.size()) {
potentialSynapsesToAdd = this
.createNewSynapsesConnectedToCurrentLearningNeurons(
potentialSynapsesToAdd, numberOfSynapsesToAdd,
columnPosition);
}
return potentialSynapsesToAdd;
}
List<Neuron> getCurrentLearningNeurons() {
return this.currentLearningNeurons;
}
List<Synapse<Cell>> createNewSynapsesConnectedToCurrentLearningNeurons(
List<Synapse<Cell>> potentialSynapsesToAdd,
int numberOfSynapsesToAdd, ColumnPosition columnPosition) {
int remainingNumberOfSynapsesToAdd = numberOfSynapsesToAdd
- potentialSynapsesToAdd.size();
this.spatialPooler.getAlgorithmStatistics().getTP_synapsesHistoryAndAdd(remainingNumberOfSynapsesToAdd);
int numberOfLearningNeurons = this.currentLearningNeurons.size();
if (numberOfLearningNeurons == 0) {
throw new IllegalStateException(
"currentLearningNeurons in TemporalPooler class "
+ "createNewSynapsesConnectedToCurrentLearningNeurons"
+ " method cannot be size 0");
}
int learningNeuronIndex = 0;
for (int i = 0; i < remainingNumberOfSynapsesToAdd; i++) {
Synapse<Cell> newSynapse = new Synapse<Cell>(
this.currentLearningNeurons.get(learningNeuronIndex),
columnPosition.getRow(), columnPosition.getColumn());
potentialSynapsesToAdd.add(newSynapse);
if ((learningNeuronIndex + 1) < numberOfLearningNeurons) {
learningNeuronIndex++;
} else { // wrap around and so as many different learning neurons
// are used
learningNeuronIndex = 0;
}
}
return potentialSynapsesToAdd;
}
/**
* Calculated the predictive state for each Neuron. A Neuron's
* predictiveState will be true if 1 or more distal segments becomes active.
*/
void phaseTwo() {
/// for c, i in cells
Column[][] columns = this.region.getColumns();
for (int rowIndex = 0; rowIndex < columns.length; rowIndex++) {
for (int columnIndex = 0; columnIndex < columns[0].length; columnIndex++) {
Column column = columns[rowIndex][columnIndex];
Neuron[] neurons = column.getNeurons();
for (int i = 0; i < neurons.length; i++) {
// we must compute the best segment here because
// if we compute it where it is commented out below
// then we would be iterating over the neuron's list
// of segments again
Segment predictingSegment = neurons[i]
.getBestPreviousActiveSegment(this.spatialPooler
.getAlgorithmStatistics());
/// for s in segments(c, i)
for (Segment segment : neurons[i].getDistalSegments()) {
// NOTE: segment may become active during the spatial pooling
// between temporal pooling iterations
/// if segmentActive(s, t, activeState) then
if (segment.getActiveState()) {
/// predictiveState(c, i, t) = 1
neurons[i].setPredictingState(true);
this.spatialPooler.getAlgorithmStatistics().getTP_activeDistalSegmentsHistoryAndAdd(1);
this.predictiveColumnsAtTForTPlus1.add(column
.getCurrentPosition());
/// activeUpdate = getSegmentActiveSynapses(c, i, s, t, false)
SegmentUpdate activeUpdate = this
.getSegmentActiveSynapses(
column.getCurrentPosition(), i,
segment, false, false);
/// segmentUpdateList.add(activeUpdate)
this.segmentUpdateList.add(activeUpdate);
// Segment predictingSegment = neurons[i]
// .getBestPreviousActiveSegment();
/// predSegment = getBestMatchingSegment(c, i, t-1)
/// predUpdate = getSegmentActiveSynapses(c, i, predSegment, t-1, true)
SegmentUpdate predictionUpdate = this
.getSegmentActiveSynapses(
column.getCurrentPosition(), i,
predictingSegment, true, true);
/// segmentUpdateList.add(predUpdate)
this.segmentUpdateList.add(predictionUpdate);
}
}
}
}
}
this.spatialPooler.getAlgorithmStatistics()
.getTP_predictionScoreHistoryAndAdd(super.algorithmStatistics
.computePredictionScore(this.spatialPooler.getActiveColumnPositions(), this.predictiveColumnsAtTForTPlus1));
}
/**
* Carries out learning. Segment updates that have been queued up are
* actually implemented once we get feed-forward input and a Neuron is
* chosen as a learning Neuron. Otherwise, if the Neuron ever stops
* predicting for any reason, we negatively reinforce the Segments.
*/
void phaseThree() {
/// for c, i in cells
Column[][] columns = this.region.getColumns();
for (int rowIndex = 0; rowIndex < columns.length; rowIndex++) {
for (int columnIndex = 0; columnIndex < columns[0].length; columnIndex++) {
Column column = columns[rowIndex][columnIndex];
ColumnPosition c = column.getCurrentPosition();
Neuron[] neurons = column.getNeurons();
for (int i = 0; i < neurons.length; i++) {
/// if learnState(s, i, t) == 1 then
if (i == column.getLearningNeuronPosition()) {
/// adaptSegments(segmentUpdateList(c, i), true)
this.adaptSegments(
this.segmentUpdateList.getSegmentUpdate(c, i), true);
/// segmentUpdateList(c, i).delete()
this.segmentUpdateList.deleteSegmentUpdate(c, i);
/// else if predictiveState(c, i, t) == 0 and predictiveState(c, i, t-1)==1 then
} else if (neurons[i].getPredictingState() == false
&& neurons[i].getPreviousPredictingState() == true) {
/// adaptSegments(segmentUpdateList(c, i), false)
this.adaptSegments(
this.segmentUpdateList.getSegmentUpdate(c, i),
false);
/// segmentUpdateList(c, i).delete()
this.segmentUpdateList.deleteSegmentUpdate(c, i);
}
}
}
}
}
/**
* Iterates through the Synapses of a SegmentUpdate and reinforces each
* Synapse. If positiveReinforcement is true then Synapses on the list get
* their permanenceValues incremented by permanenceIncrease. All other
* Synapses get their permanenceValue decremented by permanenceDecrease. If
* positiveReinforcement is false, then Synapses on the list get their
* permanenceValues decremented by permanenceDecrease. Finally, any Synapses
* in SegmentUpdate that do not yet exist get added with a permanenceValue
* of initialPermanence.
*/
void adaptSegments(SegmentUpdate segmentUpdate,
boolean positiveReinforcement) {
if (segmentUpdate == null) {
// the neuron being iterated over does not have any segments to
// update so skip
return;
}
Set<Synapse<Cell>> synapsesWithActiveCells = segmentUpdate
.getSynapsesWithActiveCells();
Set<Synapse<Cell>> synapsesWithDeactiveCells = segmentUpdate
.getSynpasesWithDeactiveCells();
if (positiveReinforcement) {
for (Synapse<Cell> synapse : synapsesWithActiveCells) {
synapse.increasePermanence();
}
for (Synapse<Cell> synapse : synapsesWithDeactiveCells) {
synapse.decreasePermanence();
}
} else {
for (Synapse<Cell> synapse : synapsesWithActiveCells) {
synapse.decreasePermanence();
}
}
}
/**
* @return The index of the Neuron with the Segment with the greatest number
* of active Synapses. If no best matching Segment is found, return the
* Neuron with the least number of active Synapses.
*/
int getBestMatchingNeuronIndex(Column column) {
int greatestNumberOfActiveSynapses = 0;
int bestMatchingNeuronIndex = 0;
int leastNumberOfSegments = -1;
int neuronWithLeastSegmentsIndex = -1;
boolean setNumberOfSegments = false;
Neuron[] neurons = column.getNeurons();
for (int i = 0; i < neurons.length; i++) {
int numberOfSegments = neurons[i].getDistalSegments().size();
if (!setNumberOfSegments) {
// following code should be only run the first time
leastNumberOfSegments = numberOfSegments;
neuronWithLeastSegmentsIndex = i;
setNumberOfSegments = true;
}
Segment bestSegment = neurons[i].getBestActiveSegment(this.spatialPooler.getAlgorithmStatistics());
int numberOfActiveSynapses = bestSegment.getNumberOfActiveSynapses();
if (numberOfActiveSynapses > greatestNumberOfActiveSynapses) {
greatestNumberOfActiveSynapses = numberOfActiveSynapses;
bestMatchingNeuronIndex = i;
}
// In the case all Neuron's Segments have 0 active Synapses we
// need to return Neuron with least Segments.
if (numberOfSegments < leastNumberOfSegments) {
leastNumberOfSegments = numberOfSegments;
neuronWithLeastSegmentsIndex = i;
}
}
if (greatestNumberOfActiveSynapses == 0) {
// All Segments have 0 active Synapses so we nned to return Neuron
// with least Segments.
return neuronWithLeastSegmentsIndex;
}
return bestMatchingNeuronIndex;
}
SegmentUpdateList getSegmentUpdateList() {
return this.segmentUpdateList;
}
int getNewSynapseCount() {
return this.newSynapseCount;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\n==========================================");
stringBuilder.append("\n-------TemporalPooler Information---------");
stringBuilder.append("\n biological region name: ");
stringBuilder.append(this.region.getBiologicalName());
stringBuilder.append("\n segmentUpdateList size: ");
stringBuilder.append(this.segmentUpdateList.size());
stringBuilder.append("\n newSynapseCount: ");
stringBuilder.append(this.newSynapseCount);
stringBuilder.append("\ncurrentLearningNeurons size: ");
stringBuilder.append(this.currentLearningNeurons.size());
stringBuilder.append("\n================================");
String temporalPoolerInformation = stringBuilder.toString();
return temporalPoolerInformation;
}
void computeActiveStateOfAllNeuronsInActiveColumn(Set<Column> activeColumns) {
for (Column column : activeColumns) {
boolean bottomUpPredicted = false;
for (Neuron neuron : column.getNeurons()) {
if (neuron.getPreviousActiveState() == true) {
DistalSegment bestSegment = neuron
.getBestPreviousActiveSegment(this.spatialPooler.getAlgorithmStatistics());
// Question: when is segment ever set to be sequence segment?
// Answer:
if (bestSegment != null
&& bestSegment
.getSequenceStatePredictsFeedFowardInputOnNextStep()) {
bottomUpPredicted = true;
neuron.setActiveState(true);
}
}
}
if (bottomUpPredicted == false) {
for (Neuron neuron : column.getNeurons()) {
neuron.setActiveState(true);
}
}
}
}
void computePredictiveStateOfAllNeurons() {
Column[][] columns = this.region.getColumns();
for (int rowIndex = 0; rowIndex < columns.length; rowIndex++) {
for (int columnIndex = 0; columnIndex < columns[0].length; columnIndex++) {
Column column = columns[rowIndex][columnIndex];
for (Neuron neuron : column.getNeurons()) {
for (Segment segment : neuron.getDistalSegments()) {
if (segment.getActiveState()) {
neuron.setPredictingState(true);
this.spatialPooler.getAlgorithmStatistics().getTP_activeDistalSegmentsHistoryAndAdd(1);
this.predictiveColumnsAtTForTPlus1.add(column
.getCurrentPosition());
}
}
}
}
}
this.spatialPooler.getAlgorithmStatistics()
.getTP_predictionScoreHistoryAndAdd(super.algorithmStatistics
.computePredictionScore(this.spatialPooler.getActiveColumnPositions(), this.predictiveColumnsAtTMinus1));
}
public int getNumberOfCurrentLearningNeurons() {
return this.currentLearningNeurons.size();
}
/**
* Save AlgorithmStatistics object into a .JSON file for the current Region.
*/
public void saveCurrentRegionAlgorithmStatistics(String pathAndFolderNameWithoutEndingBacklash) throws IOException {
Gson gson = new Gson();
String algorithmStatisticsInJSON = gson.toJson(this.spatialPooler.getAlgorithmStatistics());
String finalPathAndFile = pathAndFolderNameWithoutEndingBacklash +
"/region_" + region.getBiologicalName()
+ "_statistics.json";
FileInputOutput.saveObjectToTextFile(algorithmStatisticsInJSON,
finalPathAndFile);
}
}