public void testZeroActiveColumns() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(); p.Apply(cn); TemporalMemory.Init(cn); int[] previousActiveColumns = { 0 }; Cell cell4 = cn.GetCell(4); DistalDendrite activeSegment = cn.CreateSegment(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(cn, previousActiveColumns, true); 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(cn, zeroColumns, true); 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, activeSegment.Synapses.Count); }
public void testPredictedActiveCellsAreAlwaysWinners() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(); p.Apply(cn); TemporalMemory.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.CreateSegment(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.CreateSegment(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(cn, previousActiveColumns, false); // learn=false cc = tm.Compute(cn, activeColumns, false); // learn=false Assert.IsTrue(cc.winnerCells.SetEquals(new HashSet <Cell>(expectedWinnerCells))); }
public void testReinforcedCorrectlyActiveSegments() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(null, Parameters.KEY.INITIAL_PERMANENCE, 0.2); p = GetDefaultParameters(p, Parameters.KEY.MAX_NEW_SYNAPSE_COUNT, 4); p = GetDefaultParameters(p, Parameters.KEY.PERMANENCE_DECREMENT, 0.08); p = GetDefaultParameters(p, Parameters.KEY.PREDICTED_SEGMENT_DECREMENT, 0.02); p.Apply(cn); TemporalMemory.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.CreateSegment(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(cn, previousActiveColumns, true); tm.Compute(cn, 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 testReinforcedSelectedMatchingSegmentInBurstingColumn() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(null, Parameters.KEY.PERMANENCE_DECREMENT, 0.08); p.Apply(cn); TemporalMemory.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 activeSegment = cn.CreateSegment(burstingCells[0]); Synapse as1 = cn.CreateSynapse(activeSegment, previousActiveCells[0], 0.3); Synapse as2 = cn.CreateSynapse(activeSegment, previousActiveCells[0], 0.3); Synapse as3 = cn.CreateSynapse(activeSegment, previousActiveCells[0], 0.3); Synapse is1 = cn.CreateSynapse(activeSegment, cn.GetCell(81), 0.3); DistalDendrite otherMatchingSegment = cn.CreateSegment(burstingCells[1]); cn.CreateSynapse(otherMatchingSegment, previousActiveCells[0], 0.3); cn.CreateSynapse(otherMatchingSegment, previousActiveCells[1], 0.3); cn.CreateSynapse(otherMatchingSegment, cn.GetCell(81), 0.3); tm.Compute(cn, previousActiveColumns, true); tm.Compute(cn, activeColumns, true); Assert.AreEqual(0.4, as1.GetPermanence(), 0.01); Assert.AreEqual(0.4, as2.GetPermanence(), 0.01); Assert.AreEqual(0.4, as3.GetPermanence(), 0.01); Assert.AreEqual(0.22, 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.Permanence, 0.01); Assert.AreEqual(0.3, as2.Permanence, 0.01); Assert.AreEqual(0.3, is1.Permanence, 0.01); }
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 TestRecycleWeakestSynapseToMakeRoomForNewSynapse() { throw new AssertInconclusiveException("Not fixed."); 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.HtmConfig.MaxSynapsesPerSegment); int[] prevActiveColumns = { 0, 1, 2 }; IList <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); //DD List<Synapse> synapses = cn.GetSynapses(matchingSegment); List <Synapse> synapses = matchingSegment.Synapses; Assert.AreEqual(3, synapses.Count); //Set<Cell> presynapticCells = synapses.stream().map(s->s.getPresynapticCell()).collect(Collectors.toSet()); List <Cell> presynapticCells = new List <Cell>(); //DD foreach (var syn in cn.GetSynapses(matchingSegment)) foreach (var syn in matchingSegment.Synapses) { 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 TestClear() { int[] input1 = { 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0 }; int[] input2 = { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 }; int[] input3 = { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 }; int[] input4 = { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0 }; int[] input5 = { 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0 }; int[] input6 = { 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 }; int[] input7 = { 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }; int[][] inputs = { input1, input2, input3, input4, input5, input6, input7 }; Parameters p = GetParameters(); Connections con = new Connections(); p.Apply(con); TemporalMemory tm = new TemporalMemory(); TemporalMemory.Init(con); for (int x = 0; x < 602; x++) { foreach (int[] i in inputs) { tm.Compute(con, i.Where(n => n == 1).ToArray(), true); } } Assert.IsFalse(!con.GetActiveCells().Any()); con.Clear(); Assert.IsTrue(!con.GetActiveCells().Any()); }
public void TestTemporalMemoryExplicit() { int[] input1 = new int[] { 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0 }; int[] input2 = new int[] { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 }; int[] input3 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 }; int[] input4 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0 }; int[] input5 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0 }; int[] input6 = new int[] { 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 }; int[] input7 = new int[] { 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }; int[][] inputs = { input1, input2, input3, input4, input5, input6, input7 }; Parameters p = GetParameters(); Connections con = new Connections(); p.Apply(con); TemporalMemory tm = new TemporalMemory(); TemporalMemory.Init(con); ComputeCycle cc = null; for (int x = 0; x < 602; x++) { foreach (int[] i in inputs) { cc = tm.Compute(con, ArrayUtils.Where(i, ArrayUtils.WHERE_1), true); } } TEST_AGGREGATION[TM_EXPL] = SDR.AsCellIndices(cc.activeCells); }
public void testNoNewSegmentIfNotEnoughWinnerCells() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(null, Parameters.KEY.MAX_NEW_SYNAPSE_COUNT, 3); p.Apply(cn); TemporalMemory.Init(cn); int[] zeroColumns = { }; int[] activeColumns = { 0 }; tm.Compute(cn, zeroColumns, true); tm.Compute(cn, activeColumns, true); Assert.AreEqual(0, cn.GetNumSegments(), 0); }
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))))); }
public void testActiveSegmentGrowSynapsesAccordingToPotentialOverlap() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(null, Parameters.KEY.CELLS_PER_COLUMN, 1); p = GetDefaultParameters(p, Parameters.KEY.MIN_THRESHOLD, 1); p = GetDefaultParameters(p, Parameters.KEY.ACTIVATION_THRESHOLD, 2); p = GetDefaultParameters(p, Parameters.KEY.MAX_NEW_SYNAPSE_COUNT, 4); p.Apply(cn); TemporalMemory.Init(cn); // Use 1 cell per column so that we have easy control over the winner cells. int[] previousActiveColumns = { 0, 1, 2, 3, 4 }; HashSet <Cell> prevWinnerCells = new HashSet <Cell>(Arrays.AsList(0, 1, 2, 3, 4) .Select(i => cn.GetCell(i)) .ToList()); int[] activeColumns = { 5 }; DistalDendrite activeSegment = cn.CreateSegment(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(cn, previousActiveColumns, true); Assert.IsTrue(prevWinnerCells.SetEquals(cc.WinnerCells())); cc = tm.Compute(cn, activeColumns, true); HashSet <Cell> presynapticCells = new HashSet <Cell>(cn.GetSynapses(activeSegment) .Select(s => s.GetPresynapticCell()) .ToList()); Assert.IsTrue( presynapticCells.Count == 4 && ( !presynapticCells.Except(new List <Cell> { cn.GetCell(0), cn.GetCell(1), cn.GetCell(2), cn.GetCell(3) }).Any() || !presynapticCells.Except(new List <Cell> { cn.GetCell(0), cn.GetCell(1), cn.GetCell(2), cn.GetCell(4) }).Any())); }
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); //DD //List<DistalDendrite> segments = winnerCells[0].GetSegments(cn); List <DistalDendrite> segments = winnerCells[0].DistalDendrites; //List<DistalDendrite> segments = winnerCells[0].Segments; Assert.AreEqual(1, segments.Count); //List<Synapse> synapses = segments[0].GetAllSynapses(cn); List <Synapse> synapses = segments[0].Synapses; List <Cell> presynapticCells = new List <Cell>(); foreach (Synapse synapse in synapses) { Assert.AreEqual(0.21, synapse.Permanence, 0.01); presynapticCells.Add(synapse.GetPresynapticCell()); } presynapticCells.Sort(); Assert.IsTrue(prevWinnerCells.SequenceEqual(presynapticCells)); }
public void testNoChangeToMatchingSegmentsInPredictedActiveColumn() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(); p.Apply(cn); TemporalMemory.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); HashSet <Cell> expectedActiveCells = new HashSet <Cell> { expectedActiveCell }; // Stream.of(expectedActiveCell).collect(Collectors.toCollection(HashSet < Cell >::new)); Cell otherBurstingCell = cn.GetCell(5); DistalDendrite activeSegment = cn.CreateSegment(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.CreateSegment(expectedActiveCell); Synapse s1 = cn.CreateSynapse(matchingSegmentOnSameCell, previousActiveCells[0], 0.3); Synapse s2 = cn.CreateSynapse(matchingSegmentOnSameCell, previousActiveCells[1], 0.3); DistalDendrite matchingSegmentOnOtherCell = cn.CreateSegment(otherBurstingCell); Synapse s3 = cn.CreateSynapse(matchingSegmentOnOtherCell, previousActiveCells[0], 0.3); Synapse s4 = cn.CreateSynapse(matchingSegmentOnOtherCell, previousActiveCells[1], 0.3); ComputeCycle cc = tm.Compute(cn, previousActiveColumns, true); Assert.IsTrue(cc.PredictiveCells().SetEquals(expectedActiveCells)); tm.Compute(cn, 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 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 }; IList <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; //DD List<Synapse> synapses = cn.GetSynapses(matchingSegment); List <Synapse> synapses = matchingSegment.Synapses; Assert.AreEqual(2, synapses.Count); synapses.Sort(); foreach (Synapse synapse in synapses) { if (synapse.GetPresynapticCell().Index == 0) { continue; } Assert.AreEqual(0.21, synapse.Permanence, 0.01); Assert.AreEqual(1, synapse.GetPresynapticCell().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 testMatchingSegmentAddSynapsesToSubsetOfWinnerCells() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(null, Parameters.KEY.CELLS_PER_COLUMN, 1); p = GetDefaultParameters(p, Parameters.KEY.MIN_THRESHOLD, 1); p.Apply(cn); TemporalMemory.Init(cn); int[] previousActiveColumns = { 0, 1, 2, 3 }; HashSet <Cell> prevWinnerCells = cn.GetCellSet(new int[] { 0, 1, 2, 3 }); int[] activeColumns = { 4 }; DistalDendrite matchingSegment = cn.CreateSegment(cn.GetCell(4)); cn.CreateSynapse(matchingSegment, cn.GetCell(0), 0.5); ComputeCycle cc = tm.Compute(cn, previousActiveColumns, true); Assert.IsTrue(cc.WinnerCells().SetEquals(prevWinnerCells)); cc = tm.Compute(cn, activeColumns, true); List <Synapse> synapses = cn.GetSynapses(matchingSegment); Assert.AreEqual(3, synapses.Count); synapses.Sort(); foreach (Synapse synapse in synapses) { if (synapse.GetPresynapticCell().GetIndex() == 0) { continue; } Assert.AreEqual(0.21, synapse.GetPermanence(), 0.01); Assert.IsTrue(synapse.GetPresynapticCell().GetIndex() == 1 || synapse.GetPresynapticCell().GetIndex() == 2 || synapse.GetPresynapticCell().GetIndex() == 3); } }
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.Permanence, 0.01); Assert.AreEqual(0.3, s2.Permanence, 0.01); Assert.AreEqual(0.3, s3.Permanence, 0.01); Assert.AreEqual(0.3, s4.Permanence, 0.01); }
public void testConnectionsNeverChangeWhenLearningDisabled() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(null, Parameters.KEY.MAX_NEW_SYNAPSE_COUNT, 4); p = GetDefaultParameters(p, Parameters.KEY.PREDICTED_SEGMENT_DECREMENT, 0.02); p = GetDefaultParameters(p, Parameters.KEY.INITIAL_PERMANENCE, 0.2); p.Apply(cn); TemporalMemory.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.CreateSegment(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.CreateSegment(cn.GetCell(43)); cn.CreateSynapse(wrongMatchingSegment, prevActiveCells[0], 0.5); cn.CreateSynapse(wrongMatchingSegment, prevActiveCells[1], 0.5); cn.CreateSynapse(wrongMatchingSegment, prevInactiveCell, 0.5); Map <Cell, HashSet <Synapse> > synMapBefore = DeepCopyPlain(cn.GetReceptorSynapseMapping()); Map <Cell, List <DistalDendrite> > segMapBefore = DeepCopyPlain(cn.GetSegmentMapping()); tm.Compute(cn, prevActiveColumns, false); tm.Compute(cn, activeColumns, false); Assert.IsTrue(synMapBefore != cn.GetReceptorSynapseMapping()); Assert.AreEqual(synMapBefore, cn.GetReceptorSynapseMapping()); Assert.IsTrue(segMapBefore != cn.GetSegmentMapping()); Assert.AreEqual(segMapBefore, cn.GetSegmentMapping()); }
public void testNewSegmentAddSynapsesToSubsetOfWinnerCells() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = getDefaultParameters(null, KEY.MAX_NEW_SYNAPSE_COUNT, 2); p.apply(cn); tm.init(cn); int[] previousActiveColumns = { 0, 1, 2 }; int[] activeColumns = { 4 }; ComputeCycle cc = tm.Compute(previousActiveColumns, true) as ComputeCycle; ISet <Cell> prevWinnerCells = 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 = cn.getSynapses(segments[0]); Assert.AreEqual(2, synapses.Count); foreach (Synapse synapse in synapses) { Assert.AreEqual(0.21, synapse.getPermanence(), 0.01); Assert.IsTrue(prevWinnerCells.Contains(synapse.getPresynapticCell())); } }
public void testRecycleWeakestSynapseToMakeRoomForNewSynapse() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(null, Parameters.KEY.CELLS_PER_COLUMN, 1); p.SetParameterByKey(Parameters.KEY.COLUMN_DIMENSIONS, new int[] { 100 }); p = GetDefaultParameters(p, Parameters.KEY.MIN_THRESHOLD, 1); p = GetDefaultParameters(p, Parameters.KEY.PERMANENCE_INCREMENT, 0.02); p = GetDefaultParameters(p, Parameters.KEY.PERMANENCE_DECREMENT, 0.02); p.SetParameterByKey(Parameters.KEY.MAX_SYNAPSES_PER_SEGMENT, 3); p.Apply(cn); TemporalMemory.Init(cn); Assert.AreEqual(3, cn.GetMaxSynapsesPerSegment()); int[] prevActiveColumns = { 0, 1, 2 }; HashSet <Cell> prevWinnerCells = cn.GetCellSet(new int[] { 0, 1, 2 }); int[] activeColumns = { 4 }; DistalDendrite matchingSegment = cn.CreateSegment(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(cn, prevActiveColumns, true); Assert.IsTrue(prevWinnerCells.SetEquals(cc.winnerCells)); tm.Compute(cn, activeColumns, true); List <Synapse> synapses = cn.GetSynapses(matchingSegment); Assert.AreEqual(3, synapses.Count); HashSet <Cell> presynapticCells = new HashSet <Cell>(synapses.Select(s => s.GetPresynapticCell())); Assert.IsFalse(presynapticCells.Select(cell => cell.GetIndex()).Any(i => i == 0)); }
public void testPunishMatchingSegmentsInInactiveColumns() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(null, Parameters.KEY.MAX_NEW_SYNAPSE_COUNT, 4); p = GetDefaultParameters(p, Parameters.KEY.INITIAL_PERMANENCE, 0.2); p = GetDefaultParameters(p, Parameters.KEY.PREDICTED_SEGMENT_DECREMENT, 0.02); p.Apply(cn); TemporalMemory.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.CreateSegment(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.CreateSegment(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(cn, prevActiveColumns, true); tm.Compute(cn, 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 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 TestActivateCorrectlyPredictiveCells(int tmImplementation) { TemporalMemory tm = tmImplementation == 0 ? new TemporalMemory() : new TemporalMemoryMT(); 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 testBurstUnpredictedColumns() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(); p.Apply(cn); TemporalMemory.Init(cn); int[] activeColumns = { 0 }; HashSet <Cell> burstingCells = cn.GetCellSet(new int[] { 0, 1, 2, 3 }); ComputeCycle cc = tm.Compute(cn, activeColumns, true); Assert.IsTrue(cc.ActiveCells().SetEquals(burstingCells)); }
public void TestBurstUnpredictedColumns1() { HtmConfig htmConfig = GetDefaultTMParameters(); Connections cn = new Connections(htmConfig); TemporalMemory tm = new TemporalMemory(); tm.Init(cn); int[] activeColumns = { 0 }; IList <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 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 testRecycleLeastRecentlyActiveSegmentToMakeRoomForNewSegment() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(null, Parameters.KEY.CELLS_PER_COLUMN, 1); p = GetDefaultParameters(p, Parameters.KEY.INITIAL_PERMANENCE, 0.5); p = GetDefaultParameters(p, Parameters.KEY.PERMANENCE_INCREMENT, 0.02); p = GetDefaultParameters(p, Parameters.KEY.PERMANENCE_DECREMENT, 0.02); p.SetParameterByKey(Parameters.KEY.MAX_SEGMENTS_PER_CELL, 2); p.Apply(cn); TemporalMemory.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(cn, prevActiveColumns1, true); tm.Compute(cn, activeColumns, true); Assert.AreEqual(1, cn.GetSegments(cell9).Count); DistalDendrite oldestSegment = cn.GetSegments(cell9)[0]; tm.Reset(cn); tm.Compute(cn, prevActiveColumns2, true); tm.Compute(cn, activeColumns, true); Assert.AreEqual(2, cn.GetSegments(cell9).Count); HashSet <Cell> oldPresynaptic = new HashSet <Cell>(cn.GetSynapses(oldestSegment) .Select(s => s.GetPresynapticCell())); tm.Reset(cn); tm.Compute(cn, prevActiveColumns3, true); tm.Compute(cn, 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)) { HashSet <Cell> newPresynaptic = new HashSet <Cell>(cn.GetSynapses(segment) .Select(s => s.GetPresynapticCell())); //.collect(Collectors.toSet()); Assert.IsFalse(oldPresynaptic.Overlaps(newPresynaptic)); //Assert.IsTrue(Collections.disjoint(oldPresynaptic, newPresynaptic)); } }
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)); }