Exemple #1
0
 def testColumnForCell2D(self):
   tm = TemporalMemory(
     columnDimensions=[64, 64],
     cellsPerColumn=4
   )
   self.assertEqual(tm.columnForCell(0), 0)
   self.assertEqual(tm.columnForCell(3), 0)
   self.assertEqual(tm.columnForCell(4), 1)
   self.assertEqual(tm.columnForCell(16383), 4095)
Exemple #2
0
 def testColumnForCell1D(self):
   tm = TemporalMemory(
     columnDimensions=[2048],
     cellsPerColumn=5
   )
   self.assertEqual(tm.columnForCell(0), 0)
   self.assertEqual(tm.columnForCell(4), 0)
   self.assertEqual(tm.columnForCell(5), 1)
   self.assertEqual(tm.columnForCell(10239), 2047)
    def testColumnForCellInvalidCell(self):
        tm = TemporalMemory(columnDimensions=[64, 64], cellsPerColumn=4)

        try:
            tm.columnForCell(16383)
        except IndexError:
            self.fail("IndexError raised unexpectedly")

        args = [16384]
        self.assertRaises(IndexError, tm.columnForCell, *args)

        args = [-1]
        self.assertRaises(IndexError, tm.columnForCell, *args)
  def testColumnForCellInvalidCell(self):
    tm = TemporalMemory(
      columnDimensions=[64, 64],
      cellsPerColumn=4
    )

    try:
      tm.columnForCell(16383)
    except IndexError:
      self.fail("IndexError raised unexpectedly")

    args = [16384]
    self.assertRaises(IndexError, tm.columnForCell, *args)

    args = [-1]
    self.assertRaises(IndexError, tm.columnForCell, *args)
  def testAddSegmentToCellWithFewestSegments(self):
    grewOnCell1 = False
    grewOnCell2 = False
    for seed in xrange(100):
      tm = TemporalMemory(
        columnDimensions=[32],
        cellsPerColumn=4,
        activationThreshold=3,
        initialPermanence=.2,
        connectedPermanence=.50,
        minThreshold=2,
        maxNewSynapseCount=4,
        permanenceIncrement=.10,
        permanenceDecrement=.10,
        predictedSegmentDecrement=0.02,
        seed=seed)

      prevActiveColumns = [1, 2, 3, 4]
      activeColumns = [0]
      prevActiveCells = [4, 5, 6, 7]
      nonMatchingCells = [0, 3]
      activeCells = [0, 1, 2, 3]

      segment1 = tm.connections.createSegment(nonMatchingCells[0])
      tm.connections.createSynapse(segment1, prevActiveCells[0], .5)
      segment2 = tm.connections.createSegment(nonMatchingCells[1])
      tm.connections.createSynapse(segment2, prevActiveCells[1], .5)

      tm.compute(prevActiveColumns, True)
      tm.compute(activeColumns, True)

      self.assertEqual(activeCells, tm.getActiveCells())

      self.assertEqual(3, tm.connections.numSegments())
      self.assertEqual(1, tm.connections.numSegments(0))
      self.assertEqual(1, tm.connections.numSegments(3))
      self.assertEqual(1, tm.connections.numSynapses(segment1))
      self.assertEqual(1, tm.connections.numSynapses(segment2))

      segments = list(tm.connections.segmentsForCell(1))
      if len(segments) == 0:
        segments2 = list(tm.connections.segmentsForCell(2))
        self.assertFalse(len(segments2) == 0)
        grewOnCell2 = True
        segments.append(segments2[0])
      else:
        grewOnCell1 = True

      self.assertEqual(1, len(segments))
      synapses = list(tm.connections.synapsesForSegment(segments[0]))
      self.assertEqual(4, len(synapses))

      columnChecklist = set(prevActiveColumns)

      for synapse in synapses:
        synapseData = tm.connections.dataForSynapse(synapse)
        self.assertAlmostEqual(.2, synapseData.permanence)

        column = tm.columnForCell(synapseData.presynapticCell)
        self.assertTrue(column in columnChecklist)
        columnChecklist.remove(column)
      self.assertTrue(len(columnChecklist) == 0)

    self.assertTrue(grewOnCell1)
    self.assertTrue(grewOnCell2)
