コード例 #1
0
    def testL4L2ColumnCreate(self):
        """
    In this simplistic test we just create a network, ensure it has the
    right number of regions and try to run some inputs through it without
    crashing.
    """

        # Create a simple network to test the sensor
        net = createNetwork(networkConfig1)

        self.assertEqual(len(net.regions.keys()), 4,
                         "Incorrect number of regions")

        # Add some input vectors to the queue
        externalInput = net.regions["externalInput_0"].getSelf()
        sensorInput = net.regions["sensorInput_0"].getSelf()

        # Add 3 input vectors
        externalInput.addDataToQueue([2, 42, 1023], 0, 9)
        sensorInput.addDataToQueue([2, 42, 1023], 0, 0)

        externalInput.addDataToQueue([1, 42, 1022], 0, 0)
        sensorInput.addDataToQueue([1, 42, 1022], 0, 0)

        externalInput.addDataToQueue([3, 42, 1021], 0, 0)
        sensorInput.addDataToQueue([3, 42, 1021], 0, 0)

        # Run the network and check outputs are as expected
        net.run(3)
コード例 #2
0
  def testL4L2ColumnCreate(self):
    """
    In this simplistic test we just create a network, ensure it has the
    right number of regions and try to run some inputs through it without
    crashing.
    """

    # Create a simple network to test the sensor
    net = createNetwork(networkConfig1)

    self.assertEqual(len(net.regions.keys()),4,
                     "Incorrect number of regions")

    # Add some input vectors to the queue
    externalInput = net.regions["externalInput"].getSelf()
    sensorInput = net.regions["sensorInput"].getSelf()

    # Add 3 input vectors
    externalInput.addDataToQueue([2, 42, 1023], 0, 9)
    sensorInput.addDataToQueue([2, 42, 1023], 0, 0)

    externalInput.addDataToQueue([1, 42, 1022], 0, 0)
    sensorInput.addDataToQueue([1, 42, 1022], 0, 0)

    externalInput.addDataToQueue([3, 42, 1021], 0, 0)
    sensorInput.addDataToQueue([3, 42, 1021], 0, 0)

    # Run the network and check outputs are as expected
    net.run(3)
コード例 #3
0
  def testIncorrectWidths(self):
    """
    We create a network with sensor and coarse sensor widths that don't match
    column counts. We expect assertion errors in this case
    """
    config = copy.deepcopy(networkConfig)
    config["numCorticalColumns"] = 2
    config["L4Params"]["columnCount"] = 42
    with self.assertRaises(AssertionError):
      createNetwork(config)

    config = copy.deepcopy(networkConfig)
    config["numCorticalColumns"] = 2
    config["L6Params"]["columnCount"] = 42
    with self.assertRaises(AssertionError):
      createNetwork(config)
コード例 #4
0
  def testIncorrectWidths(self):
    """
    We create a network with sensor and coarse sensor widths that don't match
    column counts. We expect assertion errors in this case
    """
    config = copy.deepcopy(networkConfig)
    config["numCorticalColumns"] = 2
    config["L4Params"]["columnCount"] = 42
    with self.assertRaises(AssertionError):
      createNetwork(config)

    config = copy.deepcopy(networkConfig)
    config["numCorticalColumns"] = 2
    config["L6Params"]["columnCount"] = 42
    with self.assertRaises(AssertionError):
      createNetwork(config)
コード例 #5
0
  def testMultipleL4L2ColumnLinks(self):
    """
    In this simplistic test we create a network with 3 L4L2 columns, and
    ensure that it has the correct links between regions.
    """

    # Create a simple network to check its architecture
    net = createNetwork(networkConfig2)

    links = net.getLinks()

    # These are all the links we're hoping to find
    desired_links=set(["sensorInput_0.dataOut-->L4Column_0.activeColumns",
      "L2Column_0.feedForwardOutput-->L4Column_0.apicalInput",
      "externalInput_0.dataOut-->L4Column_0.basalInput",
      "L4Column_0.predictedActiveCells-->"+
      "L2Column_0.feedforwardGrowthCandidates",
      "L4Column_0.activeCells-->L2Column_0.feedforwardInput",
      "sensorInput_0.resetOut-->L2Column_0.resetIn",
      "sensorInput_0.resetOut-->L4Column_0.resetIn",
      "sensorInput_1.dataOut-->L4Column_1.activeColumns",
      "L2Column_1.feedForwardOutput-->L4Column_1.apicalInput",
      "externalInput_1.dataOut-->L4Column_1.basalInput",
      "L4Column_1.predictedActiveCells-->"+
      "L2Column_1.feedforwardGrowthCandidates",
      "L4Column_1.activeCells-->L2Column_1.feedforwardInput",
      "sensorInput_1.resetOut-->L2Column_1.resetIn",
      "sensorInput_1.resetOut-->L4Column_1.resetIn",
      "sensorInput_2.dataOut-->L4Column_2.activeColumns",
      "L2Column_2.feedForwardOutput-->L4Column_2.apicalInput",
      "externalInput_2.dataOut-->L4Column_2.basalInput",
      "L4Column_2.predictedActiveCells-->"+
      "L2Column_2.feedforwardGrowthCandidates",
      "L4Column_2.activeCells-->L2Column_2.feedforwardInput",
      "sensorInput_2.resetOut-->L2Column_2.resetIn",
      "sensorInput_2.resetOut-->L4Column_2.resetIn",
      "L2Column_0.feedForwardOutput-->L2Column_1.lateralInput",
      "L2Column_0.feedForwardOutput-->L2Column_2.lateralInput",
      "L2Column_1.feedForwardOutput-->L2Column_0.lateralInput",
      "L2Column_1.feedForwardOutput-->L2Column_2.lateralInput",
      "L2Column_2.feedForwardOutput-->L2Column_0.lateralInput",
      "L2Column_2.feedForwardOutput-->L2Column_1.lateralInput",
      "externalInput_0.dataOut-->L4Column_0.basalGrowthCandidates",
      "externalInput_1.dataOut-->L4Column_1.basalGrowthCandidates",
      "externalInput_2.dataOut-->L4Column_2.basalGrowthCandidates"])

    # This gets textual representations of the links.
    links = set([link.second.getMoniker() for link in links])

    # Build a descriptive error message to pass to the user
    error_message = "Links incorrectly formed in multicolumn L2L4 network: \n"
    for link in desired_links:
      if not link in links:
        error_message += "Failed to find link: {}\n".format(link)

    for link in links:
      if not link in desired_links:
        error_message += "Found unexpected link: {}\n".format(link)

    self.assertSetEqual(desired_links, links, error_message)
コード例 #6
0
  def testLinks(self):
    """
    We create a network with 5 columns and check all the links are correct
    """
    config = copy.deepcopy(networkConfig)
    config["numCorticalColumns"] = 5

    # Create a simple network to test the sensor
    net = createNetwork(config)

    self.assertTrue(False)
コード例 #7
0
  def testLinks(self):
    """
    We create a network with 5 columns and check all the links are correct
    """
    config = copy.deepcopy(networkConfig)
    config["numCorticalColumns"] = 5

    # Create a simple network to test the sensor
    net = createNetwork(config)

    self.assertTrue(False)
コード例 #8
0
    def testMultipleL4L2ColumnsWithTopologyCreate(self):
        """
    In this simplistic test we create a network with 5 L4L2Columns and
    topological lateral connections, ensure it has the right number of regions,
    and try to run some inputs through it without crashing.
    """

        net = createNetwork(networkConfig4)
        self.assertEqual(len(net.regions.keys()), 20,
                         "Incorrect number of regions")

        # Add some input vectors to the queue
        externalInput0 = net.regions["externalInput_0"].getSelf()
        sensorInput0 = net.regions["sensorInput_0"].getSelf()
        externalInput1 = net.regions["externalInput_1"].getSelf()
        sensorInput1 = net.regions["sensorInput_1"].getSelf()
        externalInput2 = net.regions["externalInput_2"].getSelf()
        sensorInput2 = net.regions["sensorInput_2"].getSelf()
        externalInput3 = net.regions["externalInput_3"].getSelf()
        sensorInput3 = net.regions["sensorInput_3"].getSelf()
        externalInput4 = net.regions["externalInput_4"].getSelf()
        sensorInput4 = net.regions["sensorInput_4"].getSelf()

        externalInput0.addDataToQueue([2, 42, 1023], 0, 9)
        sensorInput0.addDataToQueue([2, 42, 1023], 0, 0)
        externalInput1.addDataToQueue([2, 42, 1023], 0, 9)
        sensorInput1.addDataToQueue([2, 42, 1023], 0, 0)
        externalInput2.addDataToQueue([2, 42, 1023], 0, 9)
        sensorInput2.addDataToQueue([2, 42, 1023], 0, 0)
        externalInput3.addDataToQueue([2, 42, 1023], 0, 9)
        sensorInput3.addDataToQueue([2, 42, 1023], 0, 0)
        externalInput4.addDataToQueue([2, 42, 1023], 0, 9)
        sensorInput4.addDataToQueue([2, 42, 1023], 0, 0)

        # Run the network and check outputs are as expected
        net.run(1)

        # Spotcheck some of the phases
        self.assertEqual(net.getPhases("externalInput_0"), (0, ),
                         "Incorrect phase externalInput_0")
        self.assertEqual(net.getPhases("externalInput_1"), (0, ),
                         "Incorrect phase for externalInput_1")
        self.assertEqual(net.getPhases("L4Column_0"), (2, ),
                         "Incorrect phase for L4Column_0")
        self.assertEqual(net.getPhases("L4Column_1"), (2, ),
                         "Incorrect phase for L4Column_1")
コード例 #9
0
  def testMultipleL4L2ColumnsWithTopologyCreate(self):
    """
    In this simplistic test we create a network with 5 L4L2Columns and
    topological lateral connections, ensure it has the right number of regions,
    and try to run some inputs through it without crashing.
    """

    net = createNetwork(networkConfig4)
    self.assertEqual(len(net.regions.keys()), 20, "Incorrect number of regions")

    # Add some input vectors to the queue
    externalInput0 = net.regions["externalInput_0"].getSelf()
    sensorInput0 = net.regions["sensorInput_0"].getSelf()
    externalInput1 = net.regions["externalInput_1"].getSelf()
    sensorInput1 = net.regions["sensorInput_1"].getSelf()
    externalInput2 = net.regions["externalInput_2"].getSelf()
    sensorInput2 = net.regions["sensorInput_2"].getSelf()
    externalInput3 = net.regions["externalInput_3"].getSelf()
    sensorInput3 = net.regions["sensorInput_3"].getSelf()
    externalInput4 = net.regions["externalInput_4"].getSelf()
    sensorInput4 = net.regions["sensorInput_4"].getSelf()

    externalInput0.addDataToQueue([2, 42, 1023], 0, 9)
    sensorInput0.addDataToQueue([2, 42, 1023], 0, 0)
    externalInput1.addDataToQueue([2, 42, 1023], 0, 9)
    sensorInput1.addDataToQueue([2, 42, 1023], 0, 0)
    externalInput2.addDataToQueue([2, 42, 1023], 0, 9)
    sensorInput2.addDataToQueue([2, 42, 1023], 0, 0)
    externalInput3.addDataToQueue([2, 42, 1023], 0, 9)
    sensorInput3.addDataToQueue([2, 42, 1023], 0, 0)
    externalInput4.addDataToQueue([2, 42, 1023], 0, 9)
    sensorInput4.addDataToQueue([2, 42, 1023], 0, 0)

    # Run the network and check outputs are as expected
    net.run(1)


    # Spotcheck some of the phases
    self.assertEqual(net.getPhases("externalInput_0"), (0,),
                     "Incorrect phase externalInput_0")
    self.assertEqual(net.getPhases("externalInput_1"), (0,),
                     "Incorrect phase for externalInput_1")
    self.assertEqual(net.getPhases("L4Column_0"), (2,),
                     "Incorrect phase for L4Column_0")
    self.assertEqual(net.getPhases("L4Column_1"), (2,),
                     "Incorrect phase for L4Column_1")
コード例 #10
0
  def testCreatingMultipleColumns(self):
    """
    We create a network with 5 columns
    """
    config = copy.deepcopy(networkConfig)
    config["numCorticalColumns"] = 6

    # Create a simple network to test the sensor
    net = createNetwork(config)

    self.assertEqual(len(net.regions.keys()),7*config["numCorticalColumns"],
                     "Incorrect number of regions")

    # Do regions have the correct phases?
    self._checkPhases(net)

    # Check we can run the network
    self._runNetwork(net, config["numCorticalColumns"])
コード例 #11
0
  def testCreatingMultipleColumns(self):
    """
    We create a network with 5 columns
    """
    config = copy.deepcopy(networkConfig)
    config["numCorticalColumns"] = 6

    # Create a simple network to test the sensor
    net = createNetwork(config)

    self.assertEqual(len(net.regions.keys()),7*config["numCorticalColumns"],
                     "Incorrect number of regions")

    # Do regions have the correct phases?
    self._checkPhases(net)

    # Check we can run the network
    self._runNetwork(net, config["numCorticalColumns"])
コード例 #12
0
  def testCreatingSingleColumn(self):
    """
    In this simplistic test we just create a network, ensure it has the
    right number of regions and try to run some inputs through it without
    crashing.
    """

    # Create a simple network to test the sensor
    net = createNetwork(networkConfig)

    # Does it have the correct number of regions?
    self.assertEqual(len(net.regions.keys()),7,
                     "Incorrect number of regions")

    # Do regions have the correct phases?
    self._checkPhases(net)

    # Check we can run the network
    self._runNetwork(net, 1)
コード例 #13
0
  def testCreatingSingleColumn(self):
    """
    In this simplistic test we just create a network, ensure it has the
    right number of regions and try to run some inputs through it without
    crashing.
    """

    # Create a simple network to test the sensor
    net = createNetwork(networkConfig)

    # Does it have the correct number of regions?
    self.assertEqual(len(net.regions.keys()),7,
                     "Incorrect number of regions")

    # Do regions have the correct phases?
    self._checkPhases(net)

    # Check we can run the network
    self._runNetwork(net, 1)
コード例 #14
0
    def testLinks(self):
        """
    In this simplistic test we create a network and ensure that it has the
    correct links between regions.
    """

        # Create a simple network to check its architecture
        net = createNetwork(networkConfig1)

        # These are exactly all the links we expect
        desired_links = {
            "sensorInput_0.dataOut-->L4Column_0.activeColumns",
            "sensorInput_0.dataOut-->TMColumn_0.activeColumns",
            "L2Column_0.feedForwardOutput-->L4Column_0.apicalInput",
            "externalInput_0.dataOut-->L4Column_0.basalInput",
            "L4Column_0.predictedActiveCells-->L2Column_0.feedforwardGrowthCandidates",
            "L4Column_0.activeCells-->L2Column_0.feedforwardInput",
            "sensorInput_0.resetOut-->L2Column_0.resetIn",
            "sensorInput_0.resetOut-->L4Column_0.resetIn",
            "sensorInput_0.resetOut-->TMColumn_0.resetIn",
            "externalInput_0.dataOut-->L4Column_0.basalGrowthCandidates",
        }

        links = net.getLinks()

        # This gets textual representations of the links.
        links = set([link.second.getMoniker() for link in links])

        # Build a descriptive error message to pass to the user
        error_message = "Error: Links incorrectly formed in simple network: \n"
        for link in desired_links:
            if not link in links:
                error_message += "Failed to find link: {}\n".format(link)

        for link in links:
            if not link in desired_links:
                error_message += "Found unexpected link: {}\n".format(link)

        self.assertSetEqual(desired_links, links, error_message)
