public void TestDestroySegmentsWithTooFewSynapsesToBeMatching() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.INITIAL_PERMANENCE, .2); p = getDefaultParameters(p, KEY.MAX_NEW_SYNAPSE_COUNT, 4); p = getDefaultParameters(p, KEY.PREDICTED_SEGMENT_DECREMENT, 0.02); p.apply(cn); tm.init(cn); int[] prevActiveColumns = { 0 }; Cell[] prevActiveCells = { cn.getCell(0), cn.getCell(1), cn.getCell(2), cn.getCell(3) }; int[] activeColumns = { 2 }; Cell expectedActiveCell = cn.getCell(5); DistalDendrite matchingSegment = cn.CreateDistalSegment(cn.getCell(5)); cn.createSynapse(matchingSegment, prevActiveCells[0], .015); cn.createSynapse(matchingSegment, prevActiveCells[1], .015); cn.createSynapse(matchingSegment, prevActiveCells[2], .015); cn.createSynapse(matchingSegment, prevActiveCells[3], .015); tm.Compute(prevActiveColumns, true); tm.Compute(activeColumns, true); Assert.AreEqual(0, cn.numSegments(expectedActiveCell)); }
public void TestReinforcedCorrectlyActiveSegments() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.INITIAL_PERMANENCE, 0.2); p = getDefaultParameters(p, KEY.MAX_NEW_SYNAPSE_COUNT, 4); p = getDefaultParameters(p, KEY.PERMANENCE_DECREMENT, 0.08); p = getDefaultParameters(p, KEY.PREDICTED_SEGMENT_DECREMENT, 0.02); p.apply(cn); tm.init(cn); int[] previousActiveColumns = { 0 }; int[] activeColumns = { 1 }; Cell[] previousActiveCells = { cn.getCell(0), cn.getCell(1), cn.getCell(2), cn.getCell(3) }; Cell activeCell = cn.getCell(5); DistalDendrite activeSegment = cn.CreateDistalSegment(activeCell); Synapse as1 = cn.createSynapse(activeSegment, previousActiveCells[0], 0.5); Synapse as2 = cn.createSynapse(activeSegment, previousActiveCells[1], 0.5); Synapse as3 = cn.createSynapse(activeSegment, previousActiveCells[2], 0.5); Synapse is1 = cn.createSynapse(activeSegment, cn.getCell(81), 0.5); tm.Compute(previousActiveColumns, true); tm.Compute(activeColumns, true); Assert.AreEqual(0.6, as1.getPermanence(), 0.1); Assert.AreEqual(0.6, as2.getPermanence(), 0.1); Assert.AreEqual(0.6, as3.getPermanence(), 0.1); Assert.AreEqual(0.42, is1.getPermanence(), 0.001); }
public void testNoChangeToNonSelectedMatchingSegmentsInBurstingColumn() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.PERMANENCE_DECREMENT, 0.08); p.apply(cn); tm.init(cn); int[] previousActiveColumns = { 0 }; int[] activeColumns = { 1 }; Cell[] previousActiveCells = { cn.getCell(0), cn.getCell(1), cn.getCell(2), cn.getCell(3) }; Cell[] burstingCells = { cn.getCell(4), cn.getCell(5) }; DistalDendrite selectedMatchingSegment = cn.CreateDistalSegment(burstingCells[0]); cn.createSynapse(selectedMatchingSegment, previousActiveCells[0], 0.3); cn.createSynapse(selectedMatchingSegment, previousActiveCells[1], 0.3); cn.createSynapse(selectedMatchingSegment, previousActiveCells[2], 0.3); cn.createSynapse(selectedMatchingSegment, cn.getCell(81), 0.3); DistalDendrite otherMatchingSegment = cn.CreateDistalSegment(burstingCells[1]); Synapse as1 = cn.createSynapse(otherMatchingSegment, previousActiveCells[0], 0.3); Synapse as2 = cn.createSynapse(otherMatchingSegment, previousActiveCells[1], 0.3); Synapse is1 = cn.createSynapse(otherMatchingSegment, cn.getCell(81), 0.3); tm.Compute(previousActiveColumns, true); tm.Compute(activeColumns, true); Assert.AreEqual(0.3, as1.getPermanence(), 0.01); Assert.AreEqual(0.3, as2.getPermanence(), 0.01); Assert.AreEqual(0.3, is1.getPermanence(), 0.01); }
public void TestPredictedActiveCellsAreAlwaysWinners() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(); p.apply(cn); tm.init(cn); int[] previousActiveColumns = { 0 }; int[] activeColumns = { 1 }; Cell[] previousActiveCells = { cn.getCell(0), cn.getCell(1), cn.getCell(2), cn.getCell(3) }; List <Cell> expectedWinnerCells = new List <Cell>(cn.getCellSet(new int[] { 4, 6 })); DistalDendrite activeSegment1 = cn.CreateDistalSegment(expectedWinnerCells[0]); cn.createSynapse(activeSegment1, previousActiveCells[0], 0.5); cn.createSynapse(activeSegment1, previousActiveCells[1], 0.5); cn.createSynapse(activeSegment1, previousActiveCells[2], 0.5); DistalDendrite activeSegment2 = cn.CreateDistalSegment(expectedWinnerCells[1]); cn.createSynapse(activeSegment2, previousActiveCells[0], 0.5); cn.createSynapse(activeSegment2, previousActiveCells[1], 0.5); cn.createSynapse(activeSegment2, previousActiveCells[2], 0.5); ComputeCycle cc = tm.Compute(previousActiveColumns, false) as ComputeCycle; // learn=false cc = tm.Compute(activeColumns, false) as ComputeCycle; // learn=false Assert.IsTrue(cc.WinnerCells.SequenceEqual(new LinkedHashSet <Cell>(expectedWinnerCells))); }
public void TestZeroActiveColumns() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(); p.apply(cn); tm.init(cn); int[] previousActiveColumns = { 0 }; Cell cell4 = cn.getCell(4); DistalDendrite activeSegment = cn.CreateDistalSegment(cell4); cn.createSynapse(activeSegment, cn.getCell(0), 0.5); cn.createSynapse(activeSegment, cn.getCell(1), 0.5); cn.createSynapse(activeSegment, cn.getCell(2), 0.5); cn.createSynapse(activeSegment, cn.getCell(3), 0.5); ComputeCycle cc = tm.Compute(previousActiveColumns, true) as ComputeCycle; Assert.IsFalse(cc.ActiveCells.Count == 0); Assert.IsFalse(cc.WinnerCells.Count == 0); Assert.IsFalse(cc.predictiveCells.Count == 0); int[] zeroColumns = new int[0]; ComputeCycle cc2 = tm.Compute(zeroColumns, true) as ComputeCycle; Assert.IsTrue(cc2.ActiveCells.Count == 0); Assert.IsTrue(cc2.WinnerCells.Count == 0); Assert.IsTrue(cc2.predictiveCells.Count == 0); }
public void testDestroyWeakSynapseOnActiveReinforce() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.INITIAL_PERMANENCE, 0.2); p = getDefaultParameters(p, KEY.MAX_NEW_SYNAPSE_COUNT, 4); p = getDefaultParameters(p, KEY.PREDICTED_SEGMENT_DECREMENT, 0.02); p.apply(cn); tm.init(cn); int[] previousActiveColumns = { 0 }; Cell[] previousActiveCells = { cn.getCell(0), cn.getCell(1), cn.getCell(2), cn.getCell(3) }; int[] activeColumns = { 2 }; Cell expectedActiveCell = cn.getCell(5); DistalDendrite activeSegment = cn.CreateDistalSegment(expectedActiveCell); cn.createSynapse(activeSegment, previousActiveCells[0], 0.5); cn.createSynapse(activeSegment, previousActiveCells[1], 0.5); cn.createSynapse(activeSegment, previousActiveCells[2], 0.5); // Weak Synapse cn.createSynapse(activeSegment, previousActiveCells[3], 0.009); tm.Compute(previousActiveColumns, true); tm.Compute(activeColumns, true); Assert.AreEqual(3, cn.GetNumSynapses(activeSegment)); }
public void testRecycleLeastRecentlyActiveSegmentToMakeRoomForNewSegment() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.CELLS_PER_COLUMN, 1); p = getDefaultParameters(p, KEY.INITIAL_PERMANENCE, 0.5); p = getDefaultParameters(p, KEY.PERMANENCE_INCREMENT, 0.02); p = getDefaultParameters(p, KEY.PERMANENCE_DECREMENT, 0.02); p.Set(KEY.MAX_SEGMENTS_PER_CELL, 2); p.apply(cn); tm.init(cn); int[] prevActiveColumns1 = { 0, 1, 2 }; int[] prevActiveColumns2 = { 3, 4, 5 }; int[] prevActiveColumns3 = { 6, 7, 8 }; int[] activeColumns = { 9 }; Cell cell9 = cn.getCell(9); tm.Compute(prevActiveColumns1, true); tm.Compute(activeColumns, true); Assert.AreEqual(1, cn.getSegments(cell9).Count); DistalDendrite oldestSegment = cn.getSegments(cell9)[0]; tm.reset(cn); tm.Compute(prevActiveColumns2, true); tm.Compute(activeColumns, true); Assert.AreEqual(2, cn.getSegments(cell9).Count); //Set<Cell> oldPresynaptic = cn.getSynapses(oldestSegment) // .stream() // .map(s->s.getPresynapticCell()) // .collect(Collectors.toSet()); var oldPresynaptic = cn.getSynapses(oldestSegment).Select(s => s.getPresynapticCell()).ToList(); tm.reset(cn); tm.Compute(prevActiveColumns3, true); tm.Compute(activeColumns, true); Assert.AreEqual(2, cn.getSegments(cell9).Count); // Verify none of the segments are connected to the cells the old // segment was connected to. foreach (DistalDendrite segment in cn.getSegments(cell9)) { //Set<Cell> newPresynaptic = cn.getSynapses(segment) // .stream() // .map(s->s.getPresynapticCell()) // .collect(Collectors.toSet()); var newPresynaptic = cn.getSynapses(segment).Select(s => s.getPresynapticCell()).ToList(); Assert.IsTrue(areDisjoined <Cell>(oldPresynaptic, newPresynaptic)); } }
public void testNumberOfCells() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = Parameters.getAllDefaultParameters(); p.Set(KEY.COLUMN_DIMENSIONS, new int[] { 64, 64 }); p.Set(KEY.CELLS_PER_COLUMN, 32); p.apply(cn); tm.init(cn); Assert.AreEqual(64 * 64 * 32, cn.getCells().Length); }
public void testRecycleWeakestSynapseToMakeRoomForNewSynapse() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.CELLS_PER_COLUMN, 30); p.Set(KEY.COLUMN_DIMENSIONS, new int[] { 100 }); //p.Set(KEY.COLUMN_DIMENSIONS, new int[] { 5 }); p = getDefaultParameters(p, KEY.MIN_THRESHOLD, 1); p = getDefaultParameters(p, KEY.PERMANENCE_INCREMENT, 0.02); p = getDefaultParameters(p, KEY.PERMANENCE_DECREMENT, 0.02); p.Set(KEY.MAX_SYNAPSES_PER_SEGMENT, 3); p.apply(cn); tm.init(cn); Assert.AreEqual(3, cn.getMaxSynapsesPerSegment()); int[] prevActiveColumns = { 0, 1, 2 }; ISet <Cell> prevWinnerCells = cn.getCellSet(new int[] { 0, 1, 2 }); int[] activeColumns = { 4 }; DistalDendrite matchingSegment = cn.CreateDistalSegment(cn.getCell(4)); cn.createSynapse(matchingSegment, cn.getCell(81), 0.6); // Weakest Synapse cn.createSynapse(matchingSegment, cn.getCell(0), 0.11); ComputeCycle cc = tm.Compute(prevActiveColumns, true) as ComputeCycle; Assert.IsTrue(prevWinnerCells.SequenceEqual(cc.WinnerCells)); tm.Compute(activeColumns, true); List <Synapse> synapses = cn.getSynapses(matchingSegment); Assert.AreEqual(3, synapses.Count); //Set<Cell> presynapticCells = synapses.stream().map(s->s.getPresynapticCell()).collect(Collectors.toSet()); List <Cell> presynapticCells = new List <Cell>(); foreach (var syn in cn.getSynapses(matchingSegment)) { presynapticCells.Add(syn.getPresynapticCell()); } Assert.IsFalse(presynapticCells.Count(c => c.Index == 0) > 0); //Assert.IsFalse(presynapticCells.stream().mapToInt(cell->cell.getIndex()).anyMatch(i->i == 0)); }
public void TestBurstUnpredictedColumns() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(); p.apply(cn); tm.init(cn); int[] activeColumns = { 0 }; ISet <Cell> burstingCells = cn.getCellSet(new int[] { 0, 1, 2, 3 }); ComputeCycle cc = tm.Compute(activeColumns, true) as ComputeCycle; Assert.IsTrue(cc.ActiveCells.SequenceEqual(burstingCells)); }
public void testNoNewSegmentIfNotEnoughWinnerCells() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.MAX_NEW_SYNAPSE_COUNT, 3); p.apply(cn); tm.init(cn); int[] zeroColumns = { }; int[] activeColumns = { 0 }; tm.Compute(zeroColumns, true); tm.Compute(activeColumns, true); Assert.AreEqual(0, cn.numSegments(), 0); }
public void testAdaptSegmentToMin() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = Parameters.getAllDefaultParameters(); p.apply(cn); tm.init(cn); DistalDendrite dd = cn.CreateDistalSegment(cn.getCell(0)); Synapse s1 = cn.createSynapse(dd, cn.getCell(23), 0.1); cn.createSynapse(dd, cn.getCell(1), 0.3); tm.adaptSegment(cn, dd, cn.getCellSet(new int[] { }), cn.getPermanenceIncrement(), cn.getPermanenceDecrement()); Assert.IsFalse(cn.getSynapses(dd).Contains(s1)); }
public void testActiveSegmentGrowSynapsesAccordingToPotentialOverlap() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.CELLS_PER_COLUMN, 1); p = getDefaultParameters(p, KEY.MIN_THRESHOLD, 1); p = getDefaultParameters(p, KEY.ACTIVATION_THRESHOLD, 2); p = getDefaultParameters(p, KEY.MAX_NEW_SYNAPSE_COUNT, 4); p.apply(cn); tm.init(cn); // Use 1 cell per column so that we have easy control over the winner cells. int[] previousActiveColumns = { 0, 1, 2, 3, 4 }; List <Cell> prevWinnerCells = new List <Cell>(new Cell[] { cn.getCell(0), cn.getCell(1), cn.getCell(2), cn.getCell(3), cn.getCell(4) }); int[] activeColumns = { 5 }; DistalDendrite activeSegment = cn.CreateDistalSegment(cn.getCell(5)); cn.createSynapse(activeSegment, cn.getCell(0), 0.5); cn.createSynapse(activeSegment, cn.getCell(1), 0.5); cn.createSynapse(activeSegment, cn.getCell(2), 0.2); ComputeCycle cc = tm.Compute(previousActiveColumns, true) as ComputeCycle; Assert.IsTrue(prevWinnerCells.SequenceEqual(cc.WinnerCells)); cc = tm.Compute(activeColumns, true) as ComputeCycle; List <Cell> presynapticCells = new List <Cell>(); foreach (var syn in activeSegment.getAllSynapses(cn)) { presynapticCells.Add(syn.getPresynapticCell()); } //= cn.getSynapses(activeSegment).stream() //.map(s->s.getPresynapticCell()) //.collect(Collectors.toSet()); Assert.IsTrue( presynapticCells.Count == 4 && ( (presynapticCells.Contains(cn.getCell(0)) && presynapticCells.Contains(cn.getCell(1)) && presynapticCells.Contains(cn.getCell(2)) && presynapticCells.Contains(cn.getCell(3))) || (presynapticCells.Contains(cn.getCell(0)) && presynapticCells.Contains(cn.getCell(1)) && presynapticCells.Contains(cn.getCell(2)) && presynapticCells.Contains(cn.getCell(4))))); }
/// <summary> /// /// </summary> private void RunExperiment(int inputBits, Parameters p, EncoderBase encoder, List <double> inputValues) { Stopwatch sw = new Stopwatch(); sw.Start(); //INeuroVisualizer vis = new WSNeuroVisualizer(); //vis.InitModelAsync(new NeuroModel(null, (new long [10, 0]), 6)); int maxMatchCnt = 0; bool learn = true; //INeuroVisualizer vis = new WSNeuroVisualizer(); //GenerateNeuroModel model = new GenerateNeuroModel(); //vis.InitModel(model.CreateNeuroModel(new int[] { 1}, (long[,])p[KEY.COLUMN_DIMENSIONS], (int)p[KEY.CELLS_PER_COLUMN])); CortexNetwork net = new CortexNetwork("my cortex"); List <CortexRegion> regions = new List <CortexRegion>(); CortexRegion region0 = new CortexRegion("1st Region"); regions.Add(region0); SpatialPoolerMT sp1 = new SpatialPoolerMT(); TemporalMemory tm1 = new TemporalMemory(); var mem = new Connections(); p.apply(mem); sp1.init(mem, UnitTestHelpers.GetMemory()); tm1.init(mem); CortexLayer <object, object> layer1 = new CortexLayer <object, object>("L1"); // // NewBorn learning stage. region0.AddLayer(layer1); layer1.HtmModules.Add("encoder", encoder); layer1.HtmModules.Add("sp", sp1); HtmClassifier <double, ComputeCycle> cls = new HtmClassifier <double, ComputeCycle>(); double[] inputs = inputValues.ToArray(); int[] prevActiveCols = new int[0]; int maxSPLearningCycles = 5; List <(double Element, (int Cycle, double Similarity)[] Oscilations)> oscilationResult = new List <(double Element, (int Cycle, double Similarity)[] Oscilations)>();
public void testAdaptSegmentToMax() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = Parameters.getAllDefaultParameters(); p.apply(cn); tm.init(cn); DistalDendrite dd = cn.CreateDistalSegment(cn.getCell(0)); Synapse s1 = cn.createSynapse(dd, cn.getCell(23), 0.9); tm.adaptSegment(cn, dd, cn.getCellSet(new int[] { 23 }), cn.getPermanenceIncrement(), cn.getPermanenceDecrement()); Assert.AreEqual(1.0, s1.getPermanence(), 0.1); // Now permanence should be at max tm.adaptSegment(cn, dd, cn.getCellSet(new int[] { 23 }), cn.getPermanenceIncrement(), cn.getPermanenceDecrement()); Assert.AreEqual(1.0, s1.getPermanence(), 0.1); }
public void testLeastUsedCell() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.COLUMN_DIMENSIONS, new int[] { 2 }); p = getDefaultParameters(p, KEY.CELLS_PER_COLUMN, 2); p.apply(cn); tm.init(cn); DistalDendrite dd = cn.CreateDistalSegment(cn.getCell(0)); cn.createSynapse(dd, cn.getCell(3), 0.3); for (int i = 0; i < 100; i++) { Assert.AreEqual(1, tm.GetLeastUsedCell(cn, cn.getColumn(0).Cells, cn.getRandom()).Index); } }
public void testConnectionsNeverChangeWhenLearningDisabled() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.MAX_NEW_SYNAPSE_COUNT, 4); p = getDefaultParameters(p, KEY.PREDICTED_SEGMENT_DECREMENT, 0.02); p = getDefaultParameters(p, KEY.INITIAL_PERMANENCE, 0.2); p.apply(cn); tm.init(cn); int[] prevActiveColumns = { 0 }; Cell[] prevActiveCells = { cn.getCell(0), cn.getCell(1), cn.getCell(2), cn.getCell(3) }; int[] activeColumns = { 1, 2 }; Cell prevInactiveCell = cn.getCell(81); Cell expectedActiveCell = cn.getCell(4); DistalDendrite correctActiveSegment = cn.CreateDistalSegment(expectedActiveCell); cn.createSynapse(correctActiveSegment, prevActiveCells[0], 0.5); cn.createSynapse(correctActiveSegment, prevActiveCells[1], 0.5); cn.createSynapse(correctActiveSegment, prevActiveCells[2], 0.5); DistalDendrite wrongMatchingSegment = cn.CreateDistalSegment(cn.getCell(43)); cn.createSynapse(wrongMatchingSegment, prevActiveCells[0], 0.5); cn.createSynapse(wrongMatchingSegment, prevActiveCells[1], 0.5); cn.createSynapse(wrongMatchingSegment, prevInactiveCell, 0.5); var r = deepCopyPlain <Synapse>(cn.getReceptorSynapseMapping().Values.First().First()); var synMapBefore = deepCopyPlain <Dictionary <Cell, LinkedHashSet <Synapse> > >(cn.getReceptorSynapseMapping()); var segMapBefore = deepCopyPlain <Dictionary <Cell, List <DistalDendrite> > >(cn.getSegmentMapping()); tm.Compute(prevActiveColumns, false); tm.Compute(activeColumns, false); Assert.IsTrue(synMapBefore != cn.getReceptorSynapseMapping()); Assert.IsTrue(synMapBefore.Keys.SequenceEqual(cn.getReceptorSynapseMapping().Keys)); Assert.IsTrue(segMapBefore != cn.getSegmentMapping()); Assert.IsTrue(segMapBefore.Keys.SequenceEqual(cn.getSegmentMapping().Keys)); }
public void testMatchingSegmentAddSynapsesToAllWinnerCells() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.CELLS_PER_COLUMN, 1); p = getDefaultParameters(p, KEY.MIN_THRESHOLD, 1); p.apply(cn); tm.init(cn); int[] previousActiveColumns = { 0, 1 }; ISet <Cell> prevWinnerCells = cn.getCellSet(new int[] { 0, 1 }); int[] activeColumns = { 4 }; DistalDendrite matchingSegment = cn.CreateDistalSegment(cn.getCell(4)); cn.createSynapse(matchingSegment, cn.getCell(0), 0.5); ComputeCycle cc = tm.Compute(previousActiveColumns, true) as ComputeCycle; Assert.IsTrue(cc.WinnerCells.SequenceEqual(prevWinnerCells)); cc = tm.Compute(activeColumns, true) as ComputeCycle; List <Synapse> synapses = cn.getSynapses(matchingSegment); Assert.AreEqual(2, synapses.Count); synapses.Sort(); foreach (Synapse synapse in synapses) { if (synapse.getPresynapticCell().Index == 0) { continue; } Assert.AreEqual(0.21, synapse.getPermanence(), 0.01); Assert.AreEqual(1, synapse.getPresynapticCell().Index); } }
public void testAdaptSegment() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = Parameters.getAllDefaultParameters(); p.apply(cn); tm.init(cn); DistalDendrite dd = cn.CreateDistalSegment(cn.getCell(0)); Synapse s1 = cn.createSynapse(dd, cn.getCell(23), 0.6); Synapse s2 = cn.createSynapse(dd, cn.getCell(37), 0.4); Synapse s3 = cn.createSynapse(dd, cn.getCell(477), 0.9); tm.adaptSegment(cn, dd, cn.getCellSet(new int[] { 23, 37 }), cn.getPermanenceIncrement(), cn.getPermanenceDecrement()); Assert.AreEqual(0.7, s1.getPermanence(), 0.01); Assert.AreEqual(0.5, s2.getPermanence(), 0.01); Assert.AreEqual(0.8, s3.getPermanence(), 0.01); }
public void testNoChangeToMatchingSegmentsInPredictedActiveColumn() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(); p.apply(cn); tm.init(cn); int[] previousActiveColumns = { 0 }; int[] activeColumns = { 1 }; Cell[] previousActiveCells = { cn.getCell(0), cn.getCell(1), cn.getCell(2), cn.getCell(3) }; Cell expectedActiveCell = cn.getCell(4); List <Cell> expectedActiveCells = new List <Cell>(new Cell[] { expectedActiveCell }); Cell otherBurstingCell = cn.getCell(5); DistalDendrite activeSegment = cn.CreateDistalSegment(expectedActiveCell); cn.createSynapse(activeSegment, previousActiveCells[0], 0.5); cn.createSynapse(activeSegment, previousActiveCells[1], 0.5); cn.createSynapse(activeSegment, previousActiveCells[2], 0.5); cn.createSynapse(activeSegment, previousActiveCells[3], 0.5); DistalDendrite matchingSegmentOnSameCell = cn.CreateDistalSegment(expectedActiveCell); Synapse s1 = cn.createSynapse(matchingSegmentOnSameCell, previousActiveCells[0], 0.3); Synapse s2 = cn.createSynapse(matchingSegmentOnSameCell, previousActiveCells[1], 0.3); DistalDendrite matchingSegmentOnOtherCell = cn.CreateDistalSegment(otherBurstingCell); Synapse s3 = cn.createSynapse(matchingSegmentOnOtherCell, previousActiveCells[0], 0.3); Synapse s4 = cn.createSynapse(matchingSegmentOnOtherCell, previousActiveCells[1], 0.3); ComputeCycle cc = tm.Compute(previousActiveColumns, true) as ComputeCycle; Assert.IsTrue(cc.predictiveCells.SequenceEqual(expectedActiveCells)); tm.Compute(activeColumns, true); Assert.AreEqual(0.3, s1.getPermanence(), 0.01); Assert.AreEqual(0.3, s2.getPermanence(), 0.01); Assert.AreEqual(0.3, s3.getPermanence(), 0.01); Assert.AreEqual(0.3, s4.getPermanence(), 0.01); }
public void SerializationDeSerializationBasicTest() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(); p.apply(cn); tm.init(cn); int[] previousActiveColumns = { 0 }; int[] activeColumns = { 1 }; Cell cell4 = cn.getCell(4); ISet <Cell> expectedActiveCells = new HashSet <Cell>(new Cell[] { cell4 }); DistalDendrite activeSegment = cn.CreateDistalSegment(cell4); cn.createSynapse(activeSegment, cn.getCell(0), 0.5); cn.createSynapse(activeSegment, cn.getCell(1), 0.5); cn.createSynapse(activeSegment, cn.getCell(2), 0.5); cn.createSynapse(activeSegment, cn.getCell(3), 0.5); ComputeCycle cc = tm.Compute(previousActiveColumns, true) as ComputeCycle; Assert.IsTrue(cc.predictiveCells.SequenceEqual(expectedActiveCells)); tm.Serializer("tmSerializeNew.json"); var tm1 = TemporalMemory.Deserializer("tmSerializeNew.json"); ComputeCycle cc2 = tm1.Compute(activeColumns, true) as ComputeCycle; Assert.IsTrue(cc2.ActiveCells.SequenceEqual(expectedActiveCells)); }
public void testNewSegmentAddSynapsesToAllWinnerCells() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.MAX_NEW_SYNAPSE_COUNT, 4); p.apply(cn); tm.init(cn); int[] previousActiveColumns = { 0, 1, 2 }; int[] activeColumns = { 4 }; ComputeCycle cc = tm.Compute(previousActiveColumns, true) as ComputeCycle; List <Cell> prevWinnerCells = new List <Cell>(cc.WinnerCells); Assert.AreEqual(3, prevWinnerCells.Count); cc = tm.Compute(activeColumns, true) as ComputeCycle; List <Cell> winnerCells = new List <Cell>(cc.WinnerCells); Assert.AreEqual(1, winnerCells.Count); List <DistalDendrite> segments = winnerCells[0].getSegments(cn); //List<DistalDendrite> segments = winnerCells[0].Segments; Assert.AreEqual(1, segments.Count); List <Synapse> synapses = segments[0].getAllSynapses(cn); List <Cell> presynapticCells = new List <Cell>(); foreach (Synapse synapse in synapses) { Assert.AreEqual(0.21, synapse.getPermanence(), 0.01); presynapticCells.Add(synapse.getPresynapticCell()); } presynapticCells.Sort(); Assert.IsTrue(prevWinnerCells.SequenceEqual(presynapticCells)); }
public void testPunishMatchingSegmentsInInactiveColumns() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.MAX_NEW_SYNAPSE_COUNT, 4); p = getDefaultParameters(p, KEY.INITIAL_PERMANENCE, 0.2); p = getDefaultParameters(p, KEY.PREDICTED_SEGMENT_DECREMENT, 0.02); p.apply(cn); tm.init(cn); int[] prevActiveColumns = { 0 }; Cell[] prevActiveCells = { cn.getCell(0), cn.getCell(1), cn.getCell(2), cn.getCell(3) }; int[] activeColumns = { 1 }; Cell previousInactiveCell = cn.getCell(81); DistalDendrite activeSegment = cn.CreateDistalSegment(cn.getCell(42)); Synapse as1 = cn.createSynapse(activeSegment, prevActiveCells[0], .5); Synapse as2 = cn.createSynapse(activeSegment, prevActiveCells[1], .5); Synapse as3 = cn.createSynapse(activeSegment, prevActiveCells[2], .5); Synapse is1 = cn.createSynapse(activeSegment, previousInactiveCell, .5); DistalDendrite matchingSegment = cn.CreateDistalSegment(cn.getCell(43)); Synapse as4 = cn.createSynapse(matchingSegment, prevActiveCells[0], .5); Synapse as5 = cn.createSynapse(matchingSegment, prevActiveCells[1], .5); Synapse is2 = cn.createSynapse(matchingSegment, previousInactiveCell, .5); tm.Compute(prevActiveColumns, true); tm.Compute(activeColumns, true); Assert.AreEqual(0.48, as1.getPermanence(), 0.01); Assert.AreEqual(0.48, as2.getPermanence(), 0.01); Assert.AreEqual(0.48, as3.getPermanence(), 0.01); Assert.AreEqual(0.48, as4.getPermanence(), 0.01); Assert.AreEqual(0.48, as5.getPermanence(), 0.01); Assert.AreEqual(0.50, is1.getPermanence(), 0.01); Assert.AreEqual(0.50, is2.getPermanence(), 0.01); }
public void SerializationDeSerializationTest() { TemporalMemory tm1 = new TemporalMemory(); Connections cn1 = new Connections(); Parameters p1 = getDefaultParameters(); p1.apply(cn1); tm1.init(cn1); int[] previousActiveColumns = { 0 }; int[] activeColumns = { 1 }; Cell cell4 = cn1.getCell(4); ISet <Cell> expectedActiveCells = new HashSet <Cell>(new Cell[] { cell4 }); DistalDendrite activeSegment = cn1.CreateDistalSegment(cell4); cn1.createSynapse(activeSegment, cn1.getCell(0), 0.5); cn1.createSynapse(activeSegment, cn1.getCell(1), 0.5); cn1.createSynapse(activeSegment, cn1.getCell(2), 0.5); cn1.createSynapse(activeSegment, cn1.getCell(3), 0.5); ComputeCycle cc1 = new ComputeCycle(); for (int i = 0; i < 5; i++) { cc1 = tm1.Compute(previousActiveColumns, true); } Assert.IsTrue(cc1.predictiveCells.SequenceEqual(expectedActiveCells)); tm1.Serializer("tmTTrainSerialized.json"); string ser1 = File.ReadAllText("tmTTrainSerialized.json"); var tm2 = TemporalMemory.Deserializer("tmTTrainSerialized.json"); ComputeCycle cc2 = new ComputeCycle(); cc2 = tm2.Compute(activeColumns, true); Assert.IsTrue(cc2.ActiveCells.SequenceEqual(expectedActiveCells)); }
public void testActivateCorrectlyPredictiveCells() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(); p.apply(cn); tm.init(cn); int[] previousActiveColumns = { 0 }; int[] activeColumns = { 1 }; // Cell4 belongs to column with index 1. Cell cell4 = cn.getCell(4); // ISet<Cell> expectedActiveCells = Stream.of(cell4).collect(Collectors.toSet()); ISet <Cell> expectedActiveCells = new HashSet <Cell>(new Cell[] { cell4 }); // We add distal dentrite at column1.cell4 DistalDendrite activeSegment = cn.CreateDistalSegment(cell4); // // We add here synapses between column0.cells[0-3] and segment. cn.createSynapse(activeSegment, cn.getCell(0), 0.5); cn.createSynapse(activeSegment, cn.getCell(1), 0.5); cn.createSynapse(activeSegment, cn.getCell(2), 0.5); cn.createSynapse(activeSegment, cn.getCell(3), 0.5); ComputeCycle cc = tm.Compute(previousActiveColumns, true) as ComputeCycle; Assert.IsTrue(cc.predictiveCells.SequenceEqual(expectedActiveCells)); ComputeCycle cc2 = tm.Compute(activeColumns, true) as ComputeCycle; Assert.IsTrue(cc2.ActiveCells.SequenceEqual(expectedActiveCells)); }
public void CategorySequenceExperiment() { bool learn = true; Parameters p = Parameters.getAllDefaultParameters(); p.Set(KEY.RANDOM, new ThreadSafeRandom(42)); p.Set(KEY.INPUT_DIMENSIONS, new int[] { 100 }); p.Set(KEY.CELLS_PER_COLUMN, 30); string[] categories = new string[] { "A", "B", "C", "D" }; //string[] categories = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "K", "L" , "M", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Ö" }; CortexNetwork net = new CortexNetwork("my cortex"); List <CortexRegion> regions = new List <CortexRegion>(); CortexRegion region0 = new CortexRegion("1st Region"); regions.Add(region0); SpatialPooler sp1 = new SpatialPooler(); TemporalMemory tm1 = new TemporalMemory(); var mem = new Connections(); p.apply(mem); sp1.init(mem, UnitTestHelpers.GetMemory()); tm1.init(mem); Dictionary <string, object> settings = new Dictionary <string, object>(); //settings.Add("W", 25); settings.Add("N", 100); //settings.Add("Radius", 1); EncoderBase encoder = new CategoryEncoder(categories, settings); //encoder.Encode() CortexLayer <object, object> layer1 = new CortexLayer <object, object>("L1"); region0.AddLayer(layer1); layer1.HtmModules.Add("encoder", encoder); layer1.HtmModules.Add("sp", sp1); //layer1.HtmModules.Add(tm1); //layer1.Compute(); //IClassifier<string, ComputeCycle> cls = new HtmClassifier<string, ComputeCycle>(); HtmClassifier <string, ComputeCycle> cls = new HtmClassifier <string, ComputeCycle>(); HtmUnionClassifier <string, ComputeCycle> cls1 = new HtmUnionClassifier <string, ComputeCycle>(); //string[] inputs = new string[] { "A", "B", "C", "D" }; string[] inputs = new string[] { "A", "B", "C", "D" }; // // This trains SP. foreach (var input in inputs) { Debug.WriteLine($" ** {input} **"); for (int i = 0; i < 3; i++) { var lyrOut = layer1.Compute((object)input, learn) as ComputeCycle; } } sp1.Serializer("spCSTSerialized.json"); var sp2 = SpatialPooler.Deserializer("spCSTSerialized.json"); layer1.HtmModules.Remove("sp"); layer1.HtmModules.Add("sp", sp2); // Here we add TM module to the layer. layer1.HtmModules.Add("tm", tm1); // // Now, training with SP+TM. SP is pretrained on pattern. for (int i = 0; i < 200; i++) { foreach (var input in inputs) { var lyrOut = layer1.Compute(input, learn) as ComputeCycle; //cls1.Learn(input, lyrOut.activeCells.ToArray(), learn); //Debug.WriteLine($"Current Input: {input}"); cls.Learn(input, lyrOut.ActiveCells.ToArray(), lyrOut.predictiveCells.ToArray()); Debug.WriteLine($"Current Input: {input}"); if (learn == false) { Debug.WriteLine($"Predict Input When Not Learn: {cls.GetPredictedInputValue(lyrOut.predictiveCells.ToArray())}"); } else { Debug.WriteLine($"Predict Input: {cls.GetPredictedInputValue(lyrOut.predictiveCells.ToArray())}"); } Debug.WriteLine("-----------------------------------------------------------\n----------------------------------------------------------"); } if (i == 10) { Debug.WriteLine("Stop Learning From Here----------------------------"); learn = false; } // tm1.reset(mem); } Debug.WriteLine("------------------------------------------------------------------------\n----------------------------------------------------------------------------"); /* * learn = false; * for (int i = 0; i < 19; i++) * { * foreach (var input in inputs) * { * layer1.Compute((object)input, learn); * } * } */ sp1.Serialize("tm.serialize.json"); }
public void SimpleSequenceExperiment() { int inputBits = 50; //int inputBits = 5; bool learn = true; Parameters p = Parameters.getAllDefaultParameters(); p.Set(KEY.RANDOM, new ThreadSafeRandom(42)); p.Set(KEY.INPUT_DIMENSIONS, new int[] { inputBits }); //p.Set(KEY.CELLS_PER_COLUMN, 100); p.Set(KEY.CELLS_PER_COLUMN, 5); p.Set(KEY.COLUMN_DIMENSIONS, new int[] { 500 }); //p.Set(KEY.COLUMN_DIMENSIONS, new int[] { 5 }); //p.setStimulusThreshold(1); //p.setMinThreshold(1); CortexNetwork net = new CortexNetwork("my cortex"); List <CortexRegion> regions = new List <CortexRegion>(); CortexRegion region0 = new CortexRegion("1st Region"); regions.Add(region0); SpatialPoolerMT sp1 = new SpatialPoolerMT(); TemporalMemory tm1 = new TemporalMemory(); var mem = new Connections(); p.apply(mem); sp1.init(mem, UnitTestHelpers.GetMemory()); tm1.init(mem); Dictionary <string, object> settings = new Dictionary <string, object>() { { "W", 7 }, //{ "W", 1}, { "N", inputBits }, { "Radius", -1.0 }, { "MinVal", 0.0 }, // { "MaxVal", 20.0 }, { "Periodic", false }, { "Name", "scalar" }, { "ClipInput", false }, }; double max = 10; List <double> lst = new List <double>(); for (double i = max - 1; i >= 0; i--) { lst.Add(i); } settings["MaxVal"] = max; EncoderBase encoder = new ScalarEncoder(settings); CortexLayer <object, object> layer1 = new CortexLayer <object, object>("L1"); // // NewBorn learning stage. region0.AddLayer(layer1); layer1.HtmModules.Add("encoder", encoder); layer1.HtmModules.Add("sp", sp1); HtmClassifier <double, ComputeCycle> cls = new HtmClassifier <double, ComputeCycle>(); double[] inputs = lst.ToArray(); // // This trains SP. foreach (var input in inputs) { Debug.WriteLine($" ** {input} **"); for (int i = 0; i < 3; i++) { var lyrOut = layer1.Compute((object)input, learn) as ComputeCycle; } } // Here we add TM module to the layer. layer1.HtmModules.Add("tm", tm1); int cycle = 0; int matches = 0; double lastPredictedValue = 0; // // Now, training with SP+TM. SP is pretrained on pattern. for (int i = 0; i < 460; i++) { matches = 0; cycle++; foreach (var input in inputs) { var lyrOut = layer1.Compute(input, learn) as ComputeCycle; cls.Learn(input, lyrOut.ActiveCells.ToArray(), lyrOut.predictiveCells.ToArray()); Debug.WriteLine($"-------------- {input} ---------------"); if (learn == false) { Debug.WriteLine($"Inference mode"); } Debug.WriteLine($"W: {Helpers.StringifyVector(lyrOut.WinnerCells.Select(c => c.Index).ToArray())}"); Debug.WriteLine($"P: {Helpers.StringifyVector(lyrOut.predictiveCells.Select(c => c.Index).ToArray())}"); var predictedValue = cls.GetPredictedInputValue(lyrOut.predictiveCells.ToArray()); Debug.WriteLine($"Current Input: {input} \t| - Predicted value in previous cycle: {lastPredictedValue} \t| Predicted Input for the next cycle: {predictedValue}"); if (input == lastPredictedValue) { matches++; Debug.WriteLine($"Match {input}"); } else { Debug.WriteLine($"Missmatch Actual value: {input} - Predicted value: {lastPredictedValue}"); } lastPredictedValue = predictedValue; } if (i == 500) { Debug.WriteLine("Stop Learning From Here. Entering inference mode."); learn = false; } //tm1.reset(mem); Debug.WriteLine($"Cycle: {cycle}\tMatches={matches} of {inputs.Length}\t {(double)matches / (double)inputs.Length * 100.0}%"); } cls.TraceState(); Debug.WriteLine("------------------------------------------------------------------------\n----------------------------------------------------------------------------"); }
public void RunPowerPredictionExperiment() { const int inputBits = 300; /* without datetime component */ // 13420; /* with 4096 scalar bits */ // 10404 /* with 1024 bits */; Parameters p = Parameters.getAllDefaultParameters(); p.Set(KEY.RANDOM, new ThreadSafeRandom(42)); p.Set(KEY.COLUMN_DIMENSIONS, new int[] { 2048 }); p.Set(KEY.INPUT_DIMENSIONS, new int[] { inputBits }); p.Set(KEY.CELLS_PER_COLUMN, 10 /* 50 */); p.Set(KEY.GLOBAL_INHIBITION, true); p.Set(KEY.CONNECTED_PERMANENCE, 0.1); // N of 40 (40= 0.02*2048 columns) active cells required to activate the segment. p.setNumActiveColumnsPerInhArea(0.02 * 2048); // Activation threshold is 10 active cells of 40 cells in inhibition area. p.setActivationThreshold(10 /*15*/); p.setInhibitionRadius(15); p.Set(KEY.MAX_BOOST, 0.0); //p.Set(KEY.GLOBAL_INHIBITION, true); //p.Set(KEY.MAX_SYNAPSES_PER_SEGMENT, 32); //p.Set(KEY.MAX_SEGMENTS_PER_CELL, 128); //p.Set(KEY.MAX_NEW_SYNAPSE_COUNT, 200); //p.Set(KEY.POTENTIAL_RADIUS, 700); //p.Set(KEY.POTENTIAL_PCT, 0.5); //p.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 42); //p.Set(KEY.LOCAL_AREA_DENSITY, -1); CortexRegion region0 = new CortexRegion("1st Region"); SpatialPoolerMT sp1 = new SpatialPoolerMT(); TemporalMemory tm1 = new TemporalMemory(); var mem = new Connections(); p.apply(mem); sp1.init(mem, UnitTestHelpers.GetMemory()); tm1.init(mem); Dictionary <string, object> settings = new Dictionary <string, object>(); CortexLayer <object, object> layer1 = new CortexLayer <object, object>("L1"); region0.AddLayer(layer1); layer1.HtmModules.Add("sp", sp1); HtmClassifier <double, ComputeCycle> cls = new HtmClassifier <double, ComputeCycle>(); Stopwatch sw = new Stopwatch(); sw.Start(); Train(inputBits, layer1, cls, true); // New born mode. sw.Stop(); Debug.WriteLine($"NewBorn stage duration: {sw.ElapsedMilliseconds / 1000} s"); layer1.AddModule("tm", tm1); sw.Start(); int hunderdAccCnt = 0; for (int i = 0; i < 1000; i++) { float acc = Train(inputBits, layer1, cls, false); Debug.WriteLine($"Accuracy = {acc}, Cycle = {i}"); if (acc == 100.0) { hunderdAccCnt++; } if (hunderdAccCnt >= 10) { break; } //tm1.reset(mem); } if (hunderdAccCnt >= 10) { Debug.WriteLine($"EXPERIMENT SUCCESS. Accurracy 100% reached."); } else { Debug.WriteLine($"Experiment FAILED!. Accurracy 100% was not reached."); } cls.TraceState(); sw.Stop(); Debug.WriteLine($"Training duration: {sw.ElapsedMilliseconds / 1000} s"); }
public void testAddSegmentToCellWithFewestSegments() { bool grewOnCell1 = false; bool grewOnCell2 = false; for (int seed = 0; seed < 100; seed++) { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.MAX_NEW_SYNAPSE_COUNT, 4); p = getDefaultParameters(p, KEY.PREDICTED_SEGMENT_DECREMENT, 0.02); p = getDefaultParameters(p, KEY.SEED, seed); p.apply(cn); tm.init(cn); int[] prevActiveColumns = { 1, 2, 3, 4 }; Cell[] prevActiveCells = { cn.getCell(4), cn.getCell(5), cn.getCell(6), cn.getCell(7) }; int[] activeColumns = { 0 }; Cell[] nonMatchingCells = { cn.getCell(0), cn.getCell(3) }; ISet <Cell> activeCells = cn.getCellSet(new int[] { 0, 1, 2, 3 }); DistalDendrite segment1 = cn.CreateDistalSegment(nonMatchingCells[0]); cn.createSynapse(segment1, prevActiveCells[0], 0.5); DistalDendrite segment2 = cn.CreateDistalSegment(nonMatchingCells[1]); cn.createSynapse(segment2, prevActiveCells[1], 0.5); tm.Compute(prevActiveColumns, true); ComputeCycle cc = tm.Compute(activeColumns, true) as ComputeCycle; Assert.IsTrue(cc.ActiveCells.SequenceEqual(activeCells)); Assert.AreEqual(3, cn.numSegments()); Assert.AreEqual(1, cn.numSegments(cn.getCell(0))); Assert.AreEqual(1, cn.numSegments(cn.getCell(3))); Assert.AreEqual(1, cn.GetNumSynapses(segment1)); Assert.AreEqual(1, cn.GetNumSynapses(segment2)); List <DistalDendrite> segments = new List <DistalDendrite>(cn.getSegments(cn.getCell(1))); if (segments.Count == 0) { List <DistalDendrite> segments2 = cn.getSegments(cn.getCell(2)); Assert.IsFalse(segments2.Count == 0); grewOnCell2 = true; segments.AddRange(segments2); } else { grewOnCell1 = true; } Assert.AreEqual(1, segments.Count); List <Synapse> synapses = segments[0].getAllSynapses(cn); Assert.AreEqual(4, synapses.Count); ISet <Column> columnCheckList = cn.getColumnSet(prevActiveColumns); foreach (Synapse synapse in synapses) { Assert.AreEqual(0.2, synapse.getPermanence(), 0.01); var parentColIndx = synapse.getPresynapticCell().getParentColumnIndex(); Column column = cn.getMemory().GetColumn(parentColIndx); Assert.IsTrue(columnCheckList.Contains(column)); columnCheckList.Remove(column); } Assert.AreEqual(0, columnCheckList.Count); } Assert.IsTrue(grewOnCell1); Assert.IsTrue(grewOnCell2); }