Exemple #6
0
    tm.compute(activeColumns, learn=False)

    # The following print statements prints out the active cells, predictive
    # cells, active segments and winner cells.
    #
    # What you should notice is that the columns where active state is 1
    # represent the SDR for the current input pattern and the columns where
    # predicted state is 1 represent the SDR for the next expected pattern
    print "\nAll the active and predicted cells:"

    print("active cells " + str(tm.getActiveCells()))
    print("predictive cells " + str(tm.getPredictiveCells()))
    print("winner cells " + str(tm.getWinnerCells()))
    print("# of active segments " + str(tm.connections.numSegments()))

    activeColumnsIndeces = [tm.columnForCell(i) for i in tm.getActiveCells()]
    predictedColumnIndeces = [
        tm.columnForCell(i) for i in tm.getPredictiveCells()
    ]

    # Reconstructing the active and inactive columns with 1 as active and 0 as
    # inactive representation.

    actColState = [
        '1' if i in activeColumnsIndeces else '0'
        for i in range(tm.numberOfColumns())
    ]
    actColStr = ("".join(actColState))
    predColState = [
        '1' if i in predictedColumnIndeces else '0'
        for i in range(tm.numberOfColumns())
Exemple #7
0
    def testAddSegmentToCellWithFewestSegments(self):
        grewOnCell1 = False
        grewOnCell2 = False
        for seed in xrange(100):
            tm = TemporalMemory(columnDimensions=[32],
                                cellsPerColumn=4,
                                activationThreshold=3,
                                initialPermanence=.2,
                                connectedPermanence=.50,
                                minThreshold=2,
                                maxNewSynapseCount=4,
                                permanenceIncrement=.10,
                                permanenceDecrement=.10,
                                predictedSegmentDecrement=0.02,
                                seed=seed)

            prevActiveColumns = [1, 2, 3, 4]
            activeColumns = [0]
            prevActiveCells = [4, 5, 6, 7]
            nonMatchingCells = [0, 3]
            activeCells = [0, 1, 2, 3]

            segment1 = tm.connections.createSegment(nonMatchingCells[0])
            tm.connections.createSynapse(segment1, prevActiveCells[0], .5)
            segment2 = tm.connections.createSegment(nonMatchingCells[1])
            tm.connections.createSynapse(segment2, prevActiveCells[1], .5)

            tm.compute(prevActiveColumns, True)
            tm.compute(activeColumns, True)

            self.assertEqual(activeCells, tm.getActiveCells())

            self.assertEqual(3, tm.connections.numSegments())
            self.assertEqual(1, len(tm.connections.segmentsForCell(0)))
            self.assertEqual(1, len(tm.connections.segmentsForCell(3)))
            self.assertEqual(1,
                             len(tm.connections.synapsesForSegment(segment1)))
            self.assertEqual(1,
                             len(tm.connections.synapsesForSegment(segment2)))

            segments = tm.connections.segmentsForCell(1)
            if len(segments) == 0:
                segments2 = tm.connections.segmentsForCell(2)
                self.assertFalse(len(segments2) == 0)
                grewOnCell2 = True
                segments.append(segments2[0])
            else:
                grewOnCell1 = True

            self.assertEqual(1, len(segments))
            synapses = tm.connections.synapsesForSegment(segments[0])
            self.assertEqual(4, len(synapses))

            columnChecklist = set(prevActiveColumns)

            for synapse in synapses:
                synapseData = tm.connections.dataForSynapse(synapse)
                self.assertAlmostEqual(.2, synapseData.permanence)

                column = tm.columnForCell(synapseData.presynapticCell)
                self.assertTrue(column in columnChecklist)
                columnChecklist.remove(column)
            self.assertTrue(len(columnChecklist) == 0)

        self.assertTrue(grewOnCell1)
        self.assertTrue(grewOnCell2)
Exemple #8
0
  tm.compute(activeColumns, learn = False)
  
  # The following print statements prints out the active cells, predictive
  # cells, active segments and winner cells. 
  #
  # What you should notice is that the columns where active state is 1
  # represent the SDR for the current input pattern and the columns where
  # predicted state is 1 represent the SDR for the next expected pattern
  print "\nAll the active and predicted cells:"
  
  print("active cells " + str(tm.getActiveCells()))
  print("predictive cells " + str(tm.getPredictiveCells()))
  print("winner cells " + str(tm.getWinnerCells()))
  print("# of active segments " + str(tm.connections.numSegments()))

  activeColumnsIndeces = [tm.columnForCell(i) for i in tm.getActiveCells()]
  predictedColumnIndeces = [tm.columnForCell(i) for i in tm.getPredictiveCells()]
  
  
  # Reconstructing the active and inactive columns with 1 as active and 0 as 
  # inactive representation.

  actColState = ['1' if i in activeColumnsIndeces else '0' for i in range(tm.numberOfColumns())]
  actColStr = ("".join(actColState))
  predColState = ['1' if i in predictedColumnIndeces else '0' for i in range(tm.numberOfColumns())]
  predColStr = ("".join(predColState))

  # For convenience the cells are grouped
  # 10 at a time. When there are multiple cells per column the printout
  # is arranged so the cells in a column are stacked together
  print "Active columns:    " + formatRow(actColStr)
Exemple #9
0
    # The following print statements prints out the active cells, predictive
    # cells, active segments and winner cells.
    #
    # What you should notice is that the columns where active state is 1
    # represent the SDR for the current input pattern and the columns where
    # predicted state is 1 represent the SDR for the next expected pattern
    print "\nAll the active and predicted cells:"

    print("active cells " + str(tm.activeCells))
    print("predictive cells " + str(tm.predictiveCells))
    print("active segments " + str(tm.activeSegments))

    print("winnercells" + str(tm.winnerCells))

    activeColumnsIndeces = [tm.columnForCell(i) for i in tm.activeCells]
    predictedColumnIndeces = [tm.columnForCell(i) for i in tm.predictiveCells]

    # Reconstructing the active and inactive columns with 1 as active and 0 as
    # inactive representation.

    actColState = [
        '1' if i in activeColumnsIndeces else '0'
        for i in range(tm.numberOfColumns())
    ]
    actColStr = ("".join(actColState))
    predColState = [
        '1' if i in predictedColumnIndeces else '0'
        for i in range(tm.numberOfColumns())
    ]
    predColStr = ("".join(predColState))
Exemple #10
0
  
  # The following print statements prints out the active cells, predictive
  # cells, active segments and winner cells. 
  #
  # What you should notice is that the columns where active state is 1
  # represent the SDR for the current input pattern and the columns where
  # predicted state is 1 represent the SDR for the next expected pattern
  print "\nAll the active and predicted cells:"
  
  print("active cells " + str(tm.activeCells))
  print("predictive cells "+ str(tm.predictiveCells))
  print("active segments "+ str(tm.activeSegments))

  print("winnercells" + str(tm.winnerCells))

  activeColumnsIndeces = [tm.columnForCell(i) for i in tm.activeCells]
  predictedColumnIndeces = [tm.columnForCell(i) for i in tm.predictiveCells]
  
  
  # Reconstructing the active and inactive columns with 1 as active and 0 as 
  # inactive representation.

  actColState = ['1' if i in activeColumnsIndeces else '0' for i in range(tm.numberOfColumns())]
  actColStr = ("".join(actColState))
  predColState = ['1' if i in predictedColumnIndeces else '0' for i in range(tm.numberOfColumns())]
  predColStr = ("".join(predColState))

  # For convenience the cells are grouped
  # 10 at a time. When there are multiple cells per column the printout
  # is arranged so the cells in a column are stacked together
  print "Active columns:    " + formatRow(actColStr)