Beispiel #1
0
  def testBumpUpWeakColumns(self):
    sp = SpatialPooler(inputDimensions=[8],
                      columnDimensions=[5])

    sp._synPermBelowStimulusInc = 0.01
    sp._synPermTrimThreshold = 0.05
    sp._overlapDutyCycles = numpy.array([0, 0.009, 0.1, 0.001, 0.002])
    sp._minOverlapDutyCycles = numpy.array(5*[0.01])

    sp._potentialPools = SparseBinaryMatrix(
       [[1, 1, 1, 1, 0, 0, 0, 0],
        [1, 0, 0, 0, 1, 1, 0, 1],
        [0, 0, 1, 0, 1, 1, 1, 0],
        [1, 1, 1, 0, 0, 0, 1, 0],
        [1, 1, 1, 1, 1, 1, 1, 1]])

    sp._permanences = SparseMatrix(
      [[0.200, 0.120, 0.090, 0.040, 0.000, 0.000, 0.000, 0.000],
       [0.150, 0.000, 0.000, 0.000, 0.180, 0.120, 0.000, 0.450],
       [0.000, 0.000, 0.014, 0.000, 0.032, 0.044, 0.110, 0.000],
       [0.041, 0.000, 0.000, 0.000, 0.000, 0.000, 0.178, 0.000],
       [0.100, 0.738, 0.045, 0.002, 0.050, 0.008, 0.208, 0.034]])

    truePermanences = [
      [0.210, 0.130, 0.100, 0.000, 0.000, 0.000, 0.000, 0.000],
  #    Inc    Inc    Inc    Trim    -     -     -    -
      [0.160, 0.000, 0.000, 0.000, 0.190, 0.130, 0.000, 0.460],
  #    Inc   -     -    -     Inc   Inc    -     Inc
      [0.000, 0.000, 0.014, 0.000, 0.032, 0.044, 0.110, 0.000], #unchanged
  #    -    -     -    -     -    -     -    -
      [0.051, 0.000, 0.000, 0.000, 0.000, 0.000, 0.188, 0.000],
  #    Inc   Trim    Trim    -     -    -    Inc     -
      [0.110, 0.748, 0.055, 0.000, 0.060, 0.000, 0.218, 0.000]]

    sp._bumpUpWeakColumns()
    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])
Beispiel #2
0
  def testAdaptSynapses(self):
    sp = SpatialPooler(inputDimensions=[8],
                       columnDimensions=[4],
                       synPermInactiveDec=0.01,
                       synPermActiveInc=0.1,
                       synPermActiveSharedDec=0.02,
                       synPermOrphanDec=0.03)
    sp._synPermTrimThreshold = 0.05

    sp._potentialPools = SparseBinaryMatrix(
        [[1, 1, 1, 1, 0, 0, 0, 0],
         [1, 0, 0, 0, 1, 1, 0, 1],
         [0, 0, 1, 0, 0, 0, 1, 0],
         [1, 0, 0, 0, 0, 0, 1, 0]])

    inputVector = numpy.array([1, 0, 0, 1, 1, 0, 1, 0])
    sharedInputs = numpy.where(numpy.array(
        [1, 0, 0, 0, 0, 0, 1, 0]) > 0)[0]
    activeColumns = numpy.array([0,1,2])

    sp._permanences = SparseMatrix(
        [[0.200, 0.120, 0.090, 0.040, 0.000, 0.000, 0.000, 0.000],
         [0.150, 0.000, 0.000, 0.000, 0.180, 0.120, 0.000, 0.450],
         [0.000, 0.000, 0.014, 0.000, 0.000, 0.000, 0.110, 0.000],
         [0.040, 0.000, 0.000, 0.000, 0.000, 0.000, 0.178, 0.000]])

    truePermanences = [
        [0.280, 0.110, 0.080, 0.140, 0.000, 0.000, 0.000, 0.000],
      #  Inc/Sh   Dec     Dec    Inc   -    -    -  -
        [0.230, 0.000, 0.000, 0.000, 0.280, 0.110, 0.000, 0.440],
      #  Inc/Sh    -      -     -      Inc    Dec    -     Dec  
        [0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.190, 0.000],
      #   -      -     Trim     -     -     -    Inc/Sh   - 
        [0.040, 0.000, 0.000, 0.000, 0.000, 0.000, 0.178, 0.000]]
      #   -      -      -    -      -    -    -    -   -   

    sp._adaptSynapses(inputVector,sharedInputs, activeColumns)
    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])

    # test orphan columns
    sp._potentialPools = SparseBinaryMatrix(
        [[1, 1, 1, 0, 0, 0, 0, 0],
         [0, 1, 1, 1, 0, 0, 0, 0],
         [0, 0, 1, 1, 1, 0, 0, 0],
         [1, 0, 0, 0, 0, 0, 1, 0]])

    inputVector = numpy.array([1, 0, 0, 1, 1, 0, 1, 0])
    sharedInputs = numpy.where(numpy.array([1, 0, 0, 1, 0, 0, 0, 0]) > 0)[0]
    activeColumns = numpy.array([0,1,2])

    sp._permanences = SparseMatrix(
        [[0.200, 0.120, 0.090, 0.000, 0.000, 0.000, 0.000, 0.000],
         [0.000, 0.017, 0.232, 0.400, 0.000, 0.000, 0.000, 0.000],
         [0.000, 0.000, 0.014, 0.051, 0.730, 0.000, 0.000, 0.000],
         [0.170, 0.000, 0.000, 0.000, 0.000, 0.000, 0.380, 0.000]])

    truePermanences = [
        [0.280, 0.110, 0.080, 0.000, 0.000, 0.000, 0.000, 0.000],
        #  Inc/Sh    Dec     Dec     -       -    -    -    -
        [0.000, 0.000, 0.222, 0.480, 0.000, 0.000, 0.000, 0.000],
        #     -      Trim     Dec  Inc/Sh   -       -      -      -
        [0.000, 0.000, 0.000, 0.131, 0.830, 0.000, 0.000, 0.000],
        #   -      -      Trim Inc/Sh  Inc     -     -     -
        [0.170, 0.000, 0.000, 0.000, 0.000, 0.000, 0.380, 0.000]]
        #  -    -      -      -      -       -       -     -

    sp._adaptSynapses(inputVector,sharedInputs, activeColumns)
    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])