コード例 #15
0
  def testLinks(self):
    """
    In this simplistic test we create a network and ensure that it has the
    correct links between regions.
    """

    # Create a simple network to check its architecture
    net = createNetwork(networkConfig1)

    # These are exactly all the links we expect
    desired_links = {
      "sensorInput_0.dataOut-->L4Column_0.activeColumns",
      "sensorInput_0.dataOut-->TMColumn_0.activeColumns",
      "externalInput_0.dataOut-->L4Column_0.basalInput",
      "L4Column_0.predictedActiveCells-->L2Column_0.feedforwardGrowthCandidates",
      "L4Column_0.activeCells-->L2Column_0.feedforwardInput",
      "sensorInput_0.resetOut-->L2Column_0.resetIn",
      "sensorInput_0.resetOut-->L4Column_0.resetIn",
      "sensorInput_0.resetOut-->TMColumn_0.resetIn",
      "externalInput_0.dataOut-->L4Column_0.basalGrowthCandidates",
    }

    links = net.getLinks()

    # This gets textual representations of the links.
    links = set([link.second.getMoniker() for link in links])

    # Build a descriptive error message to pass to the user
    error_message = "Error: Links incorrectly formed in simple network: \n"
    for link in desired_links:
      if not link in links:
        error_message += "Failed to find link: {}\n".format(link)

    for link in links:
      if not link in desired_links:
        error_message += "Found unexpected link: {}\n".format(link)

    self.assertSetEqual(desired_links, links, error_message)
コード例 #16
0
  def testSingleColumnL4L2DataFlow(self):
    """
    This test trains a network with a few (feature, location) pairs and checks
    the data flows correctly, and that each intermediate representation is
    correct.
    """

    # Create a simple network to test the sensor
    net = createNetwork(networkConfig1)

    self.assertEqual(
      len(net.regions.keys()), 4,
      "Incorrect number of regions"
    )

    # Get various regions
    externalInput = net.regions["externalInput_0"].getSelf()
    sensorInput = net.regions["sensorInput_0"].getSelf()
    L4Column = net.regions["L4Column_0"].getSelf()
    L2Column = net.regions["L2Column_0"].getSelf()

    # create a feature and location pool
    features = [self.generatePattern(1024, 20) for _ in xrange(2)]
    locations = [self.generatePattern(1024, 20) for _ in xrange(3)]

    # train with following pairs:
    # (F0, L0) (F1, L1) on object A
    # (F0, L2) (F1, L1) on object B

    # Object A

    # start with an object 1 input to get L2 representation for object 1
    sensorInput.addDataToQueue(features[0], 0, 0)
    externalInput.addDataToQueue(locations[0], 0, 0)
    net.run(1)

    # get L2 representation for object A
    L2RepresentationA = self.getCurrentL2Representation(L2Column)
    self.assertEqual(len(L2RepresentationA), 40)

    for _ in xrange(4):
      sensorInput.addDataToQueue(features[0], 0, 0)
      externalInput.addDataToQueue(locations[0], 0, 0)
      net.run(1)

      # check L2
      self.assertEqual(
        self.getCurrentL2Representation(L2Column),
        L2RepresentationA
      )
      sensorInput.addDataToQueue(features[1], 0, 0)
      externalInput.addDataToQueue(locations[1], 0, 0)
      net.run(1)

      # check L2
      self.assertEqual(
        self.getCurrentL2Representation(L2Column),
        L2RepresentationA
      )

    # get L4 representations when they are stable
    sensorInput.addDataToQueue(features[0], 0, 0)
    externalInput.addDataToQueue(locations[0], 0, 0)
    net.run(1)

    L4Representation00 = self.getL4PredictedActiveCells(L4Column)
    self.assertEqual(len(L4Representation00), 20)

    # send reset signal
    sensorInput.addResetToQueue(0)
    externalInput.addResetToQueue(0)
    net.run(1)

    # Object B

    # start with empty input
    sensorInput.addDataToQueue(features[0], 0, 0)
    externalInput.addDataToQueue(locations[2], 0, 0)
    net.run(1)

    # get L2 representation for object B
    L2RepresentationB = self.getCurrentL2Representation(L2Column)
    self.assertEqual(len(L2RepresentationB), 40)
    # check that it is very different from object A
    self.assertLessEqual(len(L2RepresentationA & L2RepresentationB), 5)

    for _ in xrange(4):
      sensorInput.addDataToQueue(features[0], 0, 0)
      externalInput.addDataToQueue(locations[2], 0, 0)
      net.run(1)

      # check L2
      self.assertEqual(
        self.getCurrentL2Representation(L2Column),
        L2RepresentationB
      )

      sensorInput.addDataToQueue(features[1], 0, 0)
      externalInput.addDataToQueue(locations[1], 0, 0)
      net.run(1)

      # check L2
      self.assertEqual(
        self.getCurrentL2Representation(L2Column),
        L2RepresentationB
      )

    # get L4 representations when they are stable
    sensorInput.addDataToQueue(features[0], 0, 0)
    externalInput.addDataToQueue(locations[2], 0, 0)
    net.run(1)

    L4Representation02 = self.getL4PredictedActiveCells(L4Column)
    self.assertEqual(len(L4Representation02), 20)

    sensorInput.addDataToQueue(features[1], 0, 0)
    externalInput.addDataToQueue(locations[1], 0, 0)
    net.run(1)

    L4Representation11 = self.getL4PredictedActiveCells(L4Column)
    self.assertEqual(len(L4Representation11), 20)

    # send reset signal
    sensorInput.addResetToQueue(0)
    externalInput.addResetToQueue(0)
    net.run(1)

    # check inference with each (feature, location) pair
    L2Column.setParameter("learningMode", 0, 0)
    L4Column.setParameter("learningMode", 0, 0)

    # (F0, L0)
    sensorInput.addDataToQueue(features[0], 0, 0)
    externalInput.addDataToQueue(locations[0], 0, 0)
    net.run(1)

    # check L2 representation, L4 representation, no bursting
    self.assertEqual(
      self.getCurrentL2Representation(L2Column),
      L2RepresentationA
    )
    self.assertEqual(
      self.getL4PredictedActiveCells(L4Column),
      L4Representation00
    )
    self.assertEqual(len(self.getL4BurstingCells(L4Column)), 0)

    # (F0, L2)
    sensorInput.addDataToQueue(features[0], 0, 0)
    externalInput.addDataToQueue(locations[2], 0, 0)
    net.run(1)

    # check L2 representation, L4 representation, no bursting
    self.assertEqual(
      self.getCurrentL2Representation(L2Column),
      L2RepresentationB
    )
    self.assertEqual(
      self.getL4PredictedActiveCells(L4Column),
      L4Representation02
    )
    self.assertEqual(len(self.getL4BurstingCells(L4Column)), 0)

    # (F1, L1)
    sensorInput.addDataToQueue(features[1], 0, 0)
    externalInput.addDataToQueue(locations[1], 0, 0)
    net.run(1)

    # check L2 representation, L4 representation, no bursting
    self.assertEqual(
      self.getCurrentL2Representation(L2Column),
      L2RepresentationA | L2RepresentationB
    )
    self.assertEqual(
      self.getL4PredictedActiveCells(L4Column),
      L4Representation11
    )
    self.assertEqual(len(self.getL4BurstingCells(L4Column)), 0)
    sensorInput.addDataToQueue(features[1], 0, 0)
    externalInput.addDataToQueue(locations[2], 0, 0)
    net.run(1)

    # check bursting (representation in L2 should be like in a random SP)
    self.assertEqual(len(self.getL4PredictedActiveCells(L4Column)), 0)
    self.assertEqual(len(self.getL4BurstingCells(L4Column)), 20 * 8)
コード例 #17
0
  def testCustomParameters(self):
    """
    This test creates a network with custom parameters and tests that the
    network gets correctly constructed.
    """
    customConfig = {
      "networkType": "L4L2Column",
      "externalInputSize": 256,
      "sensorInputSize": 512,
      "L4RegionType": "py.ApicalTMPairRegion",
      "L4Params": {
        "columnCount": 512,
        "cellsPerColumn": 16,
        "learn": True,
        "learnOnOneCell": False,
        "initialPermanence": 0.23,
        "connectedPermanence": 0.75,
        "permanenceIncrement": 0.45,
        "permanenceDecrement": 0.1,
        "minThreshold": 15,
        "basalPredictedSegmentDecrement": 0.21,
        "activationThreshold": 16,
        "sampleSize": 24,
      },
      "L2Params": {
        "inputWidth": 512 * 8,
        "cellCount": 2048,
        "sdrSize": 30,
        "synPermProximalInc": 0.12,
        "synPermProximalDec": 0.011,
        "initialProximalPermanence": 0.8,
        "minThresholdProximal": 8,
        "sampleSizeProximal": 17,
        "connectedPermanenceProximal": 0.6,
        "synPermDistalInc": 0.09,
        "synPermDistalDec": 0.002,
        "initialDistalPermanence": 0.52,
        "activationThresholdDistal": 15,
        "sampleSizeDistal": 25,
        "connectedPermanenceDistal": 0.6,
        "distalSegmentInhibitionFactor": 0.8333,
        "learningMode": True,
      },
    }

    net = createNetwork(customConfig)

    self.assertEqual(
      len(net.regions.keys()), 4,
      "Incorrect number of regions"
    )

    # Get various regions
    externalInput = net.regions["externalInput_0"].getSelf()
    sensorInput = net.regions["sensorInput_0"].getSelf()
    L4Column = net.regions["L4Column_0"].getSelf()
    L2Column = net.regions["L2Column_0"].getSelf()

    # we need to do a first compute for the various elements to be constructed
    sensorInput.addDataToQueue([], 0, 0)
    externalInput.addDataToQueue([], 0, 0)
    net.run(1)

    # check that parameters are correct in L4
    for param, value in customConfig["L4Params"].iteritems():
      self.assertEqual(L4Column.getParameter(param), value)

    # check that parameters are correct in L2
    # some parameters are in the tm members
    for param, value in customConfig["L2Params"].iteritems():
      self.assertEqual(L2Column.getParameter(param), value)

    # check that parameters are correct in L2
    self.assertEqual(externalInput.outputWidth,
                     customConfig["externalInputSize"])
    self.assertEqual(sensorInput.outputWidth,
                     customConfig["sensorInputSize"])
コード例 #18
0
  def __init__(self,
               name,
               numCorticalColumns=1,
               inputSize=1024,
               numInputBits=20,
               externalInputSize=1024,
               numExternalInputBits=20,
               L2Overrides=None,
               L4RegionType="py.ApicalTMPairRegion",
               networkType = "MultipleL4L2Columns",
               longDistanceConnections = 0,
               maxConnectionDistance = 1,
               columnPositions = None,
               L4Overrides=None,
               numLearningPoints=3,
               seed=42,
               logCalls=False,
               enableLateralSP=False,
               lateralSPOverrides=None,
               enableFeedForwardSP=False,
               feedForwardSPOverrides=None,
               objectNamesAreIndices=False,
               enableFeedback=True
               ):
    """
    Creates the network.

    Parameters:
    ----------------------------
    @param   name (str)
             Experiment name

    @param   numCorticalColumns (int)
             Number of cortical columns in the network

    @param   inputSize (int)
             Size of the sensory input

    @param   numInputBits (int)
             Number of ON bits in the generated input patterns

    @param   externalInputSize (int)
             Size of the lateral input to L4 regions

    @param   numExternalInputBits (int)
             Number of ON bits in the external input patterns

    @param   L2Overrides (dict)
             Parameters to override in the L2 region

    @param   L4RegionType (string)
             The type of region to use for L4

    @param   networkType (string)
             Which type of L2L4 network to create.  If topology is being used,
             it should be specified here.  Possible values for this parameter
             are "MultipleL4L2Columns", "MultipleL4L2ColumnsWithTopology" and
             "L4L2Column"

    @param  longDistanceConnections (float)
             The probability that a column will randomly connect to a distant
             column.  Should be in [0, 1).  Only relevant when using multiple
             columns with topology.

    @param   L4Overrides (dict)
             Parameters to override in the L4 region

    @param   numLearningPoints (int)
             Number of times each pair should be seen to be learnt

    @param   logCalls (bool)
             If true, calls to main functions will be logged internally. The
             log can then be saved with saveLogs(). This allows us to recreate
             the complete network behavior using rerunExperimentFromLogfile
             which is very useful for debugging.

    @param   enableLateralSP (bool)
             If true, Spatial Pooler will be added between external input and
             L4 lateral input

    @param   lateralSPOverrides
             Parameters to override in the lateral SP region

    @param   enableFeedForwardSP (bool)
             If true, Spatial Pooler will be added between external input and
             L4 feed-forward input

    @param   feedForwardSPOverrides
             Parameters to override in the feed-forward SP region

    @param   objectNamesAreIndices (bool)
             If True, object names are used as indices in the
             getCurrentObjectOverlaps method. Object names must be positive
             integers. If False, object names can be strings, and indices will
             be assigned to each object name.

    @param   enableFeedback (bool)
             If True, enable feedback between L2 and L4

    """
    # Handle logging - this has to be done first
    self.logCalls = logCalls

    registerAllResearchRegions()
    self.name = name

    self.numLearningPoints = numLearningPoints
    self.numColumns = numCorticalColumns
    self.inputSize = inputSize
    self.externalInputSize = externalInputSize
    self.numInputBits = numInputBits
    self.objectNamesAreIndices = objectNamesAreIndices

    # seed
    self.seed = seed
    random.seed(seed)

    # update parameters with overrides
    self.config = {
      "networkType": networkType,
      "longDistanceConnections": longDistanceConnections,
      "enableFeedback": enableFeedback,
      "numCorticalColumns": numCorticalColumns,
      "externalInputSize": externalInputSize,
      "sensorInputSize": inputSize,
      "L4RegionType": L4RegionType,
      "L4Params": self.getDefaultL4Params(inputSize, numExternalInputBits),
      "L2Params": self.getDefaultL2Params(inputSize, numInputBits),
    }

    if enableLateralSP:
      self.config["lateralSPParams"] = self.getDefaultLateralSPParams(inputSize)
      if lateralSPOverrides:
        self.config["lateralSPParams"].update(lateralSPOverrides)

    if enableFeedForwardSP:
      self.config["feedForwardSPParams"] = self.getDefaultFeedForwardSPParams(inputSize)
      if feedForwardSPOverrides:
        self.config["feedForwardSPParams"].update(feedForwardSPOverrides)

    if "Topology" in self.config["networkType"]:
      self.config["maxConnectionDistance"] = maxConnectionDistance

      # Generate a grid for cortical columns.  Will attempt to generate a full
      # square grid, and cut out positions starting from the bottom-right if the
      # number of cortical columns is not a perfect square.
      if columnPositions is None:
        columnPositions = []
        side_length = int(np.ceil(np.sqrt(numCorticalColumns)))
        for i in range(side_length):
          for j in range(side_length):
            columnPositions.append((i, j))
      self.config["columnPositions"] = columnPositions[:numCorticalColumns]
      self.config["longDistanceConnections"] = longDistanceConnections

    if L2Overrides is not None:
      self.config["L2Params"].update(L2Overrides)

    if L4Overrides is not None:
      self.config["L4Params"].update(L4Overrides)

    # create network
    self.network = createNetwork(self.config)
    self.sensorInputs = []
    self.externalInputs = []
    self.L4Regions = []
    self.L2Regions = []

    for i in xrange(self.numColumns):
      self.sensorInputs.append(
        self.network.regions["sensorInput_" + str(i)].getSelf()
      )
      self.externalInputs.append(
        self.network.regions["externalInput_" + str(i)].getSelf()
      )
      self.L4Regions.append(
        self.network.regions["L4Column_" + str(i)]
      )
      self.L2Regions.append(
        self.network.regions["L2Column_" + str(i)]
      )

    self.L4Columns = [region.getSelf() for region in self.L4Regions]
    self.L2Columns = [region.getSelf() for region in self.L2Regions]

    # will be populated during training
    self.objectL2Representations = {}
    self.objectL2RepresentationsMatrices = [
      SparseMatrix(0, self.config["L2Params"]["cellCount"])
      for _ in xrange(self.numColumns)]
    self.objectNameToIndex = {}
    self.resetStatistics()
