public void TestReinforcedSelectedMatchingSegmentInBurstingColumn() { 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 activeSegment = cn.CreateDistalSegment(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.CreateDistalSegment(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(previousActiveColumns, true); tm.Compute(activeColumns, true); Assert.AreEqual(0.4, as1.Permanence, 0.01); Assert.AreEqual(0.4, as2.Permanence, 0.01); Assert.AreEqual(0.4, as3.Permanence, 0.01); Assert.AreEqual(0.22, is1.Permanence, 0.001); }
public void TestObjectGroup() { Column c0 = new Column(9, 0); Column c1 = new Column(9, 1); // Illustrates the Cell's actual index = colIndex * cellsPerColumn + indexOfCellWithinCol Assert.AreEqual(7, c0.GetCell(7).GetIndex()); Assert.AreEqual(12, c1.GetCell(3).GetIndex()); Assert.AreEqual(16, c1.GetCell(7).GetIndex()); DistalDendrite dd0 = new DistalDendrite(c0.GetCell(7), 0, 0, 0); DistalDendrite dd1 = new DistalDendrite(c1.GetCell(3 /* Col 1's Cells start at 9 */), 1, 0, 1); DistalDendrite dd2 = new DistalDendrite(c1.GetCell(7 /* Col 1's Cells start at 9 */), 2, 0, 2); List <DistalDendrite> l = new List <DistalDendrite> { dd0, dd1, dd2 }; //@SuppressWarnings("unchecked") List <Tuple <DistalDendrite, Column> > expected = new List <Tuple <DistalDendrite, Column> > { new Tuple <DistalDendrite, Column>(dd0, c0), new Tuple <DistalDendrite, Column>(dd1, c1), new Tuple <DistalDendrite, Column>(dd2, c1) }; GroupBy <DistalDendrite, Column> grouper = GroupBy <DistalDendrite, Column> .Of(l, d => d.GetParentCell().GetColumn()); int i = 0; foreach (Tuple <DistalDendrite, Column> p in grouper) { Assert.AreEqual(expected[i++], p); } }
public void TestColumnSerialize() { //Thread.Sleep(5000); //Pool pool = new Pool(10, 10); Cell cell = new Cell(); DistalDendrite dist = new DistalDendrite(cell, 0, 0, 0, 0.5, 10); dist.Synapses.Add(new Synapse(cell, null, 0, 0.5)); Synapse syn = new Synapse(cell, dist, 0, 0); var dict = UnitTestHelpers.GetMemory(); Column col = new Column(10, 0, 0.01, 10); col.ProximalDendrite = new ProximalDendrite(0, 0.01, 10); col.ProximalDendrite.Synapses = new List <Synapse>(); col.ProximalDendrite.Synapses.Add(syn); dict.ColumnDictionary.Add(0, col); var serCol = AkkaSb.Net.ActorReference.SerializeMsg(col); Assert.IsTrue(dict.ColumnDictionary[0].ProximalDendrite.Synapses[0].Segment != null); }
public void testDestroyWeakSynapseOnActiveReinforce() { 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.PREDICTED_SEGMENT_DECREMENT, 0.02); p.Apply(cn); TemporalMemory.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.CreateSegment(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(cn, previousActiveColumns, true); tm.Compute(cn, activeColumns, true); Assert.AreEqual(3, cn.GetNumSynapses(activeSegment)); }
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 testNoChangeToNonSelectedMatchingSegmentsInBurstingColumn() { 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 selectedMatchingSegment = cn.CreateSegment(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.CreateSegment(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(cn, previousActiveColumns, true); tm.Compute(cn, 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); }
/** * Creates nDesiredNewSynapes synapses on the segment passed in if * possible, choosing random cells from the previous winner cells that are * not already on the segment. * <p> * <b>Notes:</b> The process of writing the last value into the index in the array * that was most recently changed is to ensure the same results that we get * in the c++ implementation using iter_swap with vectors. * </p> * * @param conn Connections instance for the tm * @param prevWinnerCells Winner cells in `t-1` * @param segment Segment to grow synapses on. * @param initialPermanence Initial permanence of a new synapse. * @param nDesiredNewSynapses Desired number of synapses to grow * @param random Tm object used to generate random * numbers */ public void GrowSynapses(Connections conn, HashSet <Cell> prevWinnerCells, DistalDendrite segment, double initialPermanence, int nDesiredNewSynapses, IRandom random) { List <Cell> candidates = new List <Cell>(prevWinnerCells); candidates.Sort(); //Collections.sort(candidates); foreach (Synapse synapse in conn.GetSynapses(segment)) { Cell presynapticCell = synapse.GetPresynapticCell(); int index = candidates.IndexOf(presynapticCell); if (index != -1) { candidates.RemoveAt(index); } } int candidatesLength = candidates.Count; int nActual = nDesiredNewSynapses < candidatesLength ? nDesiredNewSynapses : candidatesLength; for (int i = 0; i < nActual; i++) { int rand = random.NextInt(candidates.Count); conn.CreateSynapse(segment, candidates[rand], initialPermanence); candidates.RemoveAt(rand); } }
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 TestActivateCorrectlyPredictiveCells() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(); p.Apply(cn); TemporalMemory.Init(cn); int[] previousActiveColumns = { 0 }; int[] activeColumns = { 1 }; Cell cell4 = cn.GetCell(4); HashSet <Cell> expectedActiveCells = new HashSet <Cell> { cell4 }; //Stream.of(cell4).collect(Collectors.toSet()); 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.IsTrue(cc.PredictiveCells().SetEquals(expectedActiveCells)); ComputeCycle cc2 = tm.Compute(cn, activeColumns, true); Assert.IsTrue(cc2.ActiveCells().SetEquals(expectedActiveCells)); }
public void SerializeConnectionsTest() { int[] inputDims = { 3, 4, 5 }; int[] columnDims = { 35, 43, 52 }; HtmConfig cfg = new HtmConfig(inputDims, columnDims); Connections connections = new Connections(cfg); Cell cells = new Cell(12, 14, 16, 18, new CellActivity()); var distSeg1 = new DistalDendrite(cells, 1, 2, 2, 1.0, 100); var distSeg2 = new DistalDendrite(cells, 44, 24, 34, 1.0, 100); connections.ActiveSegments.Add(distSeg1); using (StreamWriter sw = new StreamWriter($"ser_{nameof(SerializeConnectionsTest)}.txt")) { connections.Serialize(sw); } using (StreamReader sr = new StreamReader($"ser_{nameof(SerializeConnectionsTest)}.txt")) { Connections connections1 = Connections.Deserialize(sr); Assert.IsTrue(connections.Equals(connections1)); } }
public void testDestroySegmentsWithTooFewSynapsesToBeMatching() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = GetDefaultParameters(null, Parameters.KEY.INITIAL_PERMANENCE, .2); p = GetDefaultParameters(p, Parameters.KEY.MAX_NEW_SYNAPSE_COUNT, 4); 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 = { 2 }; Cell expectedActiveCell = cn.GetCell(5); DistalDendrite matchingSegment = cn.CreateSegment(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(cn, prevActiveColumns, true); tm.Compute(cn, activeColumns, true); Assert.AreEqual(0, cn.GetNumSegments(expectedActiveCell)); }
public void SerializeDistalDendriteList() { HtmSerializer2 htm = new HtmSerializer2(); Cell cell = new Cell(1, 1, 1, 1, new CellActivity()); List <DistalDendrite> distals = new List <DistalDendrite>(); distals.Add(new DistalDendrite(cell, 1, 2, 2, 1.0, 100)); distals.Add(new DistalDendrite(cell, 44, 24, 34, 1.0, 100)); List <DistalDendrite> distals2 = new List <DistalDendrite>(); distals2.Add(new DistalDendrite(cell, 1, 2, 2, 1.0, 100)); distals2.Add(new DistalDendrite(cell, 44, 24, 34, 1.0, 100)); Assert.IsTrue(distals.SequenceEqual(distals2)); using (StreamWriter sw = new StreamWriter($"ser_{nameof(SerializeDistalDendriteList)}")) { htm.SerializeBegin("UnitTest", sw); htm.SerializeValue(distals, sw); htm.SerializeEnd("UnitTest", sw); } using (StreamReader sr = new StreamReader($"ser_{nameof(SerializeDistalDendriteList)}")) { List <DistalDendrite> distals1 = new List <DistalDendrite>(); while (sr.Peek() > 0) { string str = sr.ReadLine(); if (str == htm.ReadBegin(nameof(DistalDendrite))) { distals1.Add(DistalDendrite.Deserialize(sr)); } } } }
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 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)); } }
/** * Activates all of the cells in an unpredicted active column, * chooses a winner cell, and, if learning is turned on, either adapts or * creates a segment. growSynapses is invoked on this segment. * </p><p> * <b>Pseudocode:</b> * </p><p> * <pre> * mark all cells as active * if there are any matching distal dendrite segments * find the most active matching segment * mark its cell as a winner cell * (learning) * grow and reinforce synapses to previous winner cells * else * find the cell with the least segments, mark it as a winner cell * (learning) * (optimization) if there are previous winner cells * add a segment to this winner cell * grow synapses to previous winner cells * </pre> * </p> * * @param conn Connections instance for the TM * @param column Bursting {@link Column} * @param matchingSegments List of matching {@link DistalDendrite}s * @param prevActiveCells Active cells in `t-1` * @param prevWinnerCells Winner cells in `t-1` * @param permanenceIncrement Amount by which permanences of synapses * are decremented during learning * @param permanenceDecrement Amount by which permanences of synapses * are incremented during learning * @param random Random number generator * @param learn Whether or not learning is enabled * * @return Tuple containing: * cells list of the processed column's cells * bestCell the best cell */ public BurstingResult BurstColumn(Connections conn, Column column, List <DistalDendrite> matchingSegments, ICollection <Cell> prevActiveCells, ICollection <Cell> prevWinnerCells, double permanenceIncrement, double permanenceDecrement, Random random, bool learn) { IList <Cell> cells = column.Cells; Cell leastUsedCell = null; // // Matching segments result from number of potential synapses. These are segments with number of potential // synapses permanence higher than some minimum threshold value. // Potential synapses are synapses from presynaptc cells connected to the active cell. // In other words, presynaptic cells define a statistical prediction that active cell will become the active in the next cycle. // Bursting will create new segments if there are no matching segments until some matching segments appear. // Once that happen, segment adoption will start. // If some matching segments exist, bursting will grab the segment with most potential synapses and adapt it. if (matchingSegments != null && matchingSegments.Count > 0) { DistalDendrite maxPotentialSeg = getSegmentwithHighesPotential(conn, matchingSegments); for (int i = 0; i < matchingSegments.Count; i++) { matchingSegments[i].getIndex(); } leastUsedCell = maxPotentialSeg.GetParentCell(); if (learn) { adaptSegment(conn, maxPotentialSeg, prevActiveCells, permanenceIncrement, permanenceDecrement); int nGrowDesired = conn.getMaxNewSynapseCount() - conn.getLastActivity().PotentialSynapses[maxPotentialSeg.getIndex()]; if (nGrowDesired > 0) { growSynapses(conn, prevWinnerCells, maxPotentialSeg, conn.getInitialPermanence(), nGrowDesired, random); } } } else { leastUsedCell = this.GetLeastUsedCell(conn, cells, random); if (learn) { int nGrowExact = Math.Min(conn.getMaxNewSynapseCount(), prevWinnerCells.Count); if (nGrowExact > 0) { DistalDendrite bestSegment = conn.CreateDistalSegment(leastUsedCell); growSynapses(conn, prevWinnerCells, bestSegment, conn.getInitialPermanence(), nGrowExact, random); } } } return(new BurstingResult(cells, leastUsedCell)); }
public void SerializeArrayCell() { HtmSerializer2 htm = new HtmSerializer2(); Cell[] cells = new Cell[2]; Cell[] cells1; cells[0] = new Cell(12, 14, 16, 18, new CellActivity()); var distSeg1 = new DistalDendrite(cells[0], 1, 2, 2, 1.0, 100); cells[0].DistalDendrites.Add(distSeg1); var distSeg2 = new DistalDendrite(cells[0], 44, 24, 34, 1.0, 100); cells[0].DistalDendrites.Add(distSeg2); Cell preSynapticcell = new Cell(11, 14, 16, 18, new CellActivity()); var synapse1 = new Synapse(cells[0], distSeg1.SegmentIndex, 23, 1.0); preSynapticcell.ReceptorSynapses.Add(synapse1); var synapse2 = new Synapse(cells[0], distSeg2.SegmentIndex, 27, 1.0); preSynapticcell.ReceptorSynapses.Add(synapse2); cells[1] = new Cell(2, 4, 1, 8, new CellActivity()); var distSeg3 = new DistalDendrite(cells[1], 3, 4, 7, 1.0, 100); cells[1].DistalDendrites.Add(distSeg3); var distSeg4 = new DistalDendrite(cells[1], 4, 34, 94, 1.0, 100); cells[1].DistalDendrites.Add(distSeg4); Cell preSynapticcell1 = new Cell(1, 1, 6, 8, new CellActivity()); var synapse3 = new Synapse(cells[1], distSeg3.SegmentIndex, 23, 1.0); preSynapticcell.ReceptorSynapses.Add(synapse1); var synapse4 = new Synapse(cells[1], distSeg4.SegmentIndex, 27, 1.0); preSynapticcell.ReceptorSynapses.Add(synapse2); using (StreamWriter sw = new StreamWriter($"ser_{nameof(SerializeArrayCell)}.txt")) { htm.SerializeValue(cells, sw); } using (StreamReader sr = new StreamReader($"ser_{nameof(SerializeArrayCell)}.txt")) { //cells1 = htm.DeserializeCellArray(data,sr); } //Assert.IsTrue(cells.SequenceEqual(cells1)); }
public void CompareDentriteList() { Cell c1 = new Cell(1, 1, 10, 1, NeoCortexEntities.NeuroVisualizer.CellActivity.ActiveCell); Cell c2 = new Cell(1, 1, 10, 1, NeoCortexEntities.NeuroVisualizer.CellActivity.ActiveCell); Cell c3 = new Cell(2, 1, 10, 1, NeoCortexEntities.NeuroVisualizer.CellActivity.ActiveCell); DistalDendrite d1 = new DistalDendrite(c1, 1, 1, 1, 0.5, 10); DistalDendrite d2 = new DistalDendrite(c1, 1, 1, 1, 0.5, 10); DistalDendrite d3 = new DistalDendrite(c1, 2, 1, 1, 0.5, 10); DistalDendrite d4 = new DistalDendrite(c2, 2, 2, 1, 0.5, 10); List <DistalDendrite> list1 = new List <DistalDendrite>() { d1, d2, d2 }; List <DistalDendrite> list2 = new List <DistalDendrite>() { d1, d2, d3 }; // Not same by reference Assert.IsFalse(d1 == d2); // d1 and d2 are same by value. Assert.IsTrue(d1.Equals(d2)); // d1 and d3 are NOT same by value. Assert.IsFalse(d1.Equals(d3)); // d3 and d4 are NOT same by value. Assert.IsFalse(d3.Equals(d4)); // Lists are same by value. list1.SequenceEqual(list2); // All same by references. list2 = new List <DistalDendrite>() { d1, d1, d1 }; // Lists are strill same by value. list1.SequenceEqual(list2); list2 = new List <DistalDendrite>() { d1, d2, d3 }; Assert.IsTrue(d1.Equals(d2)); Assert.IsFalse(d1.Equals(d3)); }
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 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)); } }
/** * Updates synapses on segment. * Strengthens active synapses; weakens inactive synapses. * * @param conn {@link Connections} instance for the tm * @param segment {@link DistalDendrite} to adapt * @param prevActiveCells Active {@link Cell}s in `t-1` * @param permanenceIncrement Amount to increment active synapses * @param permanenceDecrement Amount to decrement inactive synapses */ public void adaptSegment(Connections conn, DistalDendrite segment, ICollection <Cell> prevActiveCells, double permanenceIncrement, double permanenceDecrement) { // Destroying a synapse modifies the set that we're iterating through. List <Synapse> synapsesToDestroy = new List <Synapse>(); foreach (Synapse synapse in conn.getSynapses(segment)) { double permanence = synapse.getPermanence(); // // If synapse's presynaptic cell was active in the previous cycle then streng it. if (prevActiveCells.Contains(synapse.getPresynapticCell())) { permanence += permanenceIncrement; } else { permanence -= permanenceDecrement; } // Keep permanence within min/max bounds permanence = permanence <0 ? 0 : permanence> 1.0 ? 1.0 : permanence; // Use this to examine issues caused by subtle floating point differences // be careful to set the scale (1 below) to the max significant digits right of the decimal point // between the permanenceIncrement and initialPermanence // // permanence = new BigDecimal(permanence).setScale(1, RoundingMode.HALF_UP).doubleValue(); if (permanence < EPSILON) { synapsesToDestroy.Add(synapse); } else { synapse.setPermanence(conn.getSynPermConnected(), permanence); } } foreach (Synapse s in synapsesToDestroy) { conn.destroySynapse(s, segment); } if (conn.GetNumSynapses(segment) == 0) { conn.destroySegment(segment); } }
public void CompareDentrites() { Cell c1 = new Cell(1, 1, 10, 1, NeoCortexEntities.NeuroVisualizer.CellActivity.ActiveCell); Cell c2 = new Cell(1, 1, 10, 1, NeoCortexEntities.NeuroVisualizer.CellActivity.ActiveCell); Cell c3 = new Cell(2, 1, 10, 1, NeoCortexEntities.NeuroVisualizer.CellActivity.ActiveCell); DistalDendrite d1 = new DistalDendrite(c1, 1, 1, 1, 0.5, 10); DistalDendrite d2 = new DistalDendrite(c1, 1, 1, 1, 0.5, 10); DistalDendrite d3 = new DistalDendrite(c1, 2, 1, 1, 0.5, 10); Assert.IsTrue(d1.Equals(d2)); Assert.IsFalse(d1.Equals(d3)); }
/// <summary> /// Gets the segment with maximal potential. Segment's potential is measured by number of potential synapses. /// </summary> /// <param name="conn"></param> /// <param name="matchingSegments"></param> /// <returns></returns> private DistalDendrite getSegmentwithHighesPotential(Connections conn, List <DistalDendrite> matchingSegments) { // int[] numActPotential = conn.getLastActivity().numActivePotential; DistalDendrite maxSeg = matchingSegments[0]; for (int i = 0; i < matchingSegments.Count - 1; i++) { if (conn.getLastActivity().PotentialSynapses[matchingSegments[i + 1].getIndex()] > conn.getLastActivity().PotentialSynapses[matchingSegments[i].getIndex()]) { maxSeg = matchingSegments[i + 1]; } } return(maxSeg); }
/** * Activates all of the cells in an unpredicted active column, * chooses a winner cell, and, if learning is turned on, either adapts or * creates a segment. growSynapses is invoked on this segment. * </p><p> * <b>Pseudocode:</b> * </p><p> * <pre> * mark all cells as active * if there are any matching distal dendrite segments * find the most active matching segment * mark its cell as a winner cell * (learning) * grow and reinforce synapses to previous winner cells * else * find the cell with the least segments, mark it as a winner cell * (learning) * (optimization) if there are previous winner cells * add a segment to this winner cell * grow synapses to previous winner cells * </pre> * </p> * * @param conn Connections instance for the TM * @param column Bursting {@link Column} * @param matchingSegments List of matching {@link DistalDendrite}s * @param prevActiveCells Active cells in `t-1` * @param prevWinnerCells Winner cells in `t-1` * @param permanenceIncrement Amount by which permanences of synapses * are decremented during learning * @param permanenceDecrement Amount by which permanences of synapses * are incremented during learning * @param random Random number generator * @param learn Whether or not learning is enabled * * @return Tuple containing: * cells list of the processed column's cells * bestCell the best cell */ public Tuple BurstColumn(Connections conn, Column column, List <DistalDendrite> matchingSegments, HashSet <Cell> prevActiveCells, HashSet <Cell> prevWinnerCells, double permanenceIncrement, double permanenceDecrement, IRandom random, bool learn) { IList <Cell> cells = column.GetCells(); Cell bestCell = null; if (matchingSegments.Any()) { int[] numPoten = conn.GetLastActivity().numActivePotential; Comparison <DistalDendrite> cmp = (dd1, dd2) => numPoten[dd1.GetIndex()] - numPoten[dd2.GetIndex()]; var sortedSegments = new List <DistalDendrite>(matchingSegments); sortedSegments.Sort(cmp); DistalDendrite bestSegment = sortedSegments.Last(); bestCell = bestSegment.GetParentCell(); if (learn) { AdaptSegment(conn, bestSegment, prevActiveCells, permanenceIncrement, permanenceDecrement); int nGrowDesired = conn.GetMaxNewSynapseCount() - numPoten[bestSegment.GetIndex()]; if (nGrowDesired > 0) { GrowSynapses(conn, prevWinnerCells, bestSegment, conn.GetInitialPermanence(), nGrowDesired, random); } } } else { bestCell = LeastUsedCell(conn, cells, random); if (learn) { int nGrowExact = Math.Min(conn.GetMaxNewSynapseCount(), prevWinnerCells.Count); if (nGrowExact > 0) { DistalDendrite bestSegment = conn.CreateSegment(bestCell); GrowSynapses(conn, prevWinnerCells, bestSegment, conn.GetInitialPermanence(), nGrowExact, random); } } } return(new Tuple(cells, bestCell)); }
public void testAdaptSegmentToMin() { TemporalMemory tm = new TemporalMemory(); Connections cn = new Connections(); Parameters p = Parameters.GetAllDefaultParameters(); p.Apply(cn); TemporalMemory.Init(cn); DistalDendrite dd = cn.CreateSegment(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(), cn.GetPermanenceIncrement(), cn.GetPermanenceDecrement()); Assert.IsFalse(cn.GetSynapses(dd).Contains(s1)); }
public void SerializePoolTest(int size, int numInputs) { Pool pool = new Pool(size, numInputs); //pool.m_SynapseConnections = new List<int>(); //pool.m_SynapseConnections.Add(34); //pool.m_SynapseConnections.Add(87); //pool.m_SynapseConnections.Add(44); Cell cell = new Cell(12, 14, 16, 18, new CellActivity()); var distSeg1 = new DistalDendrite(cell, 1, 2, 2, 1.0, 100); cell.DistalDendrites.Add(distSeg1); var distSeg2 = new DistalDendrite(cell, 44, 24, 34, 1.0, 100); cell.DistalDendrites.Add(distSeg2); Cell preSynapticcell = new Cell(11, 14, 16, 28, new CellActivity()); var synapse1 = new Synapse(cell, distSeg1.SegmentIndex, 23, 1.0); preSynapticcell.ReceptorSynapses.Add(synapse1); var synapse2 = new Synapse(cell, distSeg2.SegmentIndex, 27, 1.0); preSynapticcell.ReceptorSynapses.Add(synapse2); pool.m_SynapsesBySourceIndex = new Dictionary <int, Synapse>(); pool.m_SynapsesBySourceIndex.Add(3, synapse1); pool.m_SynapsesBySourceIndex.Add(67, synapse2); using (StreamWriter sw = new StreamWriter($"ser_{nameof(SerializePoolTest)}.txt")) { pool.Serialize(sw); } using (StreamReader sr = new StreamReader($"ser_{nameof(SerializePoolTest)}.txt")) { Pool pool1 = Pool.Deserialize(sr); Assert.IsTrue(pool1.Equals(pool)); } }
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 SerializeSegmentTest() { //TODO Cell cell = new Cell(12, 14, 16, 18, new CellActivity()); Segment seg = new DistalDendrite(cell, 1, 2, 2, 1.0, 100); using (StreamWriter sw = new StreamWriter($"ser_{nameof(SerializeSegmentTest)}.txt")) { seg.Serialize(sw); } using (StreamReader sr = new StreamReader($"ser_{nameof(SerializeSegmentTest)}.txt")) { //Segment segment1 = Segment.Deserialize(sr); // Assert.IsTrue(segment1.Equals(seg)); } }
public void SerializeDistalDendrite() { Cell cell = new Cell(1, 1, 1, 1, new CellActivity()); DistalDendrite distal = new DistalDendrite(cell, 1, 2, 2, 1.0, 100); //distal.Synapses.Add(new Synapse(cell, 1, 23, 1.0)); //distal.Synapses.Add(new Synapse(cell, 3, 27, 1.0)); using (StreamWriter sw = new StreamWriter($"ser_{nameof(SerializeDistalDendrite)}.txt")) { distal.Serialize(sw); } using (StreamReader sr = new StreamReader($"ser_{nameof(SerializeDistalDendrite)}.txt")) { DistalDendrite distal1 = DistalDendrite.Deserialize(sr); Assert.IsTrue(distal1.Equals(distal)); } }
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); TemporalMemory.AdaptSegment(cn, dd, cn.GetCellSet(new int[] { }), cn.HtmConfig.PermanenceIncrement, cn.HtmConfig.PermanenceDecrement); //DD Assert.IsFalse(cn.GetSynapses(dd).Contains(s1)); Assert.IsFalse(dd.Synapses.Contains(s1)); }
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())); }