def testRaisePermanenceThreshold(self): sp = SpatialPooler(inputDimensions=[5], columnDimensions=[5], synPermConnected=0.1, stimulusThreshold=3) sp._synPermBelowStimulusInc = 0.01 sp._permanences = SparseMatrix( [[0.0, 0.11, 0.095, 0.092, 0.01], [0.12, 0.15, 0.02, 0.12, 0.09], [0.51, 0.081, 0.025, 0.089, 0.31], [0.18, 0.0601, 0.11, 0.011, 0.03], [0.011, 0.011, 0.011, 0.011, 0.011]]) sp._connectedSynapses = SparseBinaryMatrix( [[0, 1, 0, 0, 0], [1, 1, 0, 1, 0], [1, 0, 0, 0, 1], [1, 0, 1, 0, 0], [0, 0, 0, 0, 0]]) sp._connectedCounts = numpy.array([1, 3, 2, 2, 0]) truePermanences = [ [0.0, 0.12, 0.105, 0.102, 0.0], # incremented once [0.12, 0.15, 0.02, 0.12, 0.09], # no change [0.53, 0.101, 0.0, 0.109, 0.33], # increment twice [0.22, 0.1001, 0.15, 0.051, 0.07], # increment four times [0.101, 0.101, 0.101, 0.101, 0.101]] #increment 9 times trueConnectedSynapses = [ [0, 1, 1, 1, 0], [1, 1, 0, 1, 0], [1, 1, 0, 1, 1], [1, 1, 1, 0, 0], [1, 1, 1, 1, 1]] trueConnectedCounts = [3, 3, 4, 3, 5] sp._raisePermanenceToThreshold() for i in xrange(sp._numColumns): perm = list(sp._permanences.getRow(i)) for j in xrange(sp._numInputs): self.assertAlmostEqual(truePermanences[i][j],perm[j]) self.assertListEqual( trueConnectedSynapses[i], list(sp._connectedSynapses.getRow(i)) ) self.assertEqual(trueConnectedCounts[i], sp._connectedCounts[i])
def testCalculateOverlap(self): """ Test that column computes overlap and percent overlap correctly. """ sp = SpatialPooler(inputDimensions = [10], columnDimensions = [5]) sp._connectedSynapses = SparseBinaryMatrix( [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1]]) sp._connectedCounts = numpy.array([10.0, 8.0, 6.0, 4.0, 2.0]) inputVector = numpy.zeros(sp._numInputs, dtype='float32') overlaps, overlapsPct = sp._calculateOverlap(inputVector) trueOverlaps = list(numpy.array([0, 0, 0, 0, 0])) trueOverlapsPct = list(numpy.array([0, 0, 0, 0, 0])) self.assertListEqual(list(overlaps), trueOverlaps) self.assertListEqual(list(overlapsPct), trueOverlapsPct) sp._connectedSynapses = SparseBinaryMatrix( [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1]]) sp._connectedCounts = numpy.array([10.0, 8.0, 6.0, 4.0, 2.0]) inputVector = numpy.ones(sp._numInputs, dtype='float32') overlaps, overlapsPct = sp._calculateOverlap(inputVector) trueOverlaps = list(numpy.array([10, 8, 6, 4, 2])) trueOverlapsPct = list(numpy.array([1, 1, 1, 1, 1])) self.assertListEqual(list(overlaps), trueOverlaps) self.assertListEqual(list(overlapsPct), trueOverlapsPct) sp._connectedSynapses = SparseBinaryMatrix( [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1]]) sp._connectedCounts = numpy.array([10.0, 8.0, 6.0, 4.0, 2.0]) inputVector = numpy.zeros(sp._numInputs, dtype='float32') inputVector[9] = 1 overlaps, overlapsPct = sp._calculateOverlap(inputVector) trueOverlaps = list(numpy.array([1, 1, 1, 1, 1])) trueOverlapsPct = list(numpy.array([0.1, 0.125, 1.0/6, 0.25, 0.5])) self.assertListEqual(list(overlaps), trueOverlaps) self.assertListEqual(list(overlapsPct), trueOverlapsPct) # Zig-zag sp._connectedSynapses = SparseBinaryMatrix( [[1, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 1]]) sp._connectedCounts = numpy.array([2.0, 2.0, 2.0, 2.0, 2.0]) inputVector = numpy.zeros(sp._numInputs, dtype='float32') inputVector[range(0,10,2)] = 1 overlaps, overlapsPct = sp._calculateOverlap(inputVector) trueOverlaps = list(numpy.array([1, 1, 1, 1, 1])) trueOverlapsPct = list(numpy.array([0.5, 0.5, 0.5, 0.5, 0.5])) self.assertListEqual(list(overlaps), trueOverlaps) self.assertListEqual(list(overlapsPct), trueOverlapsPct)
def testCalculateSharedInputs(self): sp = SpatialPooler(inputDimensions=[8], columnDimensions=[5]) sp._connectedSynapses = SparseBinaryMatrix( [[0, 1, 0, 1, 0, 1, 0, 1], [0, 0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 1, 0], [1, 0, 0, 0, 1, 0, 0, 0]]) inputVector = numpy.array( [1, 1, 1, 1, 0, 0, 0, 0]) activeColumns = range(5) sharedTrue = set([3]) shared = set(sp._calculateSharedInputs(inputVector, activeColumns)) self.assertSetEqual(shared, sharedTrue) sp._connectedSynapses = SparseBinaryMatrix( [[0, 1, 0, 1, 0, 1, 0, 1], [0, 0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 1, 0], [1, 0, 0, 0, 1, 0, 0, 0]]) inputVector = numpy.array( [1, 1, 1, 0, 1, 1, 1, 1]) activeColumns = range(5) sharedTrue = set([6,7]) shared = set(sp._calculateSharedInputs(inputVector, activeColumns)) self.assertSetEqual(shared, sharedTrue) sp._connectedSynapses = SparseBinaryMatrix( [[0, 1, 0, 1, 0, 1, 0, 1], [0, 0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 1, 0], [1, 0, 0, 0, 1, 0, 0, 0]]) inputVector = numpy.array( [0, 0, 0, 0, 0, 0, 0, 0]) activeColumns = range(5) sharedTrue = set([]) shared = set(sp._calculateSharedInputs(inputVector, activeColumns)) self.assertSetEqual(shared, sharedTrue) sp._connectedSynapses = SparseBinaryMatrix( [[0, 1, 0, 1, 0, 1, 0, 1], [0, 0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 1, 0], [1, 0, 0, 0, 1, 0, 0, 0]]) inputVector = numpy.array( [1, 0, 1, 0, 1, 1, 1, 0]) activeColumns = [1,2,3] sharedTrue = set([6]) shared = set(sp._calculateSharedInputs(inputVector, activeColumns)) self.assertSetEqual(shared, sharedTrue) sp._connectedSynapses = SparseBinaryMatrix( [[0, 1, 0, 1, 0, 1, 0, 1], [0, 0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 1, 0], [1, 0, 0, 0, 1, 0, 0, 0]]) inputVector = numpy.array([1, 0, 1, 1, 1, 1, 1, 1]) activeColumns = [0,1,3,4] sharedTrue = set([3,7]) shared = set(sp._calculateSharedInputs(inputVector, activeColumns)) self.assertSetEqual(shared, sharedTrue)