コード例 #19
0
    def __init__(
        self,
        name,
        numCorticalColumns=1,
        inputSize=1024,
        numInputBits=20,
        externalInputSize=1024,
        numExternalInputBits=20,
        L2Overrides=None,
        L4Overrides=None,
        seed=42,
        logCalls=False,
        objectNamesAreIndices=False,
    ):
        """
    Creates the network.

    Parameters:
    ----------------------------
    @param   TMOverrides (dict)
             Parameters to override in the TM region
    """

        # Handle logging - this has to be done first
        self.logCalls = logCalls

        registerAllResearchRegions()
        self.name = name

        self.numLearningPoints = 1
        self.numColumns = numCorticalColumns
        self.inputSize = inputSize
        self.externalInputSize = externalInputSize
        self.numInputBits = numInputBits
        self.objectNamesAreIndices = objectNamesAreIndices
        self.numExternalInputBits = numExternalInputBits

        # seed
        self.seed = seed
        random.seed(seed)

        # Create default parameters and then update with overrides
        self.config = {
            "networkType": "CombinedSequenceColumn",
            "numCorticalColumns": numCorticalColumns,
            "externalInputSize": externalInputSize,
            "sensorInputSize": inputSize,
            "enableFeedback": False,
            "L2Params": self.getDefaultL2Params(inputSize, numInputBits),
        }
        self.config["L4Params"] = self._getDefaultCombinedL4Params(
            self.numInputBits, self.inputSize, self.numExternalInputBits,
            self.externalInputSize, self.config["L2Params"]["cellCount"])

        if L2Overrides is not None:
            self.config["L2Params"].update(L2Overrides)

        if L4Overrides is not None:
            self.config["L4Params"].update(L4Overrides)

        pprint.pprint(self.config)

        # Recreate network including TM parameters
        self.network = createNetwork(self.config)
        self.sensorInputs = []
        self.externalInputs = []
        self.L2Regions = []
        self.L4Regions = []

        for i in xrange(self.numColumns):
            self.sensorInputs.append(self.network.regions["sensorInput_" +
                                                          str(i)].getSelf())
            self.externalInputs.append(self.network.regions["externalInput_" +
                                                            str(i)].getSelf())
            self.L2Regions.append(self.network.regions["L2Column_" + str(i)])
            self.L4Regions.append(self.network.regions["L4Column_" + str(i)])

        self.L2Columns = [region.getSelf() for region in self.L2Regions]
        self.L4Columns = [region.getSelf() for region in self.L4Regions]

        # will be populated during training
        self.objectL2Representations = {}
        self.objectL2RepresentationsMatrices = [
            SparseMatrix(0, self.config["L2Params"]["cellCount"])
            for _ in xrange(self.numColumns)
        ]
        self.objectNameToIndex = {}
        self.statistics = []
コード例 #20
0
    def testDataFlowTM(self):
        """
    This test trains a network with two high order sequences and checks
    the data flows correctly, and that the TM learns them correctly.
    """

        # Create a simple network to test the sensor
        net = createNetwork(networkConfig1)

        # Get various regions
        externalInput = net.regions["externalInput_0"].getSelf()
        sensorInput = net.regions["sensorInput_0"].getSelf()
        L4Column = net.regions["L4Column_0"].getSelf()
        L2Column = net.regions["L2Column_0"].getSelf()
        TMColumn = net.regions["TMColumn_0"].getSelf()

        # create a feature and location pool
        features = [self.generatePattern(1024, 20) for _ in xrange(5)]

        # train with following sequences:
        # 1 : F0 F1 F2
        # 2 : F3 F1 F4

        # Sequence A, three repeats with a reset in between.  We add nothing for
        # location signal
        for _ in range(3):
            sensorInput.addDataToQueue(features[0], 0, 0)
            sensorInput.addDataToQueue(features[1], 0, 0)
            sensorInput.addDataToQueue(features[2], 0, 0)

            externalInput.addDataToQueue([], 0, 0)
            externalInput.addDataToQueue([], 0, 0)
            externalInput.addDataToQueue([], 0, 0)

            sensorInput.addResetToQueue(0)
            externalInput.addResetToQueue(0)

        net.run(4 * 3)  # Includes reset

        # Sequence B, three repeats with a reset in between.
        for _ in range(3):
            sensorInput.addDataToQueue(features[3], 0, 0)
            sensorInput.addDataToQueue(features[1], 0, 0)
            sensorInput.addDataToQueue(features[4], 0, 0)

            externalInput.addDataToQueue([], 0, 0)
            externalInput.addDataToQueue([], 0, 0)
            externalInput.addDataToQueue([], 0, 0)

            sensorInput.addResetToQueue(0)
            externalInput.addResetToQueue(0)

        net.run(4 * 3)  # Includes reset

        # check inference
        L2Column.setParameter("learningMode", 0, False)
        L4Column.setParameter("learn", 0, False)
        TMColumn.setParameter("learn", 0, False)

        # Sequence A with a reset in between.
        sensorInput.addDataToQueue(features[0], 0, 0)
        externalInput.addDataToQueue([], 0, 0)
        net.run(1)
        self.assertEqual(len(self.getPredictedActiveCells(TMColumn)), 0)
        self.assertEqual(len(self.getActiveCells(TMColumn)),
                         TMColumn.cellsPerColumn * 20)

        sensorInput.addDataToQueue(features[1], 0, 0)
        externalInput.addDataToQueue([], 0, 0)
        net.run(1)
        self.assertEqual(len(self.getPredictedActiveCells(TMColumn)), 20)
        self.assertEqual(len(self.getActiveCells(TMColumn)), 20)
        predictedActiveCellsS1 = self.getPredictedActiveCells(TMColumn)

        sensorInput.addDataToQueue(features[2], 0, 0)
        externalInput.addDataToQueue([], 0, 0)
        net.run(1)
        self.assertEqual(len(self.getPredictedActiveCells(TMColumn)), 20)
        self.assertEqual(len(self.getActiveCells(TMColumn)), 20)

        sensorInput.addResetToQueue(0)
        externalInput.addResetToQueue(0)
        net.run(1)

        # Sequence B with a reset
        sensorInput.addDataToQueue(features[3], 0, 0)
        externalInput.addDataToQueue([], 0, 0)
        net.run(1)
        self.assertEqual(len(self.getPredictedActiveCells(TMColumn)), 0)
        self.assertEqual(len(self.getActiveCells(TMColumn)),
                         TMColumn.cellsPerColumn * 20)

        sensorInput.addDataToQueue(features[1], 0, 0)
        externalInput.addDataToQueue([], 0, 0)
        net.run(1)
        self.assertEqual(len(self.getPredictedActiveCells(TMColumn)), 20)
        self.assertEqual(len(self.getActiveCells(TMColumn)), 20)
        predictedActiveCellsS2 = self.getPredictedActiveCells(TMColumn)

        sensorInput.addDataToQueue(features[4], 0, 0)
        externalInput.addDataToQueue([], 0, 0)
        net.run(1)
        self.assertEqual(len(self.getPredictedActiveCells(TMColumn)), 20)
        self.assertEqual(len(self.getActiveCells(TMColumn)), 20)

        # Ensure representation for ambiguous element is different
        self.assertFalse(predictedActiveCellsS1 == predictedActiveCellsS2)
コード例 #21
0
  def testTwoColumnsL4L2DataFlow(self):
    """
    This test trains a network with a few (feature, location) pairs and checks
    the data flows correctly, and that each intermediate representation is
    correct.

    Indices 0 and 1 in variable names refer to cortical column number.
    """

    # Create a simple network to test the sensor
    net = createNetwork(networkConfig3)

    self.assertEqual(
      len(net.regions.keys()), 4 * 2,
      "Incorrect number of regions"
    )

    # Get various regions
    externalInput0 = net.regions["externalInput_0"].getSelf()
    sensorInput0 = net.regions["sensorInput_0"].getSelf()
    L4Column0 = net.regions["L4Column_0"].getSelf()
    L2Column0 = net.regions["L2Column_0"].getSelf()

    externalInput1 = net.regions["externalInput_1"].getSelf()
    sensorInput1 = net.regions["sensorInput_1"].getSelf()
    L4Column1 = net.regions["L4Column_1"].getSelf()
    L2Column1 = net.regions["L2Column_1"].getSelf()

    # create a feature and location pool for column 0
    features0 = [self.generatePattern(1024, 20) for _ in xrange(2)]
    locations0 = [self.generatePattern(1024, 20) for _ in xrange(3)]

    # create a feature and location pool for column 1
    features1 = [self.generatePattern(1024, 20) for _ in xrange(2)]
    locations1 = [self.generatePattern(1024, 20) for _ in xrange(3)]

    # train with following pairs:
    # (F0, L0) (F1, L1) on object 1
    # (F0, L2) (F1, L1) on object 2

    # Object 1

    # start with an object A input to get L2 representations for object A
    sensorInput0.addDataToQueue(features0[0], 0, 0)
    externalInput0.addDataToQueue(locations0[0], 0, 0)
    sensorInput1.addDataToQueue(features1[0], 0, 0)
    externalInput1.addDataToQueue(locations1[0], 0, 0)
    net.run(1)

    # get L2 representation for object B
    L2RepresentationA0 = self.getCurrentL2Representation(L2Column0)
    L2RepresentationA1 = self.getCurrentL2Representation(L2Column1)
    self.assertEqual(len(L2RepresentationA0), 40)
    self.assertEqual(len(L2RepresentationA0), 40)

    for _ in xrange(3):
      sensorInput0.addDataToQueue(features0[0], 0, 0)
      externalInput0.addDataToQueue(locations0[0], 0, 0)
      sensorInput1.addDataToQueue(features1[0], 0, 0)
      externalInput1.addDataToQueue(locations1[0], 0, 0)
      net.run(1)

      # check L2
      self.assertEqual(
        self.getCurrentL2Representation(L2Column0),
        L2RepresentationA0
      )
      # check L2
      self.assertEqual(
        self.getCurrentL2Representation(L2Column1),
        L2RepresentationA1
      )
      sensorInput0.addDataToQueue(features0[1], 0, 0)
      externalInput0.addDataToQueue(locations0[1], 0, 0)
      sensorInput1.addDataToQueue(features1[1], 0, 0)
      externalInput1.addDataToQueue(locations1[1], 0, 0)
      net.run(1)

      # check L2
      self.assertEqual(
        self.getCurrentL2Representation(L2Column0),
        L2RepresentationA0
      )
      self.assertEqual(
        self.getCurrentL2Representation(L2Column1),
        L2RepresentationA1
      )

    # get L4 representations when they are stable
    sensorInput0.addDataToQueue(features0[0], 0, 0)
    externalInput0.addDataToQueue(locations0[0], 0, 0)
    sensorInput1.addDataToQueue(features1[0], 0, 0)
    externalInput1.addDataToQueue(locations1[0], 0, 0)
    net.run(1)

    L4Representation00_0 = self.getL4PredictedActiveCells(L4Column0)
    L4Representation00_1 = self.getL4PredictedActiveCells(L4Column1)
    self.assertEqual(len(L4Representation00_0), 20)
    self.assertEqual(len(L4Representation00_1), 20)

    # send reset signal
    sensorInput0.addResetToQueue(0)
    externalInput0.addResetToQueue(0)
    sensorInput1.addResetToQueue(0)
    externalInput1.addResetToQueue(0)
    net.run(1)

    # Object B

    # start with input to get L2 representations
    sensorInput0.addDataToQueue(features0[0], 0, 0)
    externalInput0.addDataToQueue(locations0[2], 0, 0)
    sensorInput1.addDataToQueue(features1[0], 0, 0)
    externalInput1.addDataToQueue(locations1[2], 0, 0)
    net.run(1)

    # get L2 representations for object B
    L2RepresentationB0 = self.getCurrentL2Representation(L2Column0)
    L2RepresentationB1 = self.getCurrentL2Representation(L2Column1)
    self.assertEqual(len(L2RepresentationB0), 40)
    self.assertEqual(len(L2RepresentationB1), 40)
    # check that it is very different from object A
    self.assertLessEqual(len(L2RepresentationA0 & L2RepresentationB0), 5)
    self.assertLessEqual(len(L2RepresentationA1 & L2RepresentationB1), 5)

    for _ in xrange(3):
      sensorInput0.addDataToQueue(features0[0], 0, 0)
      externalInput0.addDataToQueue(locations0[2], 0, 0)
      sensorInput1.addDataToQueue(features1[0], 0, 0)
      externalInput1.addDataToQueue(locations1[2], 0, 0)
      net.run(1)

      # check L2
      self.assertEqual(
        self.getCurrentL2Representation(L2Column0),
        L2RepresentationB0
      )
      # check L2
      self.assertEqual(
        self.getCurrentL2Representation(L2Column1),
        L2RepresentationB1
      )

      sensorInput0.addDataToQueue(features0[1], 0, 0)
      externalInput0.addDataToQueue(locations0[1], 0, 0)
      sensorInput1.addDataToQueue(features1[1], 0, 0)
      externalInput1.addDataToQueue(locations1[1], 0, 0)
      net.run(1)

      # check L2
      self.assertEqual(
        self.getCurrentL2Representation(L2Column0),
        L2RepresentationB0
      )
      # check L2
      self.assertEqual(
        self.getCurrentL2Representation(L2Column1),
        L2RepresentationB1
      )

    # get L4 representations when they are stable
    sensorInput0.addDataToQueue(features0[0], 0, 0)
    externalInput0.addDataToQueue(locations0[2], 0, 0)
    sensorInput1.addDataToQueue(features1[0], 0, 0)
    externalInput1.addDataToQueue(locations1[2], 0, 0)
    net.run(1)

    L4Representation02_0 = self.getL4PredictedActiveCells(L4Column0)
    L4Representation02_1 = self.getL4PredictedActiveCells(L4Column1)
    self.assertEqual(len(L4Representation02_0), 20)
    self.assertEqual(len(L4Representation02_1), 20)

    sensorInput0.addDataToQueue(features0[1], 0, 0)
    externalInput0.addDataToQueue(locations0[1], 0, 0)
    sensorInput1.addDataToQueue(features1[1], 0, 0)
    externalInput1.addDataToQueue(locations1[1], 0, 0)
    net.run(1)

    L4Representation11_0 = self.getL4PredictedActiveCells(L4Column0)
    L4Representation11_1 = self.getL4PredictedActiveCells(L4Column1)
    self.assertEqual(len(L4Representation11_0), 20)
    self.assertEqual(len(L4Representation11_1), 20)

    sensorInput0.addResetToQueue(0)
    externalInput0.addResetToQueue(0)
    sensorInput1.addResetToQueue(0)
    externalInput1.addResetToQueue(0)
    net.run(1)

    # check inference with each (feature, location) pair
    L2Column0.setParameter("learningMode", 0, 0)
    L4Column0.setParameter("learningMode", 0, 0)
    L2Column1.setParameter("learningMode", 0, 0)
    L4Column1.setParameter("learningMode", 0, 0)

    # (F0, L0)
    sensorInput0.addDataToQueue(features0[0], 0, 0)
    externalInput0.addDataToQueue(locations0[0], 0, 0)
    sensorInput1.addDataToQueue(features1[0], 0, 0)
    externalInput1.addDataToQueue(locations1[0], 0, 0)
    net.run(1)

    # check L2 representations, L4 representations, no bursting
    self.assertLessEqual(
      len(self.getCurrentL2Representation(L2Column0) - L2RepresentationA0),
      5
    )
    self.assertGreaterEqual(
      len(self.getCurrentL2Representation(L2Column0) & L2RepresentationA0),
      35
    )
    self.assertEqual(
      self.getL4PredictedActiveCells(L4Column0),
      L4Representation00_0
    )
    self.assertEqual(len(self.getL4BurstingCells(L4Column0)), 0)

    # be a little tolerant on this test
    self.assertLessEqual(
      len(self.getCurrentL2Representation(L2Column1) - L2RepresentationA1),
      5
    )
    self.assertGreaterEqual(
      len(self.getCurrentL2Representation(L2Column1) & L2RepresentationA1),
      35
    )
    self.assertEqual(
      self.getL4PredictedActiveCells(L4Column1),
      L4Representation00_1
    )
    self.assertEqual(len(self.getL4BurstingCells(L4Column1)), 0)

    # (F0, L2)
    # It is fed twice, for the ambiguous prediction test, because of the
    # one-off error in distal predictions
    # FIXME when this is changed in ColumnPooler
    sensorInput0.addDataToQueue(features0[0], 0, 0)
    externalInput0.addDataToQueue(locations0[2], 0, 0)
    sensorInput1.addDataToQueue(features1[0], 0, 0)
    externalInput1.addDataToQueue(locations1[2], 0, 0)

    sensorInput0.addDataToQueue(features0[0], 0, 0)
    externalInput0.addDataToQueue(locations0[2], 0, 0)
    sensorInput1.addDataToQueue(features1[0], 0, 0)
    externalInput1.addDataToQueue(locations1[2], 0, 0)
    net.run(2)

    # check L2 representation, L4 representation, no bursting
    self.assertEqual(
      self.getCurrentL2Representation(L2Column0),
      L2RepresentationB0
    )
    self.assertEqual(
      self.getL4PredictedActiveCells(L4Column0),
      L4Representation02_0
    )
    self.assertEqual(len(self.getL4BurstingCells(L4Column0)), 0)

    self.assertEqual(
      self.getCurrentL2Representation(L2Column1),
      L2RepresentationB1
    )
    self.assertEqual(
      self.getL4PredictedActiveCells(L4Column1),
      L4Representation02_1
    )
    self.assertEqual(len(self.getL4BurstingCells(L4Column1)), 0)

    # check predictions for next step...
    self.assertEqual(
      self.getCurrentL2PredictiveCells(L2Column0),
      L2RepresentationB0
    )
    self.assertEqual(
      self.getCurrentL2PredictiveCells(L2Column1),
      L2RepresentationB1
    )

    # ambiguous pattern: (F1, L1)
    sensorInput0.addDataToQueue(features0[1], 0, 0)
    externalInput0.addDataToQueue(locations0[1], 0, 0)
    sensorInput1.addDataToQueue(features1[1], 0, 0)
    externalInput1.addDataToQueue(locations1[1], 0, 0)
    net.run(1)

    # check L2 representation, L4 representation, no bursting
    # as opposed to the previous test, the representation is not ambiguous
    self.assertEqual(
      self.getCurrentL2Representation(L2Column0),
      L2RepresentationB0
    )
    self.assertEqual(
      self.getL4PredictedActiveCells(L4Column0),
      L4Representation11_0
    )
    self.assertEqual(len(self.getL4BurstingCells(L4Column0)), 0)

    self.assertEqual(
      self.getCurrentL2Representation(L2Column1),
      L2RepresentationB1
    )
    self.assertEqual(
      self.getL4PredictedActiveCells(L4Column1),
      L4Representation11_1
    )
    self.assertEqual(len(self.getL4BurstingCells(L4Column1)), 0)

    # unknown signal
    sensorInput0.addDataToQueue(features0[1], 0, 0)
    externalInput0.addDataToQueue(locations0[2], 0, 0)
    sensorInput1.addDataToQueue(features1[1], 0, 0)
    externalInput1.addDataToQueue(locations1[2], 0, 0)
    net.run(1)

    # check bursting (representation in L2 should be like in a random SP)
    self.assertLessEqual(len(self.getL4PredictedActiveCells(L4Column0)), 3)
    self.assertGreaterEqual(len(self.getL4BurstingCells(L4Column0)), 20 * 7)
    self.assertLessEqual(len(self.getL4PredictedActiveCells(L4Column1)), 3)
    self.assertGreaterEqual(len(self.getL4BurstingCells(L4Column1)), 20 * 7)
コード例 #22
0
  def testCustomParameters(self):
    """
    This test creates a network with custom parameters and tests that the
    network gets correctly constructed.
    """
    customConfig = {
      "networkType": "L4L2Column",
      "externalInputSize": 256,
      "sensorInputSize": 512,
      "L4Params": {
        "columnCount": 512,
        "cellsPerColumn": 16,
        "formInternalConnections": 1,
        "learningMode": 1,
        "inferenceMode": 1,
        "learnOnOneCell": 0,
        "initialPermanence": 0.23,
        "connectedPermanence": 0.75,
        "permanenceIncrement": 0.45,
        "permanenceDecrement": 0.1,
        "minThreshold": 15,
        "predictedSegmentDecrement": 0.21,
        "activationThreshold": 16,
        "maxNewSynapseCount": 24,

      },
      "L2Params": {
        "columnCount": 2048,
        "inputWidth": 512 * 16,
        "learningMode": 1,
        "inferenceMode": 1,
        "initialPermanence": 0.45,
        "connectedPermanence": 0.75,
        "permanenceIncrement": 0.23,
        "permanenceDecrement": 0.2,
        "minThreshold": 12,
        "predictedSegmentDecrement": 0.03,
        "activationThreshold": 8,
        "maxNewSynapseCount": 15,
        "numActiveColumnsPerInhArea": 35,
        "synPermProximalInc": 0.12,
        "synPermProximalDec": 0.1,
        "initialProximalPermanence": 0.56
      }
    }

    net = createNetwork(customConfig)

    self.assertEqual(
      len(net.regions.keys()), 4,
      "Incorrect number of regions"
    )

    # Get various regions
    externalInput = net.regions["externalInput_0"].getSelf()
    sensorInput = net.regions["sensorInput_0"].getSelf()
    L4Column = net.regions["L4Column_0"].getSelf()
    L2Column = net.regions["L2Column_0"].getSelf()

    # we need to do a first compute for the various elements to be constructed
    sensorInput.addDataToQueue([], 0, 0)
    externalInput.addDataToQueue([], 0, 0)
    net.run(1)

    # check that parameters are correct in L4
    for param, value in customConfig["L4Params"].iteritems():
      self.assertEqual(L4Column.getParameter(param), value)

    # check that parameters are correct in L2
    # some parameters are in the tm members
    for param, value in customConfig["L2Params"].iteritems():
      self.assertEqual(L2Column.getParameter(param), value)

    # check that parameters are correct in L2
    self.assertEqual(externalInput.outputWidth,
                     customConfig["externalInputSize"])
    self.assertEqual(sensorInput.outputWidth,
                     customConfig["sensorInputSize"])
コード例 #23
0
    def __init__(self,
                 numCorticalColumns=1,
                 inputSize=2048,
                 numInputBits=40,
                 L2Overrides=None,
                 L4Overrides=None,
                 numLearningPasses=4,
                 seed=42):
        """
    Creates the network and initialize the experiment.

    Parameters:
    ----------------------------
    @param   numCorticalColumns (int)
             Number of cortical columns in the network

    @param   inputSize (int)
             Size of the sensory input

    @param   numInputBits (int)
             Number of ON bits in the generated input patterns

    @param   L2Overrides (dict)
             Parameters to override in the L2 region

    @param   L4Overrides
             Parameters to override in the L4 region

    @param   numLearningPasses (int)
             Number of times each pair should be seen to be learnt
    """
        registerAllResearchRegions()

        self.numLearningPoints = numLearningPasses
        self.numColumns = numCorticalColumns
        self.inputSize = inputSize
        self.numInputBits = numInputBits

        # seed
        self.seed = seed
        random.seed(seed)
        # update parameters with overrides
        self.config = {
            "networkType": "MultipleL4L2Columns",
            "numCorticalColumns": numCorticalColumns,
            "externalInputSize": 0,
            "sensorInputSize": inputSize,
            "L4RegionType": "py.ExtendedTMRegion",
            "L4Params": self.getDefaultL4Params(inputSize),
            "L2Params": self.getDefaultL2Params(inputSize),
        }

        if L2Overrides is not None:
            self.config["L2Params"].update(L2Overrides)

        if L4Overrides is not None:
            self.config["L4Params"].update(L4Overrides)

        # create network
        self.network = createNetwork(self.config)

        # We have to explicitly initialize if we are going to change the phases
        self.network.initialize()

        self.sensorInputs = []
        self.L4Columns = []
        self.L2Columns = []

        for i in xrange(self.numColumns):
            self.sensorInputs.append(self.network.regions["sensorInput_" +
                                                          str(i)].getSelf())
            self.L4Columns.append(self.network.regions["L4Column_" +
                                                       str(i)].getSelf())
            self.L2Columns.append(self.network.regions["L2Column_" +
                                                       str(i)].getSelf())

        # will be populated during training
        self.objectL2Representations = {}
        self.statistics = []
コード例 #24
0
ファイル: l2456_model.py プロジェクト: mewbak/nupic.research
  def __init__(self,
               name,
               numCorticalColumns=1,
               L2Overrides={},
               L4Overrides={},
               L5Overrides={},
               L6Overrides={},
               numLearningPoints=3,
               seed=42,
               logCalls = False
               ):
    """
    Creates the network.

    Parameters:
    ----------------------------
    @param   name (str)
             Experiment name

    @param   numCorticalColumns (int)
             Number of cortical columns in the network

    @param   L2Overrides (dict)
             Parameters to override in the L2 region

    @param   L4Overrides (dict)
             Parameters to override in the L4 region

    @param   L5Overrides (dict)
             Parameters to override in the L5 region

    @param   L6Overrides (dict)
             Parameters to override in the L6 region

    @param   numLearningPoints (int)
             Number of times each pair should be seen to be learnt

    @param   logCalls (bool)
             If true, calls to main functions will be logged internally. The
             log can then be saved with saveLogs(). This allows us to recreate
             the complete network behavior using rerunExperimentFromLogfile
             which is very useful for debugging.
    """
    # Handle logging - this has to be done first
    self.logCalls = logCalls

    registerAllResearchRegions()
    self.name = name

    self.numLearningPoints = numLearningPoints
    self.numColumns = numCorticalColumns
    self.sensorInputSize = 2048
    self.numInputBits = 40

    # seed
    self.seed = seed
    random.seed(seed)

    # Get network parameters and update with overrides
    self.config = {
      "networkType": "L2456Columns",
      "numCorticalColumns": numCorticalColumns,
      "randomSeedBase": self.seed,
    }
    self.config.update(self.getDefaultParams())

    self.config["L2Params"].update(L2Overrides)
    self.config["L4Params"].update(L4Overrides)
    self.config["L5Params"].update(L5Overrides)
    self.config["L6Params"].update(L6Overrides)

    # create network and retrieve regions
    self.network = createNetwork(self.config)
    self._retrieveRegions()

    # will be populated during training
    self.objectRepresentationsL2 = {}
    self.objectRepresentationsL5 = {}
    self.statistics = []
コード例 #25
0
  def __init__(self,
               name,
               numCorticalColumns=1,
               inputSize=1024,
               numInputBits=20,
               externalInputSize=1024,
               L2Overrides=None,
               L4Overrides=None,
               numLearningPoints=4,
               seed=42):
    """
    Creates the network.

    Parameters:
    ----------------------------
    @param   name (str)
             Experiment name

    @param   numCorticalColumns (int)
             Number of cortical columns in the network

    @param   inputSize (int)
             Size of the sensory input

    @param   numInputBits (int)
             Number of ON bits in the generated input patterns

    @param   externalInputSize (int)
             Size of the lateral input to L4 regions

    @param   L2Overrides (dict)
             Parameters to override in the L2 region

    @param   L4Overrides
             Parameters to override in the L4 region

    @param   numLearningPoints (int)
             Number of times each pair should be seen to be learnt

    """
    registerAllResearchRegions()
    self.name = name

    self.numLearningPoints = numLearningPoints
    self.numColumns = numCorticalColumns
    self.inputSize = inputSize
    self.externalInputSize = externalInputSize
    self.numInputBits = numInputBits

    # seed
    self.seed = seed
    random.seed(seed)

    # update parameters with overrides
    self.config = {
      "networkType": "MultipleL4L2Columns",
      "numCorticalColumns": numCorticalColumns,
      "externalInputSize": externalInputSize,
      "sensorInputSize": inputSize,
      "L4Params": self.getDefaultL4Params(inputSize),
      "L2Params": self.getDefaultL2Params(inputSize),
    }

    if L2Overrides is not None:
      self.config["L2Params"].update(L2Overrides)

    if L4Overrides is not None:
      self.config["L4Params"].update(L4Overrides)

    # create network
    self.network = createNetwork(self.config)

    self.sensorInputs = []
    self.externalInputs = []
    self.L4Columns = []
    self.L2Columns = []

    for i in xrange(self.numColumns):
      self.sensorInputs.append(
        self.network.regions["sensorInput_" + str(i)].getSelf()
      )
      self.externalInputs.append(
        self.network.regions["externalInput_" + str(i)].getSelf()
      )
      self.L4Columns.append(
        self.network.regions["L4Column_" + str(i)].getSelf()
      )
      self.L2Columns.append(
        self.network.regions["L2Column_" + str(i)].getSelf()
      )

    # will be populated during training
    self.objectL2Representations = {}
    self.statistics = []

    if not os.path.exists(self.PLOT_DIRECTORY):
      os.makedirs(self.PLOT_DIRECTORY)
コード例 #26
0
    def __init__(
        self,
        name,
        numCorticalColumns=1,
        inputSize=1024,
        numInputBits=20,
        externalInputSize=1024,
        numExternalInputBits=20,
        L2Overrides=None,
        L2RegionType="py.ColumnPoolerRegion",
        L4RegionType="py.ApicalTMPairRegion",
        networkType="MultipleL4L2Columns",
        implementation=None,
        longDistanceConnections=0,
        maxConnectionDistance=1,
        columnPositions=None,
        L4Overrides=None,
        numLearningPoints=3,
        seed=42,
        logCalls=False,
        enableLateralSP=False,
        lateralSPOverrides=None,
        enableFeedForwardSP=False,
        feedForwardSPOverrides=None,
        objectNamesAreIndices=False,
        enableFeedback=True,
        maxSegmentsPerCell=10,
    ):
        """
    Creates the network.

    Parameters:
    ----------------------------
    @param   name (str)
             Experiment name

    @param   numCorticalColumns (int)
             Number of cortical columns in the network

    @param   inputSize (int)
             Size of the sensory input

    @param   numInputBits (int)
             Number of ON bits in the generated input patterns

    @param   externalInputSize (int)
             Size of the lateral input to L4 regions

    @param   numExternalInputBits (int)
             Number of ON bits in the external input patterns

    @param   L2Overrides (dict)
             Parameters to override in the L2 region

    @param   L2RegionType (string)
             The type of region to use for L2

    @param   L4RegionType (string)
             The type of region to use for L4

    @param   networkType (string)
             Which type of L2L4 network to create.  If topology is being used,
             it should be specified here.  Possible values for this parameter
             are "MultipleL4L2Columns", "MultipleL4L2ColumnsWithTopology" and
             "L4L2Column"

    @param  longDistanceConnections (float)
             The probability that a column will randomly connect to a distant
             column.  Should be in [0, 1).  Only relevant when using multiple
             columns with topology.

    @param   L4Overrides (dict)
             Parameters to override in the L4 region

    @param   numLearningPoints (int)
             Number of times each pair should be seen to be learnt

    @param   logCalls (bool)
             If true, calls to main functions will be logged internally. The
             log can then be saved with saveLogs(). This allows us to recreate
             the complete network behavior using rerunExperimentFromLogfile
             which is very useful for debugging.

    @param   enableLateralSP (bool)
             If true, Spatial Pooler will be added between external input and
             L4 lateral input

    @param   lateralSPOverrides
             Parameters to override in the lateral SP region

    @param   enableFeedForwardSP (bool)
             If true, Spatial Pooler will be added between external input and
             L4 feed-forward input

    @param   feedForwardSPOverrides
             Parameters to override in the feed-forward SP region

    @param   objectNamesAreIndices (bool)
             If True, object names are used as indices in the
             getCurrentObjectOverlaps method. Object names must be positive
             integers. If False, object names can be strings, and indices will
             be assigned to each object name.

    @param   enableFeedback (bool)
             If True, enable feedback between L2 and L4

    """
        # Handle logging - this has to be done first
        self.logCalls = logCalls

        registerAllResearchRegions()
        self.name = name

        self.numLearningPoints = numLearningPoints
        self.numColumns = numCorticalColumns
        self.inputSize = inputSize
        self.externalInputSize = externalInputSize
        self.numInputBits = numInputBits
        self.objectNamesAreIndices = objectNamesAreIndices

        # seed
        self.seed = seed
        random.seed(seed)

        # update parameters with overrides
        if implementation is None:
            self.config = {
                "networkType":
                networkType,
                "longDistanceConnections":
                longDistanceConnections,
                "enableFeedback":
                enableFeedback,
                "numCorticalColumns":
                numCorticalColumns,
                "externalInputSize":
                externalInputSize,
                "sensorInputSize":
                inputSize,
                "L2RegionType":
                L2RegionType,
                "L4RegionType":
                L4RegionType,
                "L4Params":
                self.getDefaultL4Params(inputSize, numExternalInputBits),
                "L2Params":
                self.getDefaultL2Params(inputSize, numInputBits),
            }

        else:
            if "Bayesian" in implementation:
                self.config = {
                    "networkType":
                    networkType,
                    "longDistanceConnections":
                    longDistanceConnections,
                    "enableFeedback":
                    enableFeedback,
                    "numCorticalColumns":
                    numCorticalColumns,
                    "externalInputSize":
                    externalInputSize,
                    "sensorInputSize":
                    inputSize,
                    "L2RegionType":
                    L2RegionType,
                    "L4RegionType":
                    L4RegionType,
                    "L4Params":
                    self.getBayesianL4Params(inputSize, numExternalInputBits),
                    "L2Params":
                    self.getBayesianL2Params(inputSize, numInputBits),
                }
                self.config["L4Params"][
                    "maxSegmentsPerCell"] = maxSegmentsPerCell
                self.config["L4Params"]["implementation"] = implementation
                self.config["L2Params"]["implementation"] = implementation

        if enableLateralSP:
            self.config["lateralSPParams"] = self.getDefaultLateralSPParams(
                inputSize)
            if lateralSPOverrides:
                self.config["lateralSPParams"].update(lateralSPOverrides)

        if enableFeedForwardSP:
            self.config[
                "feedForwardSPParams"] = self.getDefaultFeedForwardSPParams(
                    inputSize)
            if feedForwardSPOverrides:
                self.config["feedForwardSPParams"].update(
                    feedForwardSPOverrides)

        if "Topology" in self.config["networkType"]:
            self.config["maxConnectionDistance"] = maxConnectionDistance

            # Generate a grid for cortical columns.  Will attempt to generate a full
            # square grid, and cut out positions starting from the bottom-right if the
            # number of cortical columns is not a perfect square.
            if columnPositions is None:
                columnPositions = []
                side_length = int(np.ceil(np.sqrt(numCorticalColumns)))
                for i in range(side_length):
                    for j in range(side_length):
                        columnPositions.append((i, j))
            self.config[
                "columnPositions"] = columnPositions[:numCorticalColumns]
            self.config["longDistanceConnections"] = longDistanceConnections

        if L2Overrides is not None:
            self.config["L2Params"].update(L2Overrides)

        if L4Overrides is not None:
            self.config["L4Params"].update(L4Overrides)

        # create network
        self.network = createNetwork(self.config)
        self.sensorInputs = []
        self.externalInputs = []
        self.L4Regions = []
        self.L2Regions = []

        for i in xrange(self.numColumns):
            self.sensorInputs.append(self.network.regions["sensorInput_" +
                                                          str(i)].getSelf())
            self.externalInputs.append(self.network.regions["externalInput_" +
                                                            str(i)].getSelf())
            self.L4Regions.append(self.network.regions["L4Column_" + str(i)])
            self.L2Regions.append(self.network.regions["L2Column_" + str(i)])

        self.L4Columns = [region.getSelf() for region in self.L4Regions]
        self.L2Columns = [region.getSelf() for region in self.L2Regions]

        # will be populated during training
        self.objectL2Representations = {}
        self.objectL2RepresentationsMatrices = [
            SparseMatrix(0, self.config["L2Params"]["cellCount"])
            for _ in xrange(self.numColumns)
        ]
        self.objectNameToIndex = {}
        self.statistics = []
コード例 #27
0
  def __init__(self,
               name,
               numCorticalColumns=1,
               inputSize=1024,
               numInputBits=20,
               externalInputSize=1024,
               numExternalInputBits=20,
               L2Overrides=None,
               L4RegionType="py.ExtendedTMRegion",
               L4Overrides=None,
               numLearningPoints=3,
               seed=42,
               logCalls=False,
               enableLateralSP=False,
               lateralSPOverrides=None,
               enableFeedForwardSP=False,
               feedForwardSPOverrides=None,
               objectNamesAreIndices=False
               ):
    """
    Creates the network.

    Parameters:
    ----------------------------
    @param   name (str)
             Experiment name

    @param   numCorticalColumns (int)
             Number of cortical columns in the network

    @param   inputSize (int)
             Size of the sensory input

    @param   numInputBits (int)
             Number of ON bits in the generated input patterns

    @param   externalInputSize (int)
             Size of the lateral input to L4 regions

    @param   numExternalInputBits (int)
             Number of ON bits in the external input patterns

    @param   L2Overrides (dict)
             Parameters to override in the L2 region

    @param   L4RegionType (string)
             The type of region to use for L4

    @param   L4Overrides (dict)
             Parameters to override in the L4 region

    @param   numLearningPoints (int)
             Number of times each pair should be seen to be learnt

    @param   logCalls (bool)
             If true, calls to main functions will be logged internally. The
             log can then be saved with saveLogs(). This allows us to recreate
             the complete network behavior using rerunExperimentFromLogfile
             which is very useful for debugging.

    @param   enableLateralSP (bool)
             If true, Spatial Pooler will be added between external input and
             L4 lateral input

    @param   lateralSPOverrides
             Parameters to override in the lateral SP region

    @param   enableFeedForwardSP (bool)
             If true, Spatial Pooler will be added between external input and
             L4 feed-forward input

    @param   feedForwardSPOverrides
             Parameters to override in the feed-forward SP region

    @param   objectNamesAreIndices (bool)
             If True, object names are used as indices in the
             getCurrentObjectOverlaps method. Object names must be positive
             integers. If False, object names can be strings, and indices will
             be assigned to each object name.

    """
    # Handle logging - this has to be done first
    self.logCalls = logCalls

    registerAllResearchRegions()
    self.name = name

    self.numLearningPoints = numLearningPoints
    self.numColumns = numCorticalColumns
    self.inputSize = inputSize
    self.externalInputSize = externalInputSize
    self.numInputBits = numInputBits
    self.objectNamesAreIndices = objectNamesAreIndices

    # seed
    self.seed = seed
    random.seed(seed)

    # update parameters with overrides
    self.config = {
      "networkType": "MultipleL4L2Columns",
      "numCorticalColumns": numCorticalColumns,
      "externalInputSize": externalInputSize,
      "sensorInputSize": inputSize,
      "L4RegionType": L4RegionType,
      "L4Params": self.getDefaultL4Params(L4RegionType, inputSize,
                                          numExternalInputBits),
      "L2Params": self.getDefaultL2Params(inputSize, numInputBits),
    }

    if enableLateralSP:
      self.config["lateralSPParams"] = self.getDefaultLateralSPParams(inputSize)
      if lateralSPOverrides:
        self.config["lateralSPParams"].update(lateralSPOverrides)

    if enableFeedForwardSP:
      self.config["feedForwardSPParams"] = self.getDefaultFeedForwardSPParams(inputSize)
      if feedForwardSPOverrides:
        self.config["feedForwardSPParams"].update(feedForwardSPOverrides)

    if L2Overrides is not None:
      self.config["L2Params"].update(L2Overrides)

    if L4Overrides is not None:
      self.config["L4Params"].update(L4Overrides)

    # create network
    self.network = createNetwork(self.config)

    self.sensorInputs = []
    self.externalInputs = []
    self.L4Regions = []
    self.L2Regions = []

    for i in xrange(self.numColumns):
      self.sensorInputs.append(
        self.network.regions["sensorInput_" + str(i)].getSelf()
      )
      self.externalInputs.append(
        self.network.regions["externalInput_" + str(i)].getSelf()
      )
      self.L4Regions.append(
        self.network.regions["L4Column_" + str(i)]
      )
      self.L2Regions.append(
        self.network.regions["L2Column_" + str(i)]
      )

    self.L4Columns = [region.getSelf() for region in self.L4Regions]
    self.L2Columns = [region.getSelf() for region in self.L2Regions]

    # will be populated during training
    self.objectL2Representations = {}
    self.objectL2RepresentationsMatrices = [
      SparseMatrix(0, self.config["L2Params"]["cellCount"])
      for _ in xrange(self.numColumns)]
    self.objectNameToIndex = {}
    self.statistics = []
コード例 #28
0
    def testTwoColumnsL4L2DataFlow(self):
        """
    This test trains a network with a few (feature, location) pairs and checks
    the data flows correctly, and that each intermediate representation is
    correct.
    Indices 0 and 1 in variable names refer to cortical column number.
    """

        # Create a simple network to test the sensor
        net = createNetwork(networkConfig3)

        self.assertEqual(
            len(net.regions.keys()), 4 * 2,
            "Incorrect number of regions, expected {} but had {}".format(
                8 * 2, len(net.regions.keys())))

        # Get various regions
        externalInput0 = net.regions["externalInput_0"].getSelf()
        sensorInput0 = net.regions["sensorInput_0"].getSelf()
        L4Column0 = net.regions["L4Column_0"].getSelf()
        L2Column0 = net.regions["L2Column_0"].getSelf()

        externalInput1 = net.regions["externalInput_1"].getSelf()
        sensorInput1 = net.regions["sensorInput_1"].getSelf()
        L4Column1 = net.regions["L4Column_1"].getSelf()
        L2Column1 = net.regions["L2Column_1"].getSelf()

        # create a feature and location pool for column 0
        features0 = [self.generatePattern(1024, 20) for _ in xrange(2)]
        locations0 = [self.generatePattern(1024, 20) for _ in xrange(3)]

        # create a feature and location pool for column 1
        features1 = [self.generatePattern(1024, 20) for _ in xrange(2)]
        locations1 = [self.generatePattern(1024, 20) for _ in xrange(3)]

        # train with following pairs:
        # (F0, L0) (F1, L1) on object 1
        # (F0, L2) (F1, L1) on object 2

        # Object 1

        # start with an object A input to get L2 representations for object A
        sensorInput0.addDataToQueue(features0[0], 0, 0)
        externalInput0.addDataToQueue(locations0[0], 0, 0)
        sensorInput1.addDataToQueue(features1[0], 0, 0)
        externalInput1.addDataToQueue(locations1[0], 0, 0)
        net.run(1)

        # get L2 representation for object B
        L2RepresentationA0 = self.getCurrentL2Representation(L2Column0)
        L2RepresentationA1 = self.getCurrentL2Representation(L2Column1)
        self.assertEqual(len(L2RepresentationA0), 40)
        self.assertEqual(len(L2RepresentationA0), 40)

        for _ in xrange(3):
            sensorInput0.addDataToQueue(features0[0], 0, 0)
            externalInput0.addDataToQueue(locations0[0], 0, 0)
            sensorInput1.addDataToQueue(features1[0], 0, 0)
            externalInput1.addDataToQueue(locations1[0], 0, 0)
            net.run(1)

            # check L2
            self.assertEqual(self.getCurrentL2Representation(L2Column0),
                             L2RepresentationA0)
            # check L2
            self.assertEqual(self.getCurrentL2Representation(L2Column1),
                             L2RepresentationA1)
            sensorInput0.addDataToQueue(features0[1], 0, 0)
            externalInput0.addDataToQueue(locations0[1], 0, 0)
            sensorInput1.addDataToQueue(features1[1], 0, 0)
            externalInput1.addDataToQueue(locations1[1], 0, 0)
            net.run(1)

            # check L2
            self.assertEqual(self.getCurrentL2Representation(L2Column0),
                             L2RepresentationA0)
            self.assertEqual(self.getCurrentL2Representation(L2Column1),
                             L2RepresentationA1)

        # get L4 representations when they are stable
        sensorInput0.addDataToQueue(features0[0], 0, 0)
        externalInput0.addDataToQueue(locations0[0], 0, 0)
        sensorInput1.addDataToQueue(features1[0], 0, 0)
        externalInput1.addDataToQueue(locations1[0], 0, 0)
        net.run(1)

        L4Representation00_0 = self.getL4PredictedActiveCells(L4Column0)
        L4Representation00_1 = self.getL4PredictedActiveCells(L4Column1)
        self.assertEqual(len(L4Representation00_0), 20)
        self.assertEqual(len(L4Representation00_1), 20)

        # send reset signal
        sensorInput0.addResetToQueue(0)
        externalInput0.addResetToQueue(0)
        sensorInput1.addResetToQueue(0)
        externalInput1.addResetToQueue(0)
        net.run(1)

        # Object B

        # start with input to get L2 representations
        sensorInput0.addDataToQueue(features0[0], 0, 0)
        externalInput0.addDataToQueue(locations0[2], 0, 0)
        sensorInput1.addDataToQueue(features1[0], 0, 0)
        externalInput1.addDataToQueue(locations1[2], 0, 0)
        net.run(1)

        # get L2 representations for object B
        L2RepresentationB0 = self.getCurrentL2Representation(L2Column0)
        L2RepresentationB1 = self.getCurrentL2Representation(L2Column1)
        self.assertEqual(len(L2RepresentationB0), 40)
        self.assertEqual(len(L2RepresentationB1), 40)
        # check that it is very different from object A
        self.assertLessEqual(len(L2RepresentationA0 & L2RepresentationB0), 5)
        self.assertLessEqual(len(L2RepresentationA1 & L2RepresentationB1), 5)

        for _ in xrange(3):
            sensorInput0.addDataToQueue(features0[0], 0, 0)
            externalInput0.addDataToQueue(locations0[2], 0, 0)
            sensorInput1.addDataToQueue(features1[0], 0, 0)
            externalInput1.addDataToQueue(locations1[2], 0, 0)
            net.run(1)

            # check L2
            self.assertEqual(self.getCurrentL2Representation(L2Column0),
                             L2RepresentationB0)
            # check L2
            self.assertEqual(self.getCurrentL2Representation(L2Column1),
                             L2RepresentationB1)

            sensorInput0.addDataToQueue(features0[1], 0, 0)
            externalInput0.addDataToQueue(locations0[1], 0, 0)
            sensorInput1.addDataToQueue(features1[1], 0, 0)
            externalInput1.addDataToQueue(locations1[1], 0, 0)
            net.run(1)

            # check L2
            self.assertEqual(self.getCurrentL2Representation(L2Column0),
                             L2RepresentationB0)
            # check L2
            self.assertEqual(self.getCurrentL2Representation(L2Column1),
                             L2RepresentationB1)

        # get L4 representations when they are stable
        sensorInput0.addDataToQueue(features0[0], 0, 0)
        externalInput0.addDataToQueue(locations0[2], 0, 0)
        sensorInput1.addDataToQueue(features1[0], 0, 0)
        externalInput1.addDataToQueue(locations1[2], 0, 0)
        net.run(1)

        L4Representation02_0 = self.getL4PredictedActiveCells(L4Column0)
        L4Representation02_1 = self.getL4PredictedActiveCells(L4Column1)
        self.assertEqual(len(L4Representation02_0), 20)
        self.assertEqual(len(L4Representation02_1), 20)

        sensorInput0.addDataToQueue(features0[1], 0, 0)
        externalInput0.addDataToQueue(locations0[1], 0, 0)
        sensorInput1.addDataToQueue(features1[1], 0, 0)
        externalInput1.addDataToQueue(locations1[1], 0, 0)
        net.run(1)

        L4Representation11_0 = self.getL4PredictedActiveCells(L4Column0)
        L4Representation11_1 = self.getL4PredictedActiveCells(L4Column1)
        self.assertEqual(len(L4Representation11_0), 20)
        self.assertEqual(len(L4Representation11_1), 20)

        sensorInput0.addResetToQueue(0)
        externalInput0.addResetToQueue(0)
        sensorInput1.addResetToQueue(0)
        externalInput1.addResetToQueue(0)
        net.run(1)

        # check inference with each (feature, location) pair
        L2Column0.setParameter("learningMode", 0, False)
        L4Column0.setParameter("learn", 0, False)
        L2Column1.setParameter("learningMode", 0, False)
        L4Column1.setParameter("learn", 0, False)

        # (F0, L0)
        sensorInput0.addDataToQueue(features0[0], 0, 0)
        externalInput0.addDataToQueue(locations0[0], 0, 0)
        sensorInput1.addDataToQueue(features1[0], 0, 0)
        externalInput1.addDataToQueue(locations1[0], 0, 0)
        net.run(1)

        # check L2 representations, L4 representations, no bursting
        self.assertLessEqual(
            len(
                self.getCurrentL2Representation(L2Column0) -
                L2RepresentationA0), 5)
        self.assertGreaterEqual(
            len(
                self.getCurrentL2Representation(L2Column0)
                & L2RepresentationA0), 35)
        self.assertEqual(self.getL4PredictedActiveCells(L4Column0),
                         L4Representation00_0)
        self.assertEqual(len(self.getL4BurstingCells(L4Column0)), 0)

        # be a little tolerant on this test
        self.assertLessEqual(
            len(
                self.getCurrentL2Representation(L2Column1) -
                L2RepresentationA1), 5)
        self.assertGreaterEqual(
            len(
                self.getCurrentL2Representation(L2Column1)
                & L2RepresentationA1), 35)
        self.assertEqual(self.getL4PredictedActiveCells(L4Column1),
                         L4Representation00_1)
        self.assertEqual(len(self.getL4BurstingCells(L4Column1)), 0)

        sensorInput0.addResetToQueue(0)
        externalInput0.addResetToQueue(0)
        sensorInput1.addResetToQueue(0)
        externalInput1.addResetToQueue(0)
        net.run(1)

        # (F0, L2)
        # It is fed twice, for the ambiguous prediction test, because of the
        # one-off error in distal predictions
        # FIXME when this is changed in ColumnPooler
        sensorInput0.addDataToQueue(features0[0], 0, 0)
        externalInput0.addDataToQueue(locations0[2], 0, 0)
        sensorInput1.addDataToQueue(features1[0], 0, 0)
        externalInput1.addDataToQueue(locations1[2], 0, 0)

        sensorInput0.addDataToQueue(features0[0], 0, 0)
        externalInput0.addDataToQueue(locations0[2], 0, 0)
        sensorInput1.addDataToQueue(features1[0], 0, 0)
        externalInput1.addDataToQueue(locations1[2], 0, 0)
        net.run(2)

        # check L2 representation, L4 representation, no bursting
        self.assertEqual(self.getCurrentL2Representation(L2Column0),
                         L2RepresentationB0)
        self.assertEqual(self.getL4PredictedActiveCells(L4Column0),
                         L4Representation02_0)
        self.assertEqual(len(self.getL4BurstingCells(L4Column0)), 0)

        self.assertEqual(self.getCurrentL2Representation(L2Column1),
                         L2RepresentationB1)
        self.assertEqual(self.getL4PredictedActiveCells(L4Column1),
                         L4Representation02_1)
        self.assertEqual(len(self.getL4BurstingCells(L4Column1)), 0)

        # ambiguous pattern: (F1, L1)
        sensorInput0.addDataToQueue(features0[1], 0, 0)
        externalInput0.addDataToQueue(locations0[1], 0, 0)
        sensorInput1.addDataToQueue(features1[1], 0, 0)
        externalInput1.addDataToQueue(locations1[1], 0, 0)
        net.run(1)

        # check L2 representation, L4 representation, no bursting
        # as opposed to the previous test, the representation is not ambiguous
        self.assertEqual(self.getCurrentL2Representation(L2Column0),
                         L2RepresentationB0)
        self.assertEqual(self.getL4PredictedActiveCells(L4Column0),
                         L4Representation11_0)
        self.assertEqual(len(self.getL4BurstingCells(L4Column0)), 0)

        self.assertEqual(self.getCurrentL2Representation(L2Column1),
                         L2RepresentationB1)
        self.assertEqual(self.getL4PredictedActiveCells(L4Column1),
                         L4Representation11_1)
        self.assertEqual(len(self.getL4BurstingCells(L4Column1)), 0)

        # unknown signal
        sensorInput0.addDataToQueue(features0[1], 0, 0)
        externalInput0.addDataToQueue(locations0[2], 0, 0)
        sensorInput1.addDataToQueue(features1[1], 0, 0)
        externalInput1.addDataToQueue(locations1[2], 0, 0)
        net.run(1)

        # check bursting (representation in L2 should be like in a random SP)
        self.assertLessEqual(len(self.getL4PredictedActiveCells(L4Column0)), 3)
        self.assertGreaterEqual(len(self.getL4BurstingCells(L4Column0)),
                                20 * 7)
        self.assertLessEqual(len(self.getL4PredictedActiveCells(L4Column1)), 3)
        self.assertGreaterEqual(len(self.getL4BurstingCells(L4Column1)),
                                20 * 7)
コード例 #29
0
    def __init__(self,
                 name,
                 numCorticalColumns=1,
                 L2Overrides={},
                 L4Overrides={},
                 L5Overrides={},
                 L6Overrides={},
                 numLearningPoints=3,
                 seed=42,
                 logCalls=False):
        """
    Creates the network.

    Parameters:
    ----------------------------
    @param   name (str)
             Experiment name

    @param   numCorticalColumns (int)
             Number of cortical columns in the network

    @param   L2Overrides (dict)
             Parameters to override in the L2 region

    @param   L4Overrides (dict)
             Parameters to override in the L4 region

    @param   L5Overrides (dict)
             Parameters to override in the L5 region

    @param   L6Overrides (dict)
             Parameters to override in the L6 region

    @param   numLearningPoints (int)
             Number of times each pair should be seen to be learnt

    @param   logCalls (bool)
             If true, calls to main functions will be logged internally. The
             log can then be saved with saveLogs(). This allows us to recreate
             the complete network behavior using rerunExperimentFromLogfile
             which is very useful for debugging.
    """
        # Handle logging - this has to be done first
        self.logCalls = logCalls

        registerAllResearchRegions()
        self.name = name

        self.numLearningPoints = numLearningPoints
        self.numColumns = numCorticalColumns
        self.sensorInputSize = 2048
        self.numInputBits = 40

        # seed
        self.seed = seed
        random.seed(seed)

        # Get network parameters and update with overrides
        self.config = {
            "networkType": "L2456Columns",
            "numCorticalColumns": numCorticalColumns,
            "randomSeedBase": self.seed,
        }
        self.config.update(self.getDefaultParams())

        self.config["L2Params"].update(L2Overrides)
        self.config["L4Params"].update(L4Overrides)
        self.config["L5Params"].update(L5Overrides)
        self.config["L6Params"].update(L6Overrides)

        # create network and retrieve regions
        self.network = createNetwork(self.config)
        self._retrieveRegions()

        # will be populated during training
        self.objectRepresentationsL2 = {}
        self.objectRepresentationsL5 = {}
        self.statistics = []
コード例 #30
0
  def testDataFlowTM(self):
    """
    This test trains a network with two high order sequences and checks
    the data flows correctly, and that the TM learns them correctly.
    """

    # Create a simple network to test the sensor
    net = createNetwork(networkConfig1)

    # Get various regions
    externalInput = net.regions["externalInput_0"].getSelf()
    sensorInput = net.regions["sensorInput_0"].getSelf()
    L4Column = net.regions["L4Column_0"].getSelf()
    L2Column = net.regions["L2Column_0"].getSelf()
    TMColumn = net.regions["TMColumn_0"].getSelf()

    # create a feature and location pool
    features = [self.generatePattern(1024, 20) for _ in xrange(5)]

    # train with following sequences:
    # 1 : F0 F1 F2
    # 2 : F3 F1 F4

    # Sequence A, three repeats with a reset in between.  We add nothing for
    # location signal
    for _ in range(3):
      sensorInput.addDataToQueue(features[0], 0, 0)
      sensorInput.addDataToQueue(features[1], 0, 0)
      sensorInput.addDataToQueue(features[2], 0, 0)

      externalInput.addDataToQueue([], 0, 0)
      externalInput.addDataToQueue([], 0, 0)
      externalInput.addDataToQueue([], 0, 0)

      sensorInput.addResetToQueue(0)
      externalInput.addResetToQueue(0)

    net.run(4*3) # Includes reset

    # Sequence B, three repeats with a reset in between.
    for _ in range(3):
      sensorInput.addDataToQueue(features[3], 0, 0)
      sensorInput.addDataToQueue(features[1], 0, 0)
      sensorInput.addDataToQueue(features[4], 0, 0)

      externalInput.addDataToQueue([], 0, 0)
      externalInput.addDataToQueue([], 0, 0)
      externalInput.addDataToQueue([], 0, 0)

      sensorInput.addResetToQueue(0)
      externalInput.addResetToQueue(0)

    net.run(4*3) # Includes reset


    # check inference
    L2Column.setParameter("learningMode", 0, False)
    L4Column.setParameter("learn", 0, False)
    TMColumn.setParameter("learn", 0, False)

    # Sequence A with a reset in between.
    sensorInput.addDataToQueue(features[0], 0, 0)
    externalInput.addDataToQueue([], 0, 0)
    net.run(1)
    self.assertEqual(len(self.getPredictedActiveCells(TMColumn)), 0)
    self.assertEqual(len(self.getActiveCells(TMColumn)),
                     TMColumn.cellsPerColumn*20)

    sensorInput.addDataToQueue(features[1], 0, 0)
    externalInput.addDataToQueue([], 0, 0)
    net.run(1)
    self.assertEqual(len(self.getPredictedActiveCells(TMColumn)), 20)
    self.assertEqual(len(self.getActiveCells(TMColumn)), 20)
    predictedActiveCellsS1 = self.getPredictedActiveCells(TMColumn)

    sensorInput.addDataToQueue(features[2], 0, 0)
    externalInput.addDataToQueue([], 0, 0)
    net.run(1)
    self.assertEqual(len(self.getPredictedActiveCells(TMColumn)), 20)
    self.assertEqual(len(self.getActiveCells(TMColumn)), 20)

    sensorInput.addResetToQueue(0)
    externalInput.addResetToQueue(0)
    net.run(1)

    # Sequence B with a reset
    sensorInput.addDataToQueue(features[3], 0, 0)
    externalInput.addDataToQueue([], 0, 0)
    net.run(1)
    self.assertEqual(len(self.getPredictedActiveCells(TMColumn)), 0)
    self.assertEqual(len(self.getActiveCells(TMColumn)),
                     TMColumn.cellsPerColumn*20)

    sensorInput.addDataToQueue(features[1], 0, 0)
    externalInput.addDataToQueue([], 0, 0)
    net.run(1)
    self.assertEqual(len(self.getPredictedActiveCells(TMColumn)), 20)
    self.assertEqual(len(self.getActiveCells(TMColumn)), 20)
    predictedActiveCellsS2 = self.getPredictedActiveCells(TMColumn)

    sensorInput.addDataToQueue(features[4], 0, 0)
    externalInput.addDataToQueue([], 0, 0)
    net.run(1)
    self.assertEqual(len(self.getPredictedActiveCells(TMColumn)), 20)
    self.assertEqual(len(self.getActiveCells(TMColumn)), 20)

    # Ensure representation for ambiguous element is different
    self.assertFalse(predictedActiveCellsS1 == predictedActiveCellsS2)
コード例 #31
0
    def __init__(self,
                 name,
                 numCorticalColumns=1,
                 inputSize=1024,
                 numInputBits=20,
                 externalInputSize=1024,
                 L2Overrides=None,
                 L4Overrides=None,
                 numLearningPoints=3,
                 seed=42,
                 logCalls=False):
        """
    Creates the network.

    Parameters:
    ----------------------------
    @param   name (str)
             Experiment name

    @param   numCorticalColumns (int)
             Number of cortical columns in the network

    @param   inputSize (int)
             Size of the sensory input

    @param   numInputBits (int)
             Number of ON bits in the generated input patterns

    @param   externalInputSize (int)
             Size of the lateral input to L4 regions

    @param   L2Overrides (dict)
             Parameters to override in the L2 region

    @param   L4Overrides
             Parameters to override in the L4 region

    @param   numLearningPoints (int)
             Number of times each pair should be seen to be learnt

    @param   logCalls (bool)
             If true, calls to main functions will be logged internally. The
             log can then be saved with saveLogs(). This allows us to recreate
             the complete network behavior using rerunExperimentFromLogfile
             which is very useful for debugging.

    """
        # Handle logging - this has to be done first
        self.callLog = []
        self.logCalls = logCalls
        if self.logCalls:
            frame = inspect.currentframe()
            args, _, _, values = inspect.getargvalues(frame)
            values.pop('frame')
            values.pop('self')
            self.callLog.append([inspect.getframeinfo(frame)[2], values])

        registerAllResearchRegions()
        self.name = name

        self.numLearningPoints = numLearningPoints
        self.numColumns = numCorticalColumns
        self.inputSize = inputSize
        self.externalInputSize = externalInputSize
        self.numInputBits = numInputBits

        # seed
        self.seed = seed
        random.seed(seed)

        # update parameters with overrides
        self.config = {
            "networkType": "MultipleL4L2Columns",
            "numCorticalColumns": numCorticalColumns,
            "externalInputSize": externalInputSize,
            "sensorInputSize": inputSize,
            "L4Params": self.getDefaultL4Params(inputSize),
            "L2Params": self.getDefaultL2Params(inputSize),
        }

        if L2Overrides is not None:
            self.config["L2Params"].update(L2Overrides)

        if L4Overrides is not None:
            self.config["L4Params"].update(L4Overrides)

        # create network
        self.network = createNetwork(self.config)

        self.sensorInputs = []
        self.externalInputs = []
        self.L4Columns = []
        self.L2Columns = []

        for i in xrange(self.numColumns):
            self.sensorInputs.append(self.network.regions["sensorInput_" +
                                                          str(i)].getSelf())
            self.externalInputs.append(self.network.regions["externalInput_" +
                                                            str(i)].getSelf())
            self.L4Columns.append(self.network.regions["L4Column_" +
                                                       str(i)].getSelf())
            self.L2Columns.append(self.network.regions["L2Column_" +
                                                       str(i)].getSelf())

        # will be populated during training
        self.objectL2Representations = {}
        self.statistics = []

        if not os.path.exists(self.PLOT_DIRECTORY):
            os.makedirs(self.PLOT_DIRECTORY)
コード例 #32
0
    def testCustomParameters(self):
        """
    This test creates a network with custom parameters and tests that the
    network gets correctly constructed.
    """
        customConfig = {
            "networkType": "L4L2Column",
            "externalInputSize": 256,
            "sensorInputSize": 512,
            "L4RegionType": "py.ApicalTMPairRegion",
            "L4Params": {
                "columnCount": 512,
                "cellsPerColumn": 16,
                "learn": True,
                "learnOnOneCell": False,
                "initialPermanence": 0.23,
                "connectedPermanence": 0.75,
                "permanenceIncrement": 0.45,
                "permanenceDecrement": 0.1,
                "minThreshold": 15,
                "basalPredictedSegmentDecrement": 0.21,
                "activationThreshold": 16,
                "sampleSize": 24,
            },
            "L2Params": {
                "inputWidth": 512 * 8,
                "cellCount": 2048,
                "sdrSize": 30,
                "synPermProximalInc": 0.12,
                "synPermProximalDec": 0.011,
                "initialProximalPermanence": 0.8,
                "minThresholdProximal": 8,
                "sampleSizeProximal": 17,
                "connectedPermanenceProximal": 0.6,
                "synPermDistalInc": 0.09,
                "synPermDistalDec": 0.002,
                "initialDistalPermanence": 0.52,
                "activationThresholdDistal": 15,
                "sampleSizeDistal": 25,
                "connectedPermanenceDistal": 0.6,
                "distalSegmentInhibitionFactor": 0.8333,
                "learningMode": True,
            },
        }

        net = createNetwork(customConfig)

        self.assertEqual(len(net.regions.keys()), 4,
                         "Incorrect number of regions")

        # Get various regions
        externalInput = net.regions["externalInput_0"].getSelf()
        sensorInput = net.regions["sensorInput_0"].getSelf()
        L4Column = net.regions["L4Column_0"].getSelf()
        L2Column = net.regions["L2Column_0"].getSelf()

        # we need to do a first compute for the various elements to be constructed
        sensorInput.addDataToQueue([], 0, 0)
        externalInput.addDataToQueue([], 0, 0)
        net.run(1)

        # check that parameters are correct in L4
        for param, value in customConfig["L4Params"].iteritems():
            self.assertEqual(L4Column.getParameter(param), value)

        # check that parameters are correct in L2
        # some parameters are in the tm members
        for param, value in customConfig["L2Params"].iteritems():
            self.assertEqual(L2Column.getParameter(param), value)

        # check that parameters are correct in L2
        self.assertEqual(externalInput.outputWidth,
                         customConfig["externalInputSize"])
        self.assertEqual(sensorInput.outputWidth,
                         customConfig["sensorInputSize"])
コード例 #33
0
    def testMultipleL4L2ColumnsWithTopologyLinks(self):
        """
    In this simplistic test we create a network with 5 L4L2Columns and
    topological lateral connections, and ensure that it has the correct links
    between regions.  The network is laid out as follows:

            3
            |
    0---1---2
            |
            4
    """

        net = createNetwork(networkConfig4)
        links = net.getLinks()

        # These are all the links we're hoping to find
        desired_links = set([
            "sensorInput_0.dataOut-->L4Column_0.activeColumns",
            "L2Column_0.feedForwardOutput-->L4Column_0.apicalInput",
            "externalInput_0.dataOut-->L4Column_0.basalInput",
            "L4Column_0.predictedActiveCells-->" +
            "L2Column_0.feedforwardGrowthCandidates",
            "L4Column_0.activeCells-->L2Column_0.feedforwardInput",
            "sensorInput_0.resetOut-->L2Column_0.resetIn",
            "sensorInput_0.resetOut-->L4Column_0.resetIn",
            "sensorInput_1.dataOut-->L4Column_1.activeColumns",
            "L2Column_1.feedForwardOutput-->L4Column_1.apicalInput",
            "externalInput_1.dataOut-->L4Column_1.basalInput",
            "L4Column_1.predictedActiveCells-->" +
            "L2Column_1.feedforwardGrowthCandidates",
            "L4Column_1.activeCells-->L2Column_1.feedforwardInput",
            "sensorInput_1.resetOut-->L2Column_1.resetIn",
            "sensorInput_1.resetOut-->L4Column_1.resetIn",
            "sensorInput_2.dataOut-->L4Column_2.activeColumns",
            "L2Column_2.feedForwardOutput-->L4Column_2.apicalInput",
            "externalInput_2.dataOut-->L4Column_2.basalInput",
            "L4Column_2.predictedActiveCells-->" +
            "L2Column_2.feedforwardGrowthCandidates",
            "L4Column_2.activeCells-->L2Column_2.feedforwardInput",
            "sensorInput_2.resetOut-->L2Column_2.resetIn",
            "sensorInput_2.resetOut-->L4Column_2.resetIn",
            "sensorInput_3.dataOut-->L4Column_3.activeColumns",
            "L2Column_3.feedForwardOutput-->L4Column_3.apicalInput",
            "externalInput_3.dataOut-->L4Column_3.basalInput",
            "L4Column_3.predictedActiveCells-->" +
            "L2Column_3.feedforwardGrowthCandidates",
            "L4Column_3.activeCells-->L2Column_3.feedforwardInput",
            "sensorInput_3.resetOut-->L2Column_3.resetIn",
            "sensorInput_3.resetOut-->L4Column_3.resetIn",
            "sensorInput_4.dataOut-->L4Column_4.activeColumns",
            "L2Column_4.feedForwardOutput-->L4Column_4.apicalInput",
            "externalInput_4.dataOut-->L4Column_4.basalInput",
            "L4Column_4.predictedActiveCells-->" +
            "L2Column_4.feedforwardGrowthCandidates",
            "L4Column_4.activeCells-->L2Column_4.feedforwardInput",
            "sensorInput_4.resetOut-->L2Column_4.resetIn",
            "sensorInput_4.resetOut-->L4Column_4.resetIn",
            "L2Column_0.feedForwardOutput-->L2Column_1.lateralInput",
            "L2Column_1.feedForwardOutput-->L2Column_0.lateralInput",
            "L2Column_1.feedForwardOutput-->L2Column_2.lateralInput",
            "L2Column_2.feedForwardOutput-->L2Column_1.lateralInput",
            "L2Column_2.feedForwardOutput-->L2Column_3.lateralInput",
            "L2Column_2.feedForwardOutput-->L2Column_4.lateralInput",
            "L2Column_3.feedForwardOutput-->L2Column_2.lateralInput",
            "L2Column_4.feedForwardOutput-->L2Column_2.lateralInput",
            "externalInput_0.dataOut-->L4Column_0.basalGrowthCandidates",
            "externalInput_1.dataOut-->L4Column_1.basalGrowthCandidates",
            "externalInput_2.dataOut-->L4Column_2.basalGrowthCandidates",
            "externalInput_3.dataOut-->L4Column_3.basalGrowthCandidates",
            "externalInput_4.dataOut-->L4Column_4.basalGrowthCandidates"
        ])

        # This gets textual representations of the links.
        links = set([link.second.getMoniker() for link in links])

        # Build a descriptive error message to pass to the user
        error_message = "Links incorrectly formed in multicolumn L2L4 network: \n"
        for link in desired_links:
            if not link in links:
                error_message += "Failed to find link: {}\n".format(link)

        for link in links:
            if not link in desired_links:
                error_message += "Found unexpected link: {}\n".format(link)

        self.assertSetEqual(desired_links, links, error_message)
コード例 #34
0
  def __init__(self,
               name,
               numCorticalColumns=1,
               inputSize=1024,
               numInputBits=20,
               externalInputSize=1024,
               L2Overrides=None,
               L4Overrides=None,
               numLearningPoints=3,
               seed=42,
               logCalls = False,
               enableLateralSP=False,
               lateralSPOverrides=None,
               enableFeedForwardSP=False,
               feedForwardSPOverrides=None
               ):
    """
    Creates the network.

    Parameters:
    ----------------------------
    @param   name (str)
             Experiment name

    @param   numCorticalColumns (int)
             Number of cortical columns in the network

    @param   inputSize (int)
             Size of the sensory input

    @param   numInputBits (int)
             Number of ON bits in the generated input patterns

    @param   externalInputSize (int)
             Size of the lateral input to L4 regions

    @param   L2Overrides (dict)
             Parameters to override in the L2 region

    @param   L4Overrides
             Parameters to override in the L4 region

    @param   numLearningPoints (int)
             Number of times each pair should be seen to be learnt

    @param   logCalls (bool)
             If true, calls to main functions will be logged internally. The
             log can then be saved with saveLogs(). This allows us to recreate
             the complete network behavior using rerunExperimentFromLogfile
             which is very useful for debugging.

    @param   enableLateralSP (bool)
             If true, Spatial Pooler will be added between external input and
             L4 lateral input

    @param   lateralSPOverrides
             Parameters to override in the lateral SP region

    @param   enableFeedForwardSP (bool)
             If true, Spatial Pooler will be added between external input and
             L4 feed-forward input

    @param   feedForwardSPOverrides
             Parameters to override in the feed-forward SP region

    """
    # Handle logging - this has to be done first
    self.callLog = []
    self.logCalls = logCalls
    if self.logCalls:
      frame = inspect.currentframe()
      args, _, _, values = inspect.getargvalues(frame)
      values.pop('frame')
      values.pop('self')
      self.callLog.append([inspect.getframeinfo(frame)[2], values])

    registerAllResearchRegions()
    self.name = name

    self.numLearningPoints = numLearningPoints
    self.numColumns = numCorticalColumns
    self.inputSize = inputSize
    self.externalInputSize = externalInputSize
    self.numInputBits = numInputBits

    # seed
    self.seed = seed
    random.seed(seed)

    # update parameters with overrides
    self.config = {
      "networkType": "MultipleL4L2Columns",
      "numCorticalColumns": numCorticalColumns,
      "externalInputSize": externalInputSize,
      "sensorInputSize": inputSize,
      "L4Params": self.getDefaultL4Params(inputSize),
      "L2Params": self.getDefaultL2Params(inputSize),
    }

    if enableLateralSP:
      self.config["lateralSPParams"] = self.getDefaultLateralSPParams(inputSize)
      if lateralSPOverrides:
        self.config["lateralSPParams"].update(lateralSPOverrides)

    if enableFeedForwardSP:
      self.config["feedForwardSPParams"] = self.getDefaultFeedForwardSPParams(inputSize)
      if feedForwardSPOverrides:
        self.config["feedForwardSPParams"].update(feedForwardSPOverrides)

    if L2Overrides is not None:
      self.config["L2Params"].update(L2Overrides)

    if L4Overrides is not None:
      self.config["L4Params"].update(L4Overrides)

    # create network
    self.network = createNetwork(self.config)

    self.sensorInputs = []
    self.externalInputs = []
    self.L4Columns = []
    self.L2Columns = []

    for i in xrange(self.numColumns):
      self.sensorInputs.append(
        self.network.regions["sensorInput_" + str(i)].getSelf()
      )
      self.externalInputs.append(
        self.network.regions["externalInput_" + str(i)].getSelf()
      )
      self.L4Columns.append(
        self.network.regions["L4Column_" + str(i)].getSelf()
      )
      self.L2Columns.append(
        self.network.regions["L2Column_" + str(i)].getSelf()
      )

    # will be populated during training
    self.objectL2Representations = {}
    self.statistics = []

    if not os.path.exists(self.PLOT_DIRECTORY):
      os.makedirs(self.PLOT_DIRECTORY)
コード例 #35
0
    def testSingleColumnL4L2DataFlow(self):
        """
    This test trains a network with a few (feature, location) pairs and checks
    the data flows correctly, and that each intermediate representation is
    correct.
    """

        # Create a simple network to test the sensor
        net = createNetwork(networkConfig1)

        self.assertEqual(len(net.regions.keys()), 4,
                         "Incorrect number of regions")

        # Get various regions
        externalInput = net.regions["externalInput_0"].getSelf()
        sensorInput = net.regions["sensorInput_0"].getSelf()
        L4Column = net.regions["L4Column_0"].getSelf()
        L2Column = net.regions["L2Column_0"].getSelf()

        # create a feature and location pool
        features = [self.generatePattern(1024, 20) for _ in xrange(2)]
        locations = [self.generatePattern(1024, 20) for _ in xrange(3)]

        # train with following pairs:
        # (F0, L0) (F1, L1) on object A
        # (F0, L2) (F1, L1) on object B

        # Object A

        # start with an object 1 input to get L2 representation for object 1
        sensorInput.addDataToQueue(features[0], 0, 0)
        externalInput.addDataToQueue(locations[0], 0, 0)
        net.run(1)

        # get L2 representation for object A
        L2RepresentationA = self.getCurrentL2Representation(L2Column)
        self.assertEqual(len(L2RepresentationA), 40)

        for _ in xrange(4):
            sensorInput.addDataToQueue(features[0], 0, 0)
            externalInput.addDataToQueue(locations[0], 0, 0)
            net.run(1)

            # check L2
            self.assertEqual(self.getCurrentL2Representation(L2Column),
                             L2RepresentationA)
            sensorInput.addDataToQueue(features[1], 0, 0)
            externalInput.addDataToQueue(locations[1], 0, 0)
            net.run(1)

            # check L2
            self.assertEqual(self.getCurrentL2Representation(L2Column),
                             L2RepresentationA)

        # get L4 representations when they are stable
        sensorInput.addDataToQueue(features[0], 0, 0)
        externalInput.addDataToQueue(locations[0], 0, 0)
        net.run(1)

        L4Representation00 = self.getL4PredictedActiveCells(L4Column)
        self.assertEqual(len(L4Representation00), 20)

        # send reset signal
        sensorInput.addResetToQueue(0)
        externalInput.addResetToQueue(0)
        net.run(1)

        # Object B

        # start with empty input
        sensorInput.addDataToQueue(features[0], 0, 0)
        externalInput.addDataToQueue(locations[2], 0, 0)
        net.run(1)

        # get L2 representation for object B
        L2RepresentationB = self.getCurrentL2Representation(L2Column)
        self.assertEqual(len(L2RepresentationB), 40)
        # check that it is very different from object A
        self.assertLessEqual(len(L2RepresentationA & L2RepresentationB), 5)

        for _ in xrange(4):
            sensorInput.addDataToQueue(features[0], 0, 0)
            externalInput.addDataToQueue(locations[2], 0, 0)
            net.run(1)

            # check L2
            self.assertEqual(self.getCurrentL2Representation(L2Column),
                             L2RepresentationB)

            sensorInput.addDataToQueue(features[1], 0, 0)
            externalInput.addDataToQueue(locations[1], 0, 0)
            net.run(1)

            # check L2
            self.assertEqual(self.getCurrentL2Representation(L2Column),
                             L2RepresentationB)

        # get L4 representations when they are stable
        sensorInput.addDataToQueue(features[0], 0, 0)
        externalInput.addDataToQueue(locations[2], 0, 0)
        net.run(1)

        L4Representation02 = self.getL4PredictedActiveCells(L4Column)
        self.assertEqual(len(L4Representation02), 20)

        sensorInput.addDataToQueue(features[1], 0, 0)
        externalInput.addDataToQueue(locations[1], 0, 0)
        net.run(1)

        L4Representation11 = self.getL4PredictedActiveCells(L4Column)
        self.assertEqual(len(L4Representation11), 20)

        # send reset signal
        sensorInput.addResetToQueue(0)
        externalInput.addResetToQueue(0)
        net.run(1)

        # check inference with each (feature, location) pair
        L2Column.setParameter("learningMode", 0, False)
        L4Column.setParameter("learn", 0, False)

        # (F0, L0)
        sensorInput.addDataToQueue(features[0], 0, 0)
        externalInput.addDataToQueue(locations[0], 0, 0)
        net.run(1)

        # check L2 representation, L4 representation, no bursting
        self.assertEqual(self.getCurrentL2Representation(L2Column),
                         L2RepresentationA)
        self.assertEqual(self.getL4PredictedActiveCells(L4Column),
                         L4Representation00)
        self.assertEqual(len(self.getL4BurstingCells(L4Column)), 0)

        # send reset signal
        sensorInput.addResetToQueue(0)
        externalInput.addResetToQueue(0)
        net.run(1)

        # (F0, L2)
        sensorInput.addDataToQueue(features[0], 0, 0)
        externalInput.addDataToQueue(locations[2], 0, 0)
        net.run(1)

        # check L2 representation, L4 representation, no bursting
        self.assertEqual(self.getCurrentL2Representation(L2Column),
                         L2RepresentationB)
        self.assertEqual(self.getL4PredictedActiveCells(L4Column),
                         L4Representation02)
        self.assertEqual(len(self.getL4BurstingCells(L4Column)), 0)

        # send reset signal
        sensorInput.addResetToQueue(0)
        externalInput.addResetToQueue(0)
        net.run(1)

        # (F1, L1)
        sensorInput.addDataToQueue(features[1], 0, 0)
        externalInput.addDataToQueue(locations[1], 0, 0)
        net.run(1)

        # check L2 representation, L4 representation, no bursting
        self.assertEqual(self.getCurrentL2Representation(L2Column),
                         L2RepresentationA | L2RepresentationB)
        self.assertEqual(self.getL4PredictedActiveCells(L4Column),
                         L4Representation11)
        self.assertEqual(len(self.getL4BurstingCells(L4Column)), 0)
        sensorInput.addDataToQueue(features[1], 0, 0)
        externalInput.addDataToQueue(locations[2], 0, 0)
        net.run(1)

        # check bursting (representation in L2 should be like in a random SP)
        self.assertEqual(len(self.getL4PredictedActiveCells(L4Column)), 0)
        self.assertEqual(len(self.getL4BurstingCells(L4Column)), 20 * 8)
コード例 #36
0
  def __init__(self,
               name,
               numCorticalColumns=1,
               inputSize=1024,
               numInputBits=20,
               externalInputSize=1024,
               numExternalInputBits=20,
               L2Overrides=None,
               L4Overrides=None,
               seed=42,
               logCalls=False,
               objectNamesAreIndices=False,
               ):
    """
    Creates the network.

    Parameters:
    ----------------------------
    @param   TMOverrides (dict)
             Parameters to override in the TM region
    """

    # Handle logging - this has to be done first
    self.logCalls = logCalls

    registerAllResearchRegions()
    self.name = name

    self.numLearningPoints = 1
    self.numColumns = numCorticalColumns
    self.inputSize = inputSize
    self.externalInputSize = externalInputSize
    self.numInputBits = numInputBits
    self.objectNamesAreIndices = objectNamesAreIndices
    self.numExternalInputBits = numExternalInputBits

    # seed
    self.seed = seed
    random.seed(seed)

    # Create default parameters and then update with overrides
    self.config = {
      "networkType": "CombinedSequenceColumn",
      "numCorticalColumns": numCorticalColumns,
      "externalInputSize": externalInputSize,
      "sensorInputSize": inputSize,
      "enableFeedback": False,
      "L2Params": self.getDefaultL2Params(inputSize, numInputBits),
    }
    self.config["L4Params"] = self._getDefaultCombinedL4Params(
      self.numInputBits, self.inputSize,
      self.numExternalInputBits, self.externalInputSize,
      self.config["L2Params"]["cellCount"])

    if L2Overrides is not None:
      self.config["L2Params"].update(L2Overrides)

    if L4Overrides is not None:
      self.config["L4Params"].update(L4Overrides)

    pprint.pprint(self.config)

    # Recreate network including TM parameters
    self.network = createNetwork(self.config)
    self.sensorInputs = []
    self.externalInputs = []
    self.L2Regions = []
    self.L4Regions = []

    for i in xrange(self.numColumns):
      self.sensorInputs.append(
        self.network.regions["sensorInput_" + str(i)].getSelf()
      )
      self.externalInputs.append(
        self.network.regions["externalInput_" + str(i)].getSelf()
      )
      self.L2Regions.append(
        self.network.regions["L2Column_" + str(i)]
      )
      self.L4Regions.append(
        self.network.regions["L4Column_" + str(i)]
      )

    self.L2Columns = [region.getSelf() for region in self.L2Regions]
    self.L4Columns = [region.getSelf() for region in self.L4Regions]

    # will be populated during training
    self.objectL2Representations = {}
    self.objectL2RepresentationsMatrices = [
      SparseMatrix(0, self.config["L2Params"]["cellCount"])
      for _ in xrange(self.numColumns)]
    self.objectNameToIndex = {}
    self.statistics = []
コード例 #37
0
    def __init__(
        self,
        name,
        numCorticalColumns=1,
        inputSize=1024,
        numInputBits=20,
        externalInputSize=1024,
        numExternalInputBits=20,
        L2Overrides=None,
        networkType="L4L2TMColumn",
        L4Overrides=None,
        seed=42,
        logCalls=False,
        objectNamesAreIndices=False,
        TMOverrides=None,
    ):
        """
    Creates the network.

    Parameters:
    ----------------------------
    @param   TMOverrides (dict)
             Parameters to override in the TM region
    """

        # Handle logging - this has to be done first
        self.logCalls = logCalls

        registerAllResearchRegions()
        self.name = name

        self.numLearningPoints = 1
        self.numColumns = numCorticalColumns
        self.inputSize = inputSize
        self.externalInputSize = externalInputSize
        self.numInputBits = numInputBits
        self.objectNamesAreIndices = objectNamesAreIndices

        # seed
        self.seed = seed
        random.seed(seed)

        # update parameters with overrides
        self.config = {
            "networkType": networkType,
            "numCorticalColumns": numCorticalColumns,
            "externalInputSize": externalInputSize,
            "sensorInputSize": inputSize,
            "enableFeedback": False,
            "L4Params": self.getDefaultL4Params(inputSize,
                                                numExternalInputBits),
            "L2Params": self.getDefaultL2Params(inputSize, numInputBits),
            "TMParams": self.getDefaultTMParams(self.inputSize,
                                                self.numInputBits),
        }

        if L2Overrides is not None:
            self.config["L2Params"].update(L2Overrides)

        if L4Overrides is not None:
            self.config["L4Params"].update(L4Overrides)

        if TMOverrides is not None:
            self.config["TMParams"].update(TMOverrides)

        # Recreate network including TM parameters
        self.network = createNetwork(self.config)
        self.sensorInputs = []
        self.externalInputs = []
        self.L4Regions = []
        self.L2Regions = []
        self.TMRegions = []

        for i in xrange(self.numColumns):
            self.sensorInputs.append(self.network.regions["sensorInput_" +
                                                          str(i)].getSelf())
            self.externalInputs.append(self.network.regions["externalInput_" +
                                                            str(i)].getSelf())
            self.L4Regions.append(self.network.regions["L4Column_" + str(i)])
            self.L2Regions.append(self.network.regions["L2Column_" + str(i)])
            self.TMRegions.append(self.network.regions["TMColumn_" + str(i)])

        self.L4Columns = [region.getSelf() for region in self.L4Regions]
        self.L2Columns = [region.getSelf() for region in self.L2Regions]
        self.TMColumns = [region.getSelf() for region in self.TMRegions]

        # will be populated during training
        self.objectL2Representations = {}
        self.objectL2RepresentationsMatrices = [
            SparseMatrix(0, self.config["L2Params"]["cellCount"])
            for _ in xrange(self.numColumns)
        ]
        self.objectNameToIndex = {}
        self.statistics = []

        # Create classifier to hold supposedly unique TM states
        self.classifier = KNNClassifier(distanceMethod="rawOverlap")
        self.numTMCells = (self.TMColumns[0].cellsPerColumn *
                           self.TMColumns[0].columnCount)
コード例 #38
0
  def __init__(self,
               numCorticalColumns=1,
               inputSize=2048,
               numInputBits=40,
               L2Overrides=None,
               L4Overrides=None,
               numLearningPasses=4,
               seed=42):
    """
    Creates the network and initialize the experiment.

    Parameters:
    ----------------------------
    @param   numCorticalColumns (int)
             Number of cortical columns in the network

    @param   inputSize (int)
             Size of the sensory input

    @param   numInputBits (int)
             Number of ON bits in the generated input patterns

    @param   L2Overrides (dict)
             Parameters to override in the L2 region

    @param   L4Overrides
             Parameters to override in the L4 region

    @param   numLearningPasses (int)
             Number of times each pair should be seen to be learnt
    """
    registerAllResearchRegions()

    self.numLearningPoints = numLearningPasses
    self.numColumns = numCorticalColumns
    self.inputSize = inputSize
    self.numInputBits = numInputBits

    # seed
    self.seed = seed
    random.seed(seed)

    # update parameters with overrides
    self.config = {
      "networkType": "MultipleL4L2Columns",
      "numCorticalColumns": numCorticalColumns,
      "externalInputSize": 0,
      "sensorInputSize": inputSize,
      "L4Params": self.getDefaultL4Params(inputSize),
      "L2Params": self.getDefaultL2Params(inputSize),
    }

    if L2Overrides is not None:
      self.config["L2Params"].update(L2Overrides)

    if L4Overrides is not None:
      self.config["L4Params"].update(L4Overrides)

    # create network
    self.network = createNetwork(self.config)

    # We have to explicitly initialize if we are going to change the phases
    self.network.initialize()

    self.sensorInputs = []
    self.L4Columns = []
    self.L2Columns = []

    for i in xrange(self.numColumns):
      self.sensorInputs.append(
        self.network.regions["sensorInput_" + str(i)].getSelf()
      )
      self.L4Columns.append(
        self.network.regions["L4Column_" + str(i)].getSelf()
      )
      self.L2Columns.append(
        self.network.regions["L2Column_" + str(i)].getSelf()
      )

    # will be populated during training
    self.objectL2Representations = {}
    self.statistics = []