def testSerialization1(self): # This test verifies that pickle works for pickle of a Classifier SDR1 = SDR(15); SDR1.sparse = [1, 5, 9] SDR2 = SDR(15); SDR2.sparse = [0, 6, 9, 11] SDR3 = SDR(15); SDR3.sparse = [6, 9] SDR4 = SDR(15); SDR4.sparse = [1, 5, 9] c1 = Classifier() c1.learn(pattern=SDR1, classification=4) c1.learn(pattern=SDR2, classification=5) c1.learn(pattern=SDR3, classification=5) c1.learn(pattern=SDR4, classification=4) c1.learn(pattern=SDR4, classification=4) serialized = pickle.dumps(c1) c2 = pickle.loads(serialized) result1 = c1.infer(SDR1) result2 = c2.infer(SDR1) #print(" testSerialization1 result: %.6f, %.6f, %.6f, %.6f, %.6f, %.6f "%( result1[0], result1[1], result1[2], result1[3], result1[4], result1[5])); self.assertEqual(len(result1), 6) self.assertAlmostEqual(result1[0], 0.166344, places=5) self.assertAlmostEqual(result1[1], 0.166344, places=5) self.assertAlmostEqual(result1[2], 0.166344, places=5) self.assertAlmostEqual(result1[3], 0.166344, places=5) self.assertAlmostEqual(result1[4], 0.167847, places=5) self.assertAlmostEqual(result1[5], 0.166777, places=5) self.assertEqual(len(result1), len(result2)) for i in range(len(result1)): self.assertAlmostEqual(result1[i], result2[i], places=5)
def _trainThalamus(self, t): # Learn L6Pattern = SDR(t.l6CellCount) L6Pattern.sparse = [0, 1, 2, 3, 4, 5] t.learnL6Pattern(L6Pattern, [(0, 0), (2, 3)]) L6Pattern.sparse = [6, 7, 8, 9, 10] t.learnL6Pattern(L6Pattern, [(1, 1), (3, 4)])
def testExampleUsage(self): # Make an SDR with 9 values, arranged in a (3 x 3) grid. X = SDR(dimensions=(3, 3)) # These three statements are equivalent. X.dense = [[0, 1, 0], [0, 1, 0], [0, 0, 1]] assert (X.dense.tolist() == [[0, 1, 0], [0, 1, 0], [0, 0, 1]]) assert ([list(v) for v in X.coordinates] == [[0, 1, 2], [1, 1, 2]]) assert (list(X.sparse) == [1, 4, 8]) X.coordinates = [[0, 1, 2], [1, 1, 2]] assert (X.dense.tolist() == [[0, 1, 0], [0, 1, 0], [0, 0, 1]]) assert ([list(v) for v in X.coordinates] == [[0, 1, 2], [1, 1, 2]]) assert (list(X.sparse) == [1, 4, 8]) X.sparse = [1, 4, 8] # Access data in any format, SDR will automatically convert data formats, # even if it was not the format used by the most recent assignment to the # SDR. assert (X.dense.tolist() == [[0, 1, 0], [0, 1, 0], [0, 0, 1]]) assert ([list(v) for v in X.coordinates] == [[0, 1, 2], [1, 1, 2]]) assert (list(X.sparse) == [1, 4, 8]) # Data format conversions are cached, and when an SDR value changes the # cache is cleared. X.sparse = [1, 2, 3] # Assign new data to the SDR, clearing the cache. X.dense # This line will convert formats. X.dense # This line will resuse the result of the previous line X = SDR((1000, 1000)) data = X.dense data[0, 4] = 1 data[444, 444] = 1 X.dense = data assert (list(X.sparse) == [4, 444444])
def testComputeComplex(self): c = Predictor([1], 1.0) inp = SDR(100) inp.sparse = [1, 5, 9] c.learn(recordNum=0, pattern=inp, classification=4,) inp.sparse = [0, 6, 9, 11] c.learn(recordNum=1, pattern=inp, classification=5,) inp.sparse = [6, 9] c.learn(recordNum=2, pattern=inp, classification=5,) inp.sparse = [1, 5, 9] c.learn(recordNum=3, pattern=inp, classification=4,) inp.sparse = [1, 5, 9] result = c.infer(pattern=inp) self.assertSetEqual(set(result.keys()), set([1])) self.assertEqual(len(result[1]), 6) self.assertAlmostEqual(result[1][0], 0.034234, places=5) self.assertAlmostEqual(result[1][1], 0.034234, places=5) self.assertAlmostEqual(result[1][2], 0.034234, places=5) self.assertAlmostEqual(result[1][3], 0.034234, places=5) self.assertAlmostEqual(result[1][4], 0.093058, places=5) self.assertAlmostEqual(result[1][5], 0.770004, places=5)
def testPredictionMultipleCategories(self): """ Test the distribution of predictions. Here, we intend the classifier to learn the associations: [1,3,5] => bucketIdx 0 & 1 [2,4,6] => bucketIdx 2 & 3 The classifier should get the distribution almost right given enough repetitions and a small learning rate """ c = Classifier(0.001) SDR1 = SDR(10) SDR1.sparse = [1, 3, 5] SDR2 = SDR(10) SDR2.sparse = [2, 4, 6] random.seed(42) for _ in range(5000): c.learn(pattern=SDR1, classification=[0, 1]) c.learn(pattern=SDR2, classification=[2, 3]) result1 = c.infer(pattern=SDR1) self.assertAlmostEqual(result1[0], 0.5, places=1) self.assertAlmostEqual(result1[1], 0.5, places=1) result2 = c.infer(pattern=SDR2) self.assertAlmostEqual(result2[2], 0.5, places=1) self.assertAlmostEqual(result2[3], 0.5, places=1)
def testExampleUsage(self): A = SDR(10) B = SDR(10) X = SDR(A.dimensions) A.sparse = [0, 1, 2, 3] B.sparse = [2, 3, 4, 5] X.intersection(A, B) assert (set(X.sparse) == set([2, 3]))
def testExampleUsage(self): A = SDR(10) B = SDR(10) U = SDR(A.dimensions) A.sparse = [0, 1, 2, 3] B.sparse = [2, 3, 4, 5] U.union(A, B) assert (set(U.sparse) == set([0, 1, 2, 3, 4, 5]))
def testExampleUsage(self): A = SDR(10) B = SDR(10) C = SDR(20) A.sparse = [0, 1, 2] B.sparse = [0, 1, 2] C.concatenate(A, B) assert (set(C.sparse) == set([0, 1, 2, 10, 11, 12]))
def compute(self, activeColumns, basalInput, apicalInput=(), basalGrowthCandidates=None, apicalGrowthCandidates=None, learn=True): """ Perform one timestep. Use the basal and apical input to form a set of predictions, then activate the specified columns, then learn. @param activeColumns (numpy array) List of active columns @param basalInput (numpy array) List of active input bits for the basal dendrite segments @param apicalInput (numpy array) List of active input bits for the apical dendrite segments @param basalGrowthCandidates (numpy array or None) List of bits that the active cells may grow new basal synapses to. If None, the basalInput is assumed to be growth candidates. @param apicalGrowthCandidates (numpy array or None) List of bits that the active cells may grow new apical synapses to If None, the apicalInput is assumed to be growth candidates. @param learn (bool) Whether to grow / reinforce / punish synapses """ activeColumns = np.asarray(activeColumns) apicalInputSDR = SDR(self.apicalInputSize) apicalInputSDR.sparse = apicalInput basalInputSDR = SDR(self.basalInputSize) basalInputSDR.sparse = basalInput if basalGrowthCandidates is None: basalGrowthCandidates = basalInput basalGrowthCandidates = np.asarray(basalGrowthCandidates) if apicalGrowthCandidates is None: apicalGrowthCandidates = apicalInput apicalGrowthCandidates = np.asarray(apicalGrowthCandidates) self.depolarizeCells(basalInputSDR, apicalInputSDR, learn) apicalInputSDR.sparse = apicalInput basalInputSDR.sparse = basalInput self.activateCells(activeColumns, basalInputSDR, apicalInputSDR, basalGrowthCandidates, apicalGrowthCandidates, learn)
def testNumConnectedSynapses(self): """ Test that connections are generated on predefined segments. """ random = Random(1981) active_cells = np.array(random.sample( np.arange(0, NUM_CELLS, 1, dtype="uint32"), 40), dtype="uint32") active_cells.sort() presynaptic_input = list(range(0, 10)) presynaptic_input_set = set(presynaptic_input) inputSDR = SDR(1024) inputSDR.sparse = presynaptic_input connections = Connections(NUM_CELLS, 0.2) for i in range(NUM_CELLS): seg = connections.createSegment(i, 1) for cell in active_cells: segments = connections.segmentsForCell(cell) segment = segments[0] for c in presynaptic_input: connections.createSynapse(segment, c, 0.1) connections.adaptSegment(segment, inputSDR, 0.1, 0.0, False) connected_synapses = connections.numConnectedSynapses(segment) self.assertEqual(connected_synapses, len(presynaptic_input), "Missing synapses") presynaptic_input1 = list(range(0, 5)) presynaptic_input_set1 = set(presynaptic_input1) inputSDR.sparse = presynaptic_input1 total_connected = 0 for cell in active_cells: segments = connections.segmentsForCell(cell) segment = segments[0] connections.adaptSegment(segment, inputSDR, 0.0, 0.1, False) connected_synapses = connections.numConnectedSynapses(segment) self.assertEqual(connected_synapses, len(presynaptic_input1), "Missing synapses") total_connected += connected_synapses connected_synapses = connections.numSynapses(segment) self.assertEqual(connected_synapses, len(presynaptic_input), "Missing synapses") self.assertEqual(total_connected, len(presynaptic_input1) * 40, "Missing synapses")
def sensoryCompute(self, anchorInput, anchorGrowthCandidates, learn): """ This is called when the sensor senses something """ anchorInputSDR = SDR(self.anchorInputSize) if learn: anchorInputSDR.sparse = anchorGrowthCandidates self._sensoryComputeLearningMode(anchorInputSDR) else: anchorInputSDR.sparse = anchorInput self._sensoryComputeInferenceMode(anchorInputSDR)
def testAdaptShouldDecrementSynapses(self): """ Test that connections are generated on predefined segments. """ random = Random(1981) active_cells = np.array(random.sample( np.arange(0, NUM_CELLS, 1, dtype="uint32"), 40), dtype="uint32") active_cells.sort() presynaptic_input = list(range(0, 10)) presynaptic_input_set = set(presynaptic_input) inputSDR = SDR(1024) inputSDR.sparse = presynaptic_input connections = Connections(NUM_CELLS, 0.51) for i in range(NUM_CELLS): seg = connections.createSegment(i, 1) for cell in active_cells: segments = connections.segmentsForCell(cell) segment = segments[0] for c in presynaptic_input: connections.createSynapse(segment, c, 0.1) connections.adaptSegment(segment, inputSDR, 0.1, 0.0, False) presynamptic_cells = self._getPresynapticCells( connections, segment, 0.2) self.assertEqual(presynamptic_cells, presynaptic_input_set, "Missing synapses") presynaptic_input1 = list(range(0, 5)) presynaptic_input_set1 = set(presynaptic_input1) inputSDR.sparse = presynaptic_input1 for cell in active_cells: segments = connections.segmentsForCell(cell) segment = segments[0] connections.adaptSegment(segment, inputSDR, 0.0, 0.1, False) presynamptic_cells = self._getPresynapticCells( connections, segment, 0.2) self.assertEqual(presynamptic_cells, presynaptic_input_set1, "Too many synapses") presynamptic_cells = self._getPresynapticCells( connections, segment, 0.1) self.assertEqual(presynamptic_cells, presynaptic_input_set, "Missing synapses")
def testHalfAnomaly(self): """ Anomaly score is equal to 0.5, if half of the active columns were overlapping with the columns containing predictive cells """ activeCols = SDR(100) predictiveCols = SDR(100) activeCols.sparse = [60, 69, 80, 91] predictiveCols.sparse = [60, 80, 55, 1] score = an.calculateRawAnomaly(activeCols, predictiveCols) self.assertEqual(score, 0.5)
def testFullAnomaly(self): """ Anomaly score is equal to 1.0, if none of the active columns were overlapping with the columns containing predictive cells """ activeCols = SDR(100) predictiveCols = SDR(100) activeCols.sparse = [60, 69, 80, 91] predictiveCols.sparse = [56, 95, 68, 2] score = an.calculateRawAnomaly(activeCols, predictiveCols) self.assertEqual(score, 1.0)
def testZeroAnomaly(self): """ Anomaly score is equal to 0.0, if all active columns were overlapping with the columns containing predictive cells """ activeCols = SDR(100) predictiveCols = SDR(100) activeCols.sparse = [0, 2, 89, 99] predictiveCols.sparse = [0, 2, 89, 99] score = an.calculateRawAnomaly(activeCols, predictiveCols) self.assertEqual(score, 0.0)
def testPredictionDistribution(self): """ Test the distribution of predictions. Here, we intend the classifier to learn the associations: [1,3,5] => bucketIdx 0 (30%) => bucketIdx 1 (30%) => bucketIdx 2 (40%) [2,4,6] => bucketIdx 1 (50%) => bucketIdx 3 (50%) The classifier should get the distribution almost right given enough repetitions and a small learning rate """ c = Classifier(alpha=0.001) SDR1 = SDR(10) SDR1.sparse = [1, 3, 5] SDR2 = SDR(10) SDR2.sparse = [2, 4, 6] random.seed(42) for _ in range(5000): randomNumber = random.random() if randomNumber < 0.3: bucketIdx = 0 elif randomNumber < 0.6: bucketIdx = 1 else: bucketIdx = 2 c.learn(pattern=SDR1, classification=bucketIdx) randomNumber = random.random() if randomNumber < 0.5: bucketIdx = 1 else: bucketIdx = 3 c.learn(pattern=SDR2, classification=bucketIdx) result1 = c.infer(pattern=SDR1) self.assertAlmostEqual(result1[0], 0.3, places=1) self.assertAlmostEqual(result1[1], 0.3, places=1) self.assertAlmostEqual(result1[2], 0.4, places=1) result2 = c.infer(pattern=SDR2) self.assertAlmostEqual(result2[1], 0.5, places=1) self.assertAlmostEqual(result2[3], 0.5, places=1)
def compute(self, activeColumns, apicalInput=(), apicalGrowthCandidates=None, learn=True): """ Perform one timestep. Activate the specified columns, using the predictions from the previous timestep, then learn. Then form a new set of predictions using the new active cells and the apicalInput. @param activeColumns (numpy array) List of active columns @param apicalInput (numpy array) List of active input bits for the apical dendrite segments @param apicalGrowthCandidates (numpy array or None) List of bits that the active cells may grow new apical synapses to If None, the apicalInput is assumed to be growth candidates. @param learn (bool) Whether to grow / reinforce / punish synapses """ activeColumns = np.asarray(activeColumns) apicalInput = np.asarray(apicalInput) apicalInputSDR = SDR(self.apicalInputSize) basalInputSDR = SDR(self.basalInputSize) basalInputSDR.sparse = self.activeCells if apicalGrowthCandidates is None: apicalGrowthCandidates = apicalInput apicalGrowthCandidates = np.asarray(apicalGrowthCandidates) self.prevPredictedCells = self.predictedCells apicalInputSDR.sparse = self.prevApicalInput self.activateCells(activeColumns, basalInputSDR, apicalInputSDR, self.winnerCells, self.prevApicalGrowthCandidates, learn) apicalInputSDR.sparse = apicalInput basalInputSDR.sparse = self.activeCells self.depolarizeCells(basalInputSDR, apicalInputSDR, learn) self.prevApicalInput = apicalInput.copy() self.prevApicalGrowthCandidates = apicalGrowthCandidates.copy()
def testAdaptShouldRemoveSegments(self): """ Test that connections are generated on predefined segments. """ random = Random(1981) active_cells = np.array(random.sample(np.arange(0, NUM_CELLS, 1, dtype="uint32"), 40), dtype="uint32") active_cells.sort() presynaptic_input = list(range(0, 10)) inputSDR = SDR(1024) inputSDR.sparse = presynaptic_input connections = Connections(NUM_CELLS, 0.51) for i in range(NUM_CELLS): seg = connections.createSegment(i, 2) seg = connections.createSegment(i, 2) #create 2 segments on each cell for cell in active_cells: segments = connections.segmentsForCell(cell) self.assertEqual(len(segments), 2, "Segments were prematurely destroyed.") segment = segments[0] numSynapsesOnSegment = len(segments) connections.adaptSegment(segment, inputSDR, 0.1, 0.001, pruneZeroSynapses=True, segmentThreshold=1) #set to =1 so that segments get always deleted in this test segments = connections.segmentsForCell(cell) self.assertEqual(len(segments), 1, "Segments were not destroyed.")
def deInactivateCells(self, l6Input): """ Activate trnCells according to the l6Input. These in turn will impact bursting mode in relay cells that are connected to these trnCells. Given the feedForwardInput, compute which cells will be silent, tonic, or bursting. :param l6Input: An SDR from L6. List of indices corresponding to L6 cells. :return: nothing """ # Figure out which TRN cells recognize the L6 pattern. self.trnOverlaps = self.trnConnections.computeActivity(l6Input, False) self.activeTRNSegments = np.flatnonzero( self.trnOverlaps >= self.trnActivationThreshold) self.activeTRNCellIndices = self.trnConnections.mapSegmentsToCells( self.activeTRNSegments) # for s, idx in zip(self.activeTRNSegments, self.activeTRNCellIndices): # print(self.trnOverlaps[s], idx, self.trnIndextoCoord(idx)) # Figure out which relay cells have dendrites in de-inactivated state activeTRNCells = SDR(self.trnCellCount) activeTRNCells.sparse = self.activeTRNCellIndices self.relayOverlaps = self.relayConnections.computeActivity( activeTRNCells, False) self.activeRelaySegments = np.flatnonzero( self.relayOverlaps >= self.relayThreshold) self.burstReadyCellIndices = self.relayConnections.mapSegmentsToCells( self.activeRelaySegments) self.burstReadyCells.reshape(-1)[self.burstReadyCellIndices] = 1
def testThalamusBursting(self, verbose=False): """ Test that thalamus relays around the trained locations, and also does busrts. """ t = Thalamus(trnThreshold=6) self._trainThalamus(t) ff = np.zeros((32, 32)) ff.reshape(-1)[[8, 9, 98, 99]] = 1.0 L6Pattern = SDR(t.l6CellCount) L6Pattern.sparse = [0, 1, 2, 3, 4, 5] result = self._inferThalamus(t, L6Pattern, ff) non_bursting = result[result >= 0.4].nonzero()[0].tolist() bursting = result[result >= 1.4].nonzero()[0].tolist() self.assertEqual([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 ], non_bursting, "Non-bursting not correct") self.assertEqual([0, 1, 2, 3, 4, 5, 6, 7, 8], bursting, "Bursting not correct") if verbose: print(non_bursting) print(bursting)
def testAdaptShouldRemoveSegments(self): """ Test that connections are generated on predefined segments. """ random = Random(1981) active_cells = np.array(random.sample( np.arange(0, NUM_CELLS, 1, dtype="uint32"), 40), dtype="uint32") active_cells.sort() presynaptic_input = list(range(0, 10)) inputSDR = SDR(1024) inputSDR.sparse = presynaptic_input connections = Connections(NUM_CELLS, 0.51) for i in range(NUM_CELLS): seg = connections.createSegment(i, 1) for cell in active_cells: segments = connections.segmentsForCell(cell) self.assertEqual(len(segments), 1, "Segments were prematurely destroyed.") segment = segments[0] connections.adaptSegment(segment, inputSDR, 0.1, 0.001, True) segments = connections.segmentsForCell(cell) self.assertEqual(len(segments), 0, "Segments were not destroyed.")
def CellsToColumns(self, cells, cellsPerColumn, columnsCount): array = [] for cell in cells.sparse: col = int(cell / cellsPerColumn) if col not in array: #each column max once array += [col] columns = SDR(columnsCount) columns.sparse = array return columns
def testComputeActivityUnion(self): """ Test that connections are generated on predefined segments. """ random = Random(1981) active_cells = np.array(random.sample( np.arange(0, NUM_CELLS, 1, dtype="uint32"), 40), dtype="uint32") active_cells.sort() presynaptic_input1 = list(range(0, 10)) presynaptic_input1_set = set(presynaptic_input1) presynaptic_input2 = list(range(10, 20)) presynaptic_input2_set = set(presynaptic_input1) connections = Connections(NUM_CELLS, 0.51, False) for i in range(NUM_CELLS): seg = connections.createSegment(i, 1) self._learn(connections, active_cells, presynaptic_input1) self._learn(connections, active_cells, presynaptic_input2) numSynapses = connections.numSynapses() self.assertNotEqual( numSynapses, 40, "There should be a synapse for each presynaptic cell") active_cells_set = set(active_cells) inputSDR = SDR(1024) inputSDR.sparse = presynaptic_input1 numActiveConnectedSynapsesForSegment = connections.computeActivity( inputSDR, False) for cell, count in enumerate(numActiveConnectedSynapsesForSegment): if cell in active_cells_set: self.assertNotEqual(count, 0, "Segment should be active") inputSDR.sparse = presynaptic_input2 numActiveConnectedSynapsesForSegment = connections.computeActivity( inputSDR, False) for cell, count in enumerate(numActiveConnectedSynapsesForSegment): if cell in active_cells_set: self.assertNotEqual(count, 0, "Segment should be active")
def testSparse(self): A = SDR((103, )) B = SDR((100, 100, 1)) A.sparse B.sparse = [1, 2, 3, 4] assert (all(B.sparse == np.array([1, 2, 3, 4]))) B.sparse = [] assert (not B.dense.any()) # Test wrong dimensions assigned C = SDR(1000) C.randomize(.98) try: A.sparse = C.sparse except RuntimeError: pass else: self.fail()
def testMultiStepPredictions(self): """ Test multi-step predictions We train the 0-step and the 1-step classifiers simultaneously on data stream (SDR1, bucketIdx0) (SDR2, bucketIdx1) (SDR1, bucketIdx0) (SDR2, bucketIdx1) ... We intend the 0-step classifier to learn the associations: SDR1 => bucketIdx 0 SDR2 => bucketIdx 1 and the 1-step classifier to learn the associations SDR1 => bucketIdx 1 SDR2 => bucketIdx 0 """ c = Predictor([0, 1], 1.0) SDR1 = SDR(10) SDR1.sparse = [1, 3, 5] SDR2 = SDR(10) SDR2.sparse = [2, 4, 6] recordNum = 0 for _ in range(100): c.learn(recordNum, pattern=SDR1, classification=0) recordNum += 1 c.learn(recordNum, pattern=SDR2, classification=1) recordNum += 1 result1 = c.infer(recordNum, SDR1) result2 = c.infer(recordNum, SDR2) self.assertAlmostEqual(result1[0][0], 1.0, places=1) self.assertAlmostEqual(result1[0][1], 0.0, places=1) self.assertAlmostEqual(result2[0][0], 0.0, places=1) self.assertAlmostEqual(result2[0][1], 1.0, places=1)
def testDeterminism(self): GOLD = SDR(1000) GOLD.sparse = [ 2, 34, 37, 38, 69, 79, 114, 170, 200, 234, 254, 258, 279, 289, 291, 292, 295, 307, 321, 336, 345, 350, 361, 373, 378, 400, 450, 461, 462, 487, 520, 532, 539, 548, 576, 583, 616, 623, 626, 627, 663, 681, 695, 716, 794, 799, 830, 835, 837, 841] params = SimHashDocumentEncoderParameters() params.size = GOLD.size params.sparsity = 0.05 encoder = SimHashDocumentEncoder(params) current = encoder.encode("I came to the fork in the road") assert(current == GOLD)
def _learn(self, connections, active_cells, presynaptic_input): inputSDR = SDR(1024) inputSDR.sparse = presynaptic_input for cell in active_cells: segments = connections.segmentsForCell(cell) segment = segments[0] for c in presynaptic_input: connections.createSynapse(segment, c, 0.1) for cell in active_cells: segments = connections.segmentsForCell(cell) segment = segments[0] connections.adaptSegment(segment, inputSDR, 0.5, 0.0, False)
def testComputeActivity(self): """ Test that connections are generated on predefined segments. """ random = Random(1981) active_cells = np.array(random.sample( np.arange(0, NUM_CELLS, 1, dtype="uint32"), 40), dtype="uint32") active_cells.sort() presynaptic_input = list(range(0, 10)) presynaptic_input_set = set(presynaptic_input) inputSDR = SDR(1024) inputSDR.sparse = presynaptic_input l = len(presynaptic_input) connections = Connections(NUM_CELLS, 0.51, False) for i in range(NUM_CELLS): seg = connections.createSegment(i, 1) numActiveConnectedSynapsesForSegment = connections.computeActivity( inputSDR, False) for count in numActiveConnectedSynapsesForSegment: self.assertEqual(count, 0, "Segment should not be active") for cell in active_cells: segments = connections.segmentsForCell(cell) segment = segments[0] for c in presynaptic_input: connections.createSynapse(segment, c, 0.1) numActiveConnectedSynapsesForSegment = connections.computeActivity( inputSDR, False) for count in numActiveConnectedSynapsesForSegment: self.assertEqual(count, 0, "Segment should not be active") for cell in active_cells: segments = connections.segmentsForCell(cell) segment = segments[0] connections.adaptSegment(segment, inputSDR, 0.5, 0.0, False) active_cells_set = set(active_cells) numActiveConnectedSynapsesForSegment = connections.computeActivity( inputSDR, False) for cell, count in enumerate(numActiveConnectedSynapsesForSegment): if cell in active_cells_set: self.assertEqual(count, l, "Segment should be active") else: self.assertEqual(count, 0, "Segment should not be active")
def testSerialization3(self): # This test verifies that saveToFile() and loadFromFile() on Classifier are accessable from Python. SDR1 = SDR(15); SDR1.sparse = [1, 5, 9] SDR2 = SDR(15); SDR2.sparse = [0, 6, 9, 11] SDR3 = SDR(15); SDR3.sparse = [6, 9] SDR4 = SDR(15); SDR4.sparse = [1, 5, 9] c1 = Classifier() c1.learn(pattern=SDR1, classification=4) c1.learn(pattern=SDR2, classification=5) c1.learn(pattern=SDR3, classification=5) c1.learn(pattern=SDR4, classification=4) # The Predictor now has some data in it, try serialization. file = "Classifier_test_save.XML" c1.saveToFile(file, "XML") c2 = Classifier() c2.loadFromFile(file, "XML") os.remove(file) result1 = c1.infer(SDR1) result2 = c2.infer(SDR1) self.assertEqual(len(result1), len(result2)) for i in range(len(result1)): self.assertAlmostEqual(result1[i], result2[i], places=5)
def forward(self, encoding): activeColumns = SDR(self.sp.getColumnDimensions()) self.sp.compute(encoding, self.learn, activeColumns) predictedColumns = None if self.temporal: self.tm.compute(activeColumns, self.learn) self.tm.activateDendrites(self.learn) predictedColumnIndices = { self.tm.columnForCell(i) for i in self.tm.getPredictiveCells().sparse } predictedColumns = SDR(self.sp.getColumnDimensions()) predictedColumns.sparse = list(predictedColumnIndices) return activeColumns, predictedColumns