def testRunPCANode(self):
    from nupic.engine import *

    numpy.random.RandomState(37)

    inputSize = 8

    net = Network()
    Network.registerRegion(ImageSensor)
    net.addRegion('sensor', 'py.ImageSensor' ,
          '{ width: %d, height: %d }' % (inputSize, inputSize))

    params = """{bottomUpCount: %d,
              SVDSampleCount: 5,
              SVDDimCount: 2}""" % inputSize

    pca = net.addRegion('pca', 'py.PCANode', params)

    #nodeAbove = CreateNode("py.ImageSensor", phase=0, categoryOut=1, dataOut=3,
    #                       width=3, height=1)
    #net.addElement('nodeAbove', nodeAbove)

    linkParams = '{ mapping: in, rfSize: [%d, %d] }' % (inputSize, inputSize)
    net.link('sensor', 'pca', 'UniformLink', linkParams, 'dataOut', 'bottomUpIn')

    net.initialize()

    for i in range(10):
      pca.getSelf()._testInputs = numpy.random.random([inputSize])
      net.run(1)
Exemple #2
0
def main():
  # Create Network instance
  network = Network()

  # Add three TestNode regions to network
  network.addRegion("region1", "TestNode", "")
  network.addRegion("region2", "TestNode", "")
  network.addRegion("region3", "TestNode", "")

  # Set dimensions on first region
  region1 = network.getRegions().getByName("region1")
  region1.setDimensions(Dimensions([1, 1]))

  # Link regions
  network.link("region1", "region2", "UniformLink", "")
  network.link("region2", "region1", "UniformLink", "")
  network.link("region1", "region3", "UniformLink", "")
  network.link("region2", "region3", "UniformLink", "")

  # Initialize network
  network.initialize()

  # Initialize Network Visualizer
  viz = NetworkVisualizer(network)

  # Render w/ graphviz
  viz.render(renderer=GraphVizRenderer)

  # Render w/ networkx
  viz.render(renderer=NetworkXRenderer)
Exemple #3
0
def _createLPFNetwork(addSP = True, addTP = False):
  """Create an 'old-style' network ala LPF and return it."""

  # ==========================================================================
  # Create the encoder and data source stuff we need to configure the sensor
  sensorParams = dict(verbosity = _VERBOSITY)
  encoder = _createEncoder()
  trainFile = findDataset("extra/gym/gym.csv")
  dataSource = FileRecordStream(streamID=trainFile)
  dataSource.setAutoRewind(True)

  # Create all the stuff we need to configure the CLARegion
  g_claConfig['spEnable'] = addSP
  g_claConfig['tpEnable'] = addTP
  claParams = _getCLAParams(encoder = encoder, config= g_claConfig)
  claParams['spSeed'] = g_claConfig['spSeed']
  claParams['tpSeed'] = g_claConfig['tpSeed']

  # ==========================================================================
  # Now create the network itself
  n = Network()

  n.addRegion("sensor", "py.RecordSensor", json.dumps(sensorParams))

  sensor = n.regions['sensor'].getSelf()
  sensor.encoder = encoder
  sensor.dataSource = dataSource

  n.addRegion("level1", "py.CLARegion", json.dumps(claParams))

  n.link("sensor", "level1", "UniformLink", "")
  n.link("sensor", "level1", "UniformLink", "",
         srcOutput="resetOut", destInput="resetIn")

  return n
Exemple #4
0
def _createLPFNetwork(addSP=True, addTP=False):
    """Create an 'old-style' network ala LPF and return it."""

    # ==========================================================================
    # Create the encoder and data source stuff we need to configure the sensor
    sensorParams = dict(verbosity=_VERBOSITY)
    encoder = _createEncoder()
    trainFile = findDataset("extra/gym/gym.csv")
    dataSource = FileRecordStream(streamID=trainFile)
    dataSource.setAutoRewind(True)

    # Create all the stuff we need to configure the CLARegion
    g_claConfig["spEnable"] = addSP
    g_claConfig["tpEnable"] = addTP
    claParams = _getCLAParams(encoder=encoder, config=g_claConfig)
    claParams["spSeed"] = g_claConfig["spSeed"]
    claParams["tpSeed"] = g_claConfig["tpSeed"]

    # ==========================================================================
    # Now create the network itself
    n = Network()

    n.addRegion("sensor", "py.RecordSensor", json.dumps(sensorParams))

    sensor = n.regions["sensor"].getSelf()
    sensor.encoder = encoder
    sensor.dataSource = dataSource

    n.addRegion("level1", "py.CLARegion", json.dumps(claParams))

    n.link("sensor", "level1", "UniformLink", "")
    n.link("sensor", "level1", "UniformLink", "", srcOutput="resetOut", destInput="resetIn")

    return n
def runExperiment():
    Network.unregisterRegion("ImageSensor")
    Network.registerRegion(ImageSensor)
    Network.registerRegion(PCANode)
    inputSize = 8

    net = Network()
    sensor = net.addRegion(
        "sensor", "py.ImageSensor",
        "{ width: %d, height: %d }" % (inputSize, inputSize))

    params = ("{bottomUpCount: %s, "
              " SVDSampleCount: 5, "
              " SVDDimCount: 2}" % inputSize)

    pca = net.addRegion("pca", "py.PCANode", params)

    linkParams = "{ mapping: in, rfSize: [%d, %d] }" % (inputSize, inputSize)
    net.link("sensor", "pca", "UniformLink", linkParams, "dataOut",
             "bottomUpIn")

    net.initialize()

    for i in range(10):
        pca.getSelf()._testInputs = numpy.random.random([inputSize])
        net.run(1)
        print s.sendRequest("nodeOPrint pca_node")
def runExperiment():
  Network.unregisterRegion("ImageSensor")
  Network.registerRegion(ImageSensor)
  Network.registerRegion(PCANode)
  inputSize = 8

  net = Network()
  sensor = net.addRegion(
      "sensor", "py.ImageSensor" ,
      "{ width: %d, height: %d }" % (inputSize, inputSize))

  params = ("{bottomUpCount: %s, "
            " SVDSampleCount: 5, "
            " SVDDimCount: 2}" % inputSize)

  pca = net.addRegion("pca", "py.PCANode", params)

  linkParams = "{ mapping: in, rfSize: [%d, %d] }" % (inputSize, inputSize)
  net.link("sensor", "pca", "UniformLink", linkParams, "dataOut", "bottomUpIn")

  net.initialize()

  for i in range(10):
    pca.getSelf()._testInputs = numpy.random.random([inputSize])
    net.run(1)
    print s.sendRequest("nodeOPrint pca_node")
Exemple #7
0
    def testRunPCANode(self):
        from nupic.engine import *

        numpy.random.RandomState(37)

        inputSize = 8

        net = Network()
        Network.registerRegion(ImageSensor)
        net.addRegion('sensor', 'py.ImageSensor',
                      '{ width: %d, height: %d }' % (inputSize, inputSize))

        params = """{bottomUpCount: %d,
              SVDSampleCount: 5,
              SVDDimCount: 2}""" % inputSize

        pca = net.addRegion('pca', 'py.PCANode', params)

        #nodeAbove = CreateNode("py.ImageSensor", phase=0, categoryOut=1, dataOut=3,
        #                       width=3, height=1)
        #net.addElement('nodeAbove', nodeAbove)

        linkParams = '{ mapping: in, rfSize: [%d, %d] }' % (inputSize,
                                                            inputSize)
        net.link('sensor', 'pca', 'UniformLink', linkParams, 'dataOut',
                 'bottomUpIn')

        net.initialize()

        for i in range(10):
            pca.getSelf()._testInputs = numpy.random.random([inputSize])
            net.run(1)
  def testSensor(self):

    # Create a simple network to test the sensor
    rawParams = {"outputWidth": 1029}
    net = Network()
    rawSensor = net.addRegion("raw","py.RawSensor", json.dumps(rawParams))
    vfe = net.addRegion("output","VectorFileEffector","")
    net.link("raw", "output", "UniformLink", "")

    self.assertEqual(rawSensor.getParameter("outputWidth"),1029,
                     "Incorrect outputWidth parameter")

    # Add vectors to the queue using two different add methods. Later we
    # will check to ensure these are actually output properly.
    rawSensor.executeCommand(["addDataToQueue", "[2, 4, 6]", "0", "42"])
    rawSensorPy = rawSensor.getSelf()
    rawSensorPy.addDataToQueue([2, 42, 1023], 1, 43)
    rawSensorPy.addDataToQueue([18, 19, 20], 0, 44)

    # Set an output file before we run anything
    vfe.setParameter("outputFile",os.path.join(self.tmpDir,"temp.csv"))

    # Run the network and check outputs are as expected
    net.run(1)
    self.assertEqual(rawSensor.getOutputData("dataOut").nonzero()[0].sum(),
                     sum([2, 4, 6]), "Value of dataOut incorrect")
    self.assertEqual(rawSensor.getOutputData("resetOut").sum(),0,
                     "Value of resetOut incorrect")
    self.assertEqual( rawSensor.getOutputData("sequenceIdOut").sum(),42,
                      "Value of sequenceIdOut incorrect")

    net.run(1)
    self.assertEqual(rawSensor.getOutputData("dataOut").nonzero()[0].sum(),
                     sum([2, 42, 1023]), "Value of dataOut incorrect")
    self.assertEqual(rawSensor.getOutputData("resetOut").sum(),1,
                     "Value of resetOut incorrect")
    self.assertEqual( rawSensor.getOutputData("sequenceIdOut").sum(),43,
                      "Value of sequenceIdOut incorrect")

    # Make sure we can save and load the network after running
    net.save(os.path.join(self.tmpDir,"rawNetwork.nta"))
    net2 = Network(os.path.join(self.tmpDir,"rawNetwork.nta"))
    rawSensor2 = net2.regions.get("raw")
    vfe2 = net2.regions.get("output")

    # Ensure parameters are preserved
    self.assertEqual(rawSensor2.getParameter("outputWidth"),1029,
                     "Incorrect outputWidth parameter")

    # Ensure the queue is preserved through save/load
    vfe2.setParameter("outputFile",os.path.join(self.tmpDir,"temp.csv"))
    net2.run(1)
    self.assertEqual(rawSensor2.getOutputData("dataOut").nonzero()[0].sum(),
                     sum([18, 19, 20]), "Value of dataOut incorrect")
    self.assertEqual(rawSensor2.getOutputData("resetOut").sum(),0,
                     "Value of resetOut incorrect")
    self.assertEqual( rawSensor2.getOutputData("sequenceIdOut").sum(),44,
                      "Value of sequenceIdOut incorrect")
Exemple #9
0
    def testSensor(self):
        # Create a simple network to test the sensor
        params = {
            "activeBits": self.encoder.w,
            "outputWidth": self.encoder.n,
            "radius": 2,
            "verbosity": self.encoder.verbosity,
        }
        net = Network()
        region = net.addRegion("coordinate", "py.CoordinateSensorRegion", json.dumps(params))
        vfe = net.addRegion("output", "VectorFileEffector", "")
        net.link("coordinate", "output", "UniformLink", "")

        self.assertEqual(region.getParameter("outputWidth"), self.encoder.n, "Incorrect outputWidth parameter")

        # Add vectors to the queue using two different add methods. Later we
        # will check to ensure these are actually output properly.
        region.executeCommand(["addDataToQueue", "[2, 4, 6]", "0", "42"])
        regionPy = region.getSelf()
        regionPy.addDataToQueue([2, 42, 1023], 1, 43)
        regionPy.addDataToQueue([18, 19, 20], 0, 44)

        # Set an output file before we run anything
        vfe.setParameter("outputFile", os.path.join(self.tmpDir, "temp.csv"))

        # Run the network and check outputs are as expected
        net.run(1)
        expected = self.encoder.encode((numpy.array([2, 4, 6]), params["radius"]))
        actual = region.getOutputData("dataOut")
        self.assertEqual(actual.sum(), expected.sum(), "Value of dataOut incorrect")
        self.assertEqual(region.getOutputData("resetOut"), 0, "Value of resetOut incorrect")
        self.assertEqual(region.getOutputData("sequenceIdOut"), 42, "Value of sequenceIdOut incorrect")

        net.run(1)
        expected = self.encoder.encode((numpy.array([2, 42, 1023]), params["radius"]))
        actual = region.getOutputData("dataOut")
        self.assertEqual(actual.sum(), expected.sum(), "Value of dataOut incorrect")
        self.assertEqual(region.getOutputData("resetOut"), 1, "Value of resetOut incorrect")
        self.assertEqual(region.getOutputData("sequenceIdOut"), 43, "Value of sequenceIdOut incorrect")

        # Make sure we can save and load the network after running
        net.save(os.path.join(self.tmpDir, "coordinateNetwork.nta"))
        net2 = Network(os.path.join(self.tmpDir, "coordinateNetwork.nta"))
        region2 = net2.regions.get("coordinate")
        vfe2 = net2.regions.get("output")

        # Ensure parameters are preserved
        self.assertEqual(region2.getParameter("outputWidth"), self.encoder.n, "Incorrect outputWidth parameter")

        # Ensure the queue is preserved through save/load
        vfe2.setParameter("outputFile", os.path.join(self.tmpDir, "temp.csv"))
        net2.run(1)
        expected = self.encoder.encode((numpy.array([18, 19, 20]), params["radius"]))
        actual = region2.getOutputData("dataOut")
        self.assertEqual(actual.sum(), expected.sum(), "Value of dataOut incorrect")
        self.assertEqual(region2.getOutputData("resetOut"), 0, "Value of resetOut incorrect")

        self.assertEqual(region2.getOutputData("sequenceIdOut"), 44, "Value of sequenceIdOut incorrect")
Exemple #10
0
def createNetwork():
    """Create the Network instance.

    The network has a sensor region reading data from `rataSource` and passing
    the encoded representation to an SPRegion. The SPRegion output is passed to
    a TPRegion.

    :param dataSource: a RecordStream instance to get data from
    :returns: a Network instance ready to run
    """
    network = Network()

    # Create Sensor
    network.addRegion("sensor", "py.RecordSensor", json.dumps({"verbosity":
                                                               0}))
    sensor = network.regions["sensor"].getSelf()
    sensor.encoder = createSensorEncoder()
    sensor.dataSource = DataBuffer()

    # Add the spatial pooler region
    PARAMS['SP']["inputWidth"] = sensor.encoder.getWidth()
    network.addRegion("spatialPoolerRegion", "py.SPRegion",
                      json.dumps(PARAMS['SP']))
    network.link("sensor", "spatialPoolerRegion", "UniformLink", "")

    # Add the TPRegion on top of the SPRegion
    network.addRegion("temporalPoolerRegion", "py.TPRegion",
                      json.dumps(PARAMS['TP']))
    network.link("spatialPoolerRegion", "temporalPoolerRegion", "UniformLink",
                 "")

    # Add classifier
    network.addRegion("classifierRegion", "py.CLAClassifierRegion",
                      json.dumps(PARAMS['CL']))

    network.initialize()

    # Make sure learning is enabled
    spatialPoolerRegion = network.regions["spatialPoolerRegion"]
    spatialPoolerRegion.setParameter("learningMode", True)
    spatialPoolerRegion.setParameter("anomalyMode", True)

    temporalPoolerRegion = network.regions["temporalPoolerRegion"]
    temporalPoolerRegion.setParameter("topDownMode", False)
    temporalPoolerRegion.setParameter("learningMode", True)
    temporalPoolerRegion.setParameter("inferenceMode", True)
    temporalPoolerRegion.setParameter("anomalyMode", False)

    classifierRegion = network.regions["classifierRegion"]
    classifierRegion.setParameter('inferenceMode', True)
    classifierRegion.setParameter('learningMode', True)

    return network
Exemple #11
0
def createNetwork(dataSource):
    """Create the Network instance.

  The network has a sensor region reading data from `dataSource` and passing
  the encoded representation to an Identity Region.

  :param dataSource: a RecordStream instance to get data from
  :returns: a Network instance ready to run
  """
    network = Network()

    # Our input is sensor data from the gym file. The RecordSensor region
    # allows us to specify a file record stream as the input source via the
    # dataSource attribute.
    network.addRegion("sensor", "py.RecordSensor",
                      json.dumps({"verbosity": _VERBOSITY}))
    sensor = network.regions["sensor"].getSelf()
    # The RecordSensor needs to know how to encode the input values
    sensor.encoder = createEncoder()
    # Specify the dataSource as a file record stream instance
    sensor.dataSource = dataSource

    # CUSTOM REGION
    # Add path to custom region to PYTHONPATH
    # NOTE: Before using a custom region, please modify your PYTHONPATH
    # export PYTHONPATH="<path to custom region module>:$PYTHONPATH"
    # In this demo, we have modified it using sys.path.append since we need it to
    # have an effect on this program.
    sys.path.append(os.path.dirname(os.path.abspath(__file__)))

    from custom_region.identity_region import IdentityRegion

    # Add custom region class to the network
    Network.registerRegion(IdentityRegion)

    # Create a custom region
    network.addRegion("identityRegion", "py.IdentityRegion",
                      json.dumps(I_PARAMS))

    # Link the Identity region to the sensor input
    network.link("sensor",
                 "identityRegion",
                 "UniformLink",
                 "",
                 srcOutput="sourceOut",
                 destInput="in")

    network.initialize()

    return network
def createNetwork():
    """Create the Network instance.

    The network has a sensor region reading data from `rataSource` and passing
    the encoded representation to an SPRegion. The SPRegion output is passed to
    a TPRegion.

    :param dataSource: a RecordStream instance to get data from
    :returns: a Network instance ready to run
    """
    network = Network()

    # Create Sensor
    network.addRegion("sensor", "py.RecordSensor", json.dumps({"verbosity": 0}))
    sensor = network.regions["sensor"].getSelf()
    sensor.encoder    = createSensorEncoder()
    sensor.dataSource = DataBuffer()

    # Add the spatial pooler region
    PARAMS['SP']["inputWidth"] = sensor.encoder.getWidth()
    network.addRegion("spatialPoolerRegion", "py.SPRegion", json.dumps(PARAMS['SP']))
    network.link("sensor", "spatialPoolerRegion", "UniformLink", "")

    # Add the TPRegion on top of the SPRegion
    network.addRegion("temporalPoolerRegion", "py.TPRegion", json.dumps(PARAMS['TP']))
    network.link("spatialPoolerRegion", "temporalPoolerRegion", "UniformLink", "")

    # Add classifier
    network.addRegion( "classifierRegion", "py.CLAClassifierRegion", json.dumps(PARAMS['CL']))

    network.initialize()



    # Make sure learning is enabled
    spatialPoolerRegion = network.regions["spatialPoolerRegion"]
    spatialPoolerRegion.setParameter("learningMode", True)
    spatialPoolerRegion.setParameter("anomalyMode", True)

    temporalPoolerRegion = network.regions["temporalPoolerRegion"]
    temporalPoolerRegion.setParameter("topDownMode", False)
    temporalPoolerRegion.setParameter("learningMode", True)
    temporalPoolerRegion.setParameter("inferenceMode", True)
    temporalPoolerRegion.setParameter("anomalyMode", False)

    classifierRegion = network.regions["classifierRegion"]
    classifierRegion.setParameter('inferenceMode', True)
    classifierRegion.setParameter('learningMode', True)

    return network
Exemple #13
0
  def createNet(self):
    """ Set up the structure of the network """
    net = Network()

    Network.unregisterRegion(SaccadeSensor.__name__)
    Network.registerRegion(SaccadeSensor)

    Network.registerRegion(TMRegion)

    imageSensorParams = copy.deepcopy(DEFAULT_IMAGESENSOR_PARAMS)
    if self.loggingDir is not None:
      imageSensorParams["logDir"] = "sensorImages/" + self.loggingDir
      imageSensorParams["logOutputImages"] = 1
      imageSensorParams["logOriginalImages"] = 1
      imageSensorParams["logFilteredImages"] = 1
      imageSensorParams["logLocationImages"] = 1
      imageSensorParams["logLocationOnOriginalImage"] = 1

    net.addRegion("sensor", "py.SaccadeSensor",
                  yaml.dump(imageSensorParams))
    sensor = net.regions["sensor"].getSelf()

    DEFAULT_SP_PARAMS["columnCount"] = sensor.getOutputElementCount("dataOut")
    net.addRegion("SP", "py.SPRegion", yaml.dump(DEFAULT_SP_PARAMS))
    sp = net.regions["SP"].getSelf()

    DEFAULT_TM_PARAMS["columnDimensions"] = (sp.getOutputElementCount("bottomUpOut"),)
    net.addRegion("TM", "py.TMRegion", yaml.dump(DEFAULT_TM_PARAMS))

    net.addRegion("classifier","py.KNNClassifierRegion",
                  yaml.dump(DEFAULT_CLASSIFIER_PARAMS))


    net.link("sensor", "SP", "UniformLink", "",
             srcOutput="dataOut", destInput="bottomUpIn")
    net.link("SP", "TM", "UniformLink", "",
             srcOutput="bottomUpOut", destInput="activeColumns")
    net.link("sensor", "TM", "UniformLink", "",
             srcOutput="saccadeOut", destInput="activeExternalCells")
    net.link("TM", "classifier", "UniformLink", "",
             srcOutput="predictedActiveCells", destInput="bottomUpIn")
    net.link("sensor", "classifier", "UniformLink", "",
             srcOutput="categoryOut", destInput="categoryIn")

    self.net = net
    self.networkSensor = self.net.regions["sensor"]
    self.networkSP = self.net.regions["SP"]
    self.networkTM = self.net.regions["TM"]
    self.networkClassifier = self.net.regions["classifier"]
Exemple #14
0
def createNetwork(dataSource):
  """Create the Network instance.

  The network has a sensor region reading data from `dataSource` and passing
  the encoded representation to an Identity Region.

  :param dataSource: a RecordStream instance to get data from
  :returns: a Network instance ready to run
  """
  network = Network()

  # Our input is sensor data from the gym file. The RecordSensor region
  # allows us to specify a file record stream as the input source via the
  # dataSource attribute.
  network.addRegion("sensor", "py.RecordSensor",
                    json.dumps({"verbosity": _VERBOSITY}))
  sensor = network.regions["sensor"].getSelf()
  # The RecordSensor needs to know how to encode the input values
  sensor.encoder = createEncoder()
  # Specify the dataSource as a file record stream instance
  sensor.dataSource = dataSource

  # CUSTOM REGION
  # Add path to custom region to PYTHONPATH
  # NOTE: Before using a custom region, please modify your PYTHONPATH
  # export PYTHONPATH="<path to custom region module>:$PYTHONPATH"
  # In this demo, we have modified it using sys.path.append since we need it to
  # have an effect on this program.
  sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  
  from custom_region.identity_region import IdentityRegion

  # Add custom region class to the network
  Network.registerRegion(IdentityRegion)

  # Create a custom region
  network.addRegion("identityRegion", "py.IdentityRegion",
                    json.dumps({
                      "dataWidth": sensor.encoder.getWidth(),
                    }))

  # Link the Identity region to the sensor output
  network.link("sensor", "identityRegion", "UniformLink", "")

  network.initialize()

  return network
  def testOverlap(self):
    """Create a simple network to test the region."""

    rawParams = {"outputWidth": 8 * 2048}
    net = Network()
    rawSensor = net.addRegion("raw", "py.RawSensor", json.dumps(rawParams))
    l2c = net.addRegion("L2", "py.ColumnPoolerRegion", "")
    net.link("raw", "L2", "UniformLink", "")

    self.assertEqual(rawSensor.getParameter("outputWidth"),
                     l2c.getParameter("inputWidth"),
                     "Incorrect outputWidth parameter")

    rawSensorPy = rawSensor.getSelf()
    rawSensorPy.addDataToQueue([2, 4, 6], 0, 42)
    rawSensorPy.addDataToQueue([2, 42, 1023], 1, 43)
    rawSensorPy.addDataToQueue([18, 19, 20], 0, 44)

    # Run the network and check outputs are as expected
    net.run(3)
  def testNetworkCreate(self):
    """Create a simple network to test the region."""

    rawParams = {"outputWidth": 16*2048}
    net = Network()
    rawSensor = net.addRegion("raw","py.RawSensor", json.dumps(rawParams))
    l2c = net.addRegion("L2", "py.L2Column", "")
    net.link("raw", "L2", "UniformLink", "")

    self.assertEqual(rawSensor.getParameter("outputWidth"),
                     l2c.getParameter("inputWidth"),
                     "Incorrect outputWidth parameter")

    rawSensorPy = rawSensor.getSelf()
    rawSensorPy.addDataToQueue([2, 4, 6], 0, 42)
    rawSensorPy.addDataToQueue([2, 42, 1023], 1, 43)
    rawSensorPy.addDataToQueue([18, 19, 20], 0, 44)

    # Run the network and check outputs are as expected
    net.run(3)
Exemple #17
0
def createNetwork():
  """
  Set up the following simple network and return it:

    ImageSensorRegion -> SP -> KNNClassifier Region

  """
  net = Network()

  # Register the ImageSensor region with the network
  Network.registerRegion(ImageSensor)

  # Add the three regions
  net.addRegion("sensor", "py.ImageSensor",
                yaml.dump(DEFAULT_IMAGESENSOR_PARAMS))
  net.addRegion("SP", "py.SPRegion", yaml.dump(DEFAULT_SP_PARAMS))
  net.addRegion("classifier","py.KNNClassifierRegion",
                yaml.dump(DEFAULT_CLASSIFIER_PARAMS))

  # Link up the regions. Note that we need to create a link from the sensor
  # to the classifier to send in the category labels.
  net.link("sensor", "SP", "UniformLink", "",
           srcOutput = "dataOut", destInput = "bottomUpIn")
  net.link("SP", "classifier", "UniformLink", "",
           srcOutput = "bottomUpOut", destInput = "bottomUpIn")
  net.link("sensor", "classifier", "UniformLink", "",
           srcOutput = "categoryOut", destInput = "categoryIn")

  return net
  def createNet(self):
    """ Set up the structure of the network """
    net = Network()

    Network.unregisterRegion(ImageSensor.__name__)
    Network.registerRegion(ImageSensor)

    imageSensorParams = copy.deepcopy(DEFAULT_IMAGESENSOR_PARAMS)
    if self.loggingDir is not None:
      imageSensorParams["logDir"] = "sensorImages/" + self.loggingDir
      imageSensorParams["logOutputImages"] = 1
      imageSensorParams["logOriginalImages"] = 1
      imageSensorParams["logFilteredImages"] = 1
      imageSensorParams["logLocationImages"] = 1
      imageSensorParams["logLocationOnOriginalImage"] = 1

    net.addRegion("sensor", "py.ImageSensor",
                  yaml.dump(imageSensorParams))
    net.addRegion("SP", "py.SPRegion", yaml.dump(DEFAULT_SP_PARAMS))
    net.addRegion("classifier","py.KNNClassifierRegion",
                  yaml.dump(DEFAULT_CLASSIFIER_PARAMS))

    net.link("sensor", "SP", "UniformLink", "",
             srcOutput = "dataOut", destInput = "bottomUpIn")
    net.link("SP", "classifier", "UniformLink", "",
             srcOutput = "bottomUpOut", destInput = "bottomUpIn")
    net.link("sensor", "classifier", "UniformLink", "",
             srcOutput = "categoryOut", destInput = "categoryIn")

    self.net = net
    self.networkSensor = self.net.regions["sensor"]
    self.networkSP = self.net.regions["SP"]
    self.networkClassifier = self.net.regions["classifier"]
Exemple #19
0
def createNetwork():
  """
  Set up the following simple network and return it:

    ImageSensorRegion -> SP -> KNNClassifier Region

  """
  net = Network()

  # Add the three regions
  net.addRegion("sensor", "py.ImageSensor",
                json.dumps(DEFAULT_IMAGESENSOR_PARAMS))
  net.addRegion("SP", "py.SPRegion", json.dumps(DEFAULT_SP_PARAMS))
  net.addRegion("classifier","py.KNNClassifierRegion",
                json.dumps(DEFAULT_CLASSIFIER_PARAMS))

  # Link up the regions
  net.link("sensor", "SP", "UniformLink", "",
           srcOutput = "dataOut", destInput = "bottomUpIn")
  net.link("SP", "classifier", "UniformLink", "",
           srcOutput = "bottomUpOut", destInput = "bottomUpIn")
  net.link("sensor", "classifier", "UniformLink", "",
           srcOutput = "categoryOut", destInput = "categoryIn")

  return net
def createNetwork():
  """
  Set up the following simple network and return it:

    ImageSensorRegion -> SP -> KNNClassifier Region

  """
  net = Network()

  # Add the three regions
  net.addRegion("sensor", "py.ImageSensor",
                json.dumps(DEFAULT_IMAGESENSOR_PARAMS))
  net.addRegion("SP", "py.SPRegion", json.dumps(DEFAULT_SP_PARAMS))
  net.addRegion("classifier","py.KNNClassifierRegion",
                json.dumps(DEFAULT_CLASSIFIER_PARAMS))

  # Link up the regions. Note that we need to create a link from the sensor
  # to the classifier to send in the category labels.
  net.link("sensor", "SP", "UniformLink", "",
           srcOutput = "dataOut", destInput = "bottomUpIn")
  net.link("SP", "classifier", "UniformLink", "",
           srcOutput = "bottomUpOut", destInput = "bottomUpIn")
  net.link("sensor", "classifier", "UniformLink", "",
           srcOutput = "categoryOut", destInput = "categoryIn")

  # Make sure all objects are initialized
  net.initialize()

  return net
Exemple #21
0
def main():
  # Create Network instance
  network = Network()

  # Add three TestNode regions to network
  network.addRegion("region1", "TestNode", "")
  network.addRegion("region2", "TestNode", "")
  network.addRegion("region3", "TestNode", "")

  # Set dimensions on first region
  region1 = network.getRegions().getByName("region1")
  region1.setDimensions(Dimensions([1, 1]))

  # Link regions
  network.link("region1", "region2", "UniformLink", "")
  network.link("region2", "region1", "UniformLink", "")
  network.link("region1", "region3", "UniformLink", "")
  network.link("region2", "region3", "UniformLink", "")

  # Initialize network
  network.initialize()

  # Initialize Network Visualizer
  viz = NetworkVisualizer(network)

  # Render w/ graphviz
  viz.render(renderer=GraphVizRenderer)

  # Render w/ networkx
  viz.render(renderer=NetworkXRenderer)

  # Render to dot (stdout)
  viz.render(renderer=DotRenderer)

  # Render to dot (file)
  viz.render(renderer=lambda: DotRenderer(open("example.dot", "w")))
def create_network():
    network = Network()

    m_sensor = network.addRegion("Measurement", 'ScalarSensor',
                                 json.dumps(_SCALAR_ENCODER))
    dt_sensor = network.addRegion("DT", 'py.PluggableEncoderSensor', "")
    dt_sensor.getSelf().encoder = DateEncoder(**_DATE_ENCODER)

    # Add a SPRegion, a region containing a spatial pooler
    scalar_n = m_sensor.getParameter('n')
    dt_n = dt_sensor.getSelf().encoder.getWidth()
    _SP_PARAMS["inputWidth"] = scalar_n + dt_n
    network.addRegion("sp", "py.SPRegion", json.dumps(_SP_PARAMS))

    # Input to the Spatial Pooler
    network.link("Measurement", "sp", "UniformLink", "")
    network.link("DT", "sp", "UniformLink", "")

    # Add a TPRegion, a region containing a Temporal Memory
    network.addRegion("tm", "py.TMRegion", json.dumps(_TM_PARAMS))

    # Set up links
    network.link("sp", "tm", "UniformLink", "")
    network.link("tm",
                 "sp",
                 "UniformLink",
                 "",
                 srcOutput="topDownOut",
                 destInput="topDownIn")

    network.regions['sp'].setParameter("learningMode", True)
    network.regions['sp'].setParameter("anomalyMode", False)

    # network.regions['tm'].setParameter("topDownMode", True)  # check this

    # Make sure learning is enabled (this is the default)
    network.regions['tm'].setParameter("learningMode", True)
    # Enable anomalyMode so the tm calculates anomaly scores
    network.regions['tm'].setParameter("anomalyMode", True)
    # Enable inference mode to be able to get predictions
    network.regions['tm'].setParameter("inferenceMode", True)

    # TODO: enable all inferences
    return network
  def testLinkingDownwardDimensions(self):
    #
    # Linking can induce dimensions downward
    #
    net = Network()
    level1 = net.addRegion("level1", "TestNode", "")
    level2 = net.addRegion("level2", "TestNode", "")
    dims = Dimensions([3, 2])
    level2.setDimensions(dims)
    net.link("level1", "level2", "TestFanIn2", "")
    net.initialize()

    # Level1 should now have dimensions [6, 4]
    self.assertEquals(level1.getDimensions()[0], 6)
    self.assertEquals(level1.getDimensions()[1], 4)

    #
    # We get nice error messages when network can't be initialized
    #
    LOGGER.info("=====")
    LOGGER.info("Creating a 3 level network in which levels 1 and 2 have")
    LOGGER.info("dimensions but network initialization will fail because")
    LOGGER.info("level3 does not have dimensions")
    LOGGER.info("Error message follows:")

    net = Network()
    level1 = net.addRegion("level1", "TestNode", "")
    level2 = net.addRegion("level2", "TestNode", "")
    _level3 = net.addRegion("level3", "TestNode", "")
    dims = Dimensions([6, 4])
    level1.setDimensions(dims)
    net.link("level1", "level2", "TestFanIn2", "")
    self.assertRaises(RuntimeError, net.initialize)
    LOGGER.info("=====")

    LOGGER.info("======")
    LOGGER.info("Creating a link with incompatible dimensions. \
      Error message follows")
    net.link("level2", "level3", "TestFanIn2", "")
    self.assertRaises(RuntimeError, net.initialize)
    def testLinkingDownwardDimensions(self):
        #
        # Linking can induce dimensions downward
        #
        net = Network()
        level1 = net.addRegion("level1", "TestNode", "")
        level2 = net.addRegion("level2", "TestNode", "")
        dims = Dimensions([3, 2])
        level2.setDimensions(dims)
        net.link("level1", "level2", "TestFanIn2", "")
        net.initialize()

        # Level1 should now have dimensions [6, 4]
        self.assertEqual(level1.getDimensions()[0], 6)
        self.assertEqual(level1.getDimensions()[1], 4)

        #
        # We get nice error messages when network can't be initialized
        #
        LOGGER.info("=====")
        LOGGER.info("Creating a 3 level network in which levels 1 and 2 have")
        LOGGER.info("dimensions but network initialization will fail because")
        LOGGER.info("level3 does not have dimensions")
        LOGGER.info("Error message follows:")

        net = Network()
        level1 = net.addRegion("level1", "TestNode", "")
        level2 = net.addRegion("level2", "TestNode", "")
        _level3 = net.addRegion("level3", "TestNode", "")
        dims = Dimensions([6, 4])
        level1.setDimensions(dims)
        net.link("level1", "level2", "TestFanIn2", "")
        self.assertRaises(RuntimeError, net.initialize)
        LOGGER.info("=====")

        LOGGER.info("======")
        LOGGER.info("Creating a link with incompatible dimensions. \
      Error message follows")
        net.link("level2", "level3", "TestFanIn2", "")
        self.assertRaises(RuntimeError, net.initialize)
def createAnomalyNetwork(dataSource):

    network = Network()
    
    #sensor region
    network.addRegion("sensor", "py.RecordSensor", json.dumps({"verbosity": _VERBOSITY}))
    
    #encoder setup
    sensorRegion = network.regions["sensor"].getSelf()
    sensorRegion.encoder = createEncoder()
    
    sensorRegion.dataSource = dataSource

    #SP width must have sensor output width
    SP_PARAMS["inputWidth"] = sensorRegion.encoder.getWidth()
    
    #Add SP and TM regions
    network.addRegion("SP", "py.SPRegion", json.dumps(SP_PARAMS))
    network.link("sensor", "SP", "UniformLink", "")
    network.link("sensor", "SP", "UniformLink", "",
                 srcOutput="resetOut", destInput="resetIn")
    network.link("SP", "sensor", "UniformLink", "",
                  srcOutput="spatialTopDownOut", destInput="spatialTopDownIn")
    network.link("SP", "sensor","UniformLink", "",
                  srcOutput ="temporalTopDownOut", destInput="temporalTopDownIn")
    network.addRegion("TM", "py.TMRegion", json.dumps(TM_PARAMS))
    network.link("SP", "TM", "UniformLink", "")
    network.link("TM", "SP", "UniformLink", "", srcOutput="topDownOut", destInput="topDownIn")
    
    #Add anomalyLikeliHood
    network.addRegion("ALH", "py.AnomalyLikelihoodRegion", json.dumps({}))
    network.link("TM", "ALH", "UniformLink", "", srcOutput="anomalyScore", destInput="rawAnomalyScore")
    network.link("sensor", "ALH", "UniformLink", "", srcOutput="sourceOut", destInput="metricValue")


    #set layer parameters
    spRegion = network.regions["SP"]
    spRegion.setParameter("learningMode", True)
    spRegion.setParameter("anomalyMode", False)
    
    tmRegion = network.regions["TM"]
    tmRegion.setParameter("topDownMode", True)
    tmRegion.setParameter("learningMode", True)
    tmRegion.setParameter("inferenceMode", True)
    tmRegion.setParameter("anomalyMode", True)
    
    return network
Exemple #26
0
    def testSimpleMulticlassNetwork(self):

        # Setup data record stream of fake data (with three categories)
        filename = _getTempFileName()
        fields = [("timestamp", "datetime", "T"), ("value", "float", ""),
                  ("reset", "int", "R"), ("sid", "int", "S"),
                  ("categories", "list", "C")]
        records = ([datetime(day=1, month=3, year=2010), 0.0, 1, 0, ""], [
            datetime(day=2, month=3, year=2010), 1.0, 0, 0, "1 2"
        ], [datetime(day=3, month=3, year=2010), 1.0, 0, 0,
            "1 2"], [datetime(day=4, month=3, year=2010), 2.0, 0, 0, "0"], [
                datetime(day=5, month=3, year=2010), 3.0, 0, 0, "1 2"
            ], [datetime(day=6, month=3, year=2010), 5.0, 0, 0,
                "1 2"], [datetime(day=7, month=3, year=2010), 8.0, 0, 0, "0"],
                   [datetime(day=8, month=3, year=2010), 13.0, 0, 0, "1 2"])
        dataSource = FileRecordStream(streamID=filename,
                                      write=True,
                                      fields=fields)
        for r in records:
            dataSource.appendRecord(list(r))

        # Create the network and get region instances.
        net = Network()
        net.addRegion("sensor", "py.RecordSensor", "{'numCategories': 3}")
        net.addRegion("classifier", "py.KNNClassifierRegion",
                      "{'k': 2,'distThreshold': 0,'maxCategoryCount': 3}")
        net.link("sensor",
                 "classifier",
                 "UniformLink",
                 "",
                 srcOutput="dataOut",
                 destInput="bottomUpIn")
        net.link("sensor",
                 "classifier",
                 "UniformLink",
                 "",
                 srcOutput="categoryOut",
                 destInput="categoryIn")
        sensor = net.regions["sensor"]
        classifier = net.regions["classifier"]

        # Setup sensor region encoder and data stream.
        dataSource.close()
        dataSource = FileRecordStream(filename)
        sensorRegion = sensor.getSelf()
        sensorRegion.encoder = MultiEncoder()
        sensorRegion.encoder.addEncoder(
            "value", ScalarEncoder(21, 0.0, 13.0, n=256, name="value"))
        sensorRegion.dataSource = dataSource

        # Get ready to run.
        net.initialize()

        # Train the network (by default learning is ON in the classifier, but assert
        # anyway) and then turn off learning and turn on inference mode.
        self.assertEqual(classifier.getParameter("learningMode"), 1)
        net.run(8)
        classifier.setParameter("inferenceMode", 1)
        classifier.setParameter("learningMode", 0)

        # Assert learning is OFF and that the classifier learned the dataset.
        self.assertEqual(classifier.getParameter("learningMode"), 0,
                         "Learning mode is not turned off.")
        self.assertEqual(classifier.getParameter("inferenceMode"), 1,
                         "Inference mode is not turned on.")
        self.assertEqual(
            classifier.getParameter("categoryCount"), 3,
            "The classifier should count three total categories.")
        # classififer learns 12 patterns b/c there are 12 categories amongst the
        # records:
        self.assertEqual(
            classifier.getParameter("patternCount"), 12,
            "The classifier should've learned 12 samples in total.")

        # Test the network on the same data as it trained on; should classify with
        # 100% accuracy.
        expectedCats = ([0.0, 0.5, 0.5], [0.0, 0.5, 0.5], [0.0, 0.5, 0.5],
                        [0.5, 0.5, 0.0], [0.0, 0.5,
                                          0.5], [0.0, 0.5,
                                                 0.5], [0.5, 0.5,
                                                        0.0], [0.0, 0.5, 0.5])
        dataSource.rewind()
        for i in xrange(8):
            net.run(1)
            inferredCats = classifier.getOutputData("categoriesOut")
            self.assertSequenceEqual(
                expectedCats[i], inferredCats.tolist(),
                "Classififer did not infer expected category probabilites for record "
                "number {}.".format(i))

        # Close data stream, delete file.
        dataSource.close()
        os.remove(filename)
def createMultiLevelNetwork(dataSource):
  
	network = Network()

	# Create and add a record sensor and a SP region
	sensor = NetworkUtils.createRecordSensor(network, name=_RECORD_SENSOR,
							  dataSource=dataSource, multilevelAnomaly=True)
	NetworkUtils.createSpatialPooler(network, name=_L1_SPATIAL_POOLER,
					  inputWidth=sensor.encoder.getWidth())

	# Link the SP region to the sensor input
	linkType = "UniformLink"
	linkParams = ""
	network.link(_RECORD_SENSOR, _L1_SPATIAL_POOLER, linkType, linkParams)

	# Create and add a TM region
	l1temporalMemory = NetworkUtils.createTemporalMemory(network, _L1_TEMPORAL_MEMORY)

	# Link SP region to TM region in the feedforward direction
	network.link(_L1_SPATIAL_POOLER, _L1_TEMPORAL_MEMORY, linkType, linkParams)

	# Add a classifier
	classifierParams = {  # Learning rate. Higher values make it adapt faster.
						'alpha': 0.005,

						# A comma separated list of the number of steps the
						# classifier predicts in the future. The classifier will
						# learn predictions of each order specified.
						'steps': '1,2,3,4,5,6,7',

						# The specific implementation of the classifier to use
						# See SDRClassifierFactory#create for options
						'implementation': 'py',

						# Diagnostic output verbosity control;
						# 0: silent; [1..6]: increasing levels of verbosity
						'verbosity': 0}

	l1Classifier = network.addRegion(_L1_CLASSIFIER, "py.SDRClassifierRegion",
								   json.dumps(classifierParams))
	l1Classifier.setParameter('inferenceMode', True)
	l1Classifier.setParameter('learningMode', True)
	network.link(_L1_TEMPORAL_MEMORY, _L1_CLASSIFIER, linkType, linkParams,
			   srcOutput="bottomUpOut", destInput="bottomUpIn")
	network.link(_RECORD_SENSOR, _L1_CLASSIFIER, linkType, linkParams,
			   srcOutput="categoryOut", destInput="categoryIn")
	network.link(_RECORD_SENSOR, _L1_CLASSIFIER, linkType, linkParams,
			   srcOutput="bucketIdxOut", destInput="bucketIdxIn")
	network.link(_RECORD_SENSOR, _L1_CLASSIFIER, linkType, linkParams,
			   srcOutput="actValueOut", destInput="actValueIn")
	
	# Second Level
	l2inputWidth = l1temporalMemory.getSelf().getOutputElementCount("bottomUpOut")
	NetworkUtils.createSpatialPooler(network, name=_L2_SPATIAL_POOLER, inputWidth=l2inputWidth)
	network.link(_L1_TEMPORAL_MEMORY, _L2_SPATIAL_POOLER, linkType, linkParams)

	NetworkUtils.createTemporalMemory(network, _L2_TEMPORAL_MEMORY)
	network.link(_L2_SPATIAL_POOLER, _L2_TEMPORAL_MEMORY, linkType, linkParams)

	l2Classifier = network.addRegion(_L2_CLASSIFIER, "py.SDRClassifierRegion",
								   json.dumps(classifierParams))
	l2Classifier.setParameter('inferenceMode', True)
	l2Classifier.setParameter('learningMode', True)
	network.link(_L2_TEMPORAL_MEMORY, _L2_CLASSIFIER, linkType, linkParams,
			   srcOutput="bottomUpOut", destInput="bottomUpIn")
	network.link(_RECORD_SENSOR, _L2_CLASSIFIER, linkType, linkParams,
			   srcOutput="categoryOut", destInput="categoryIn")
	network.link(_RECORD_SENSOR, _L2_CLASSIFIER, linkType, linkParams,
			   srcOutput="bucketIdxOut", destInput="bucketIdxIn")
	network.link(_RECORD_SENSOR, _L2_CLASSIFIER, linkType, linkParams,
			   srcOutput="actValueOut", destInput="actValueIn")

	steps = l2Classifier.getSelf().stepsList
	
	# initialize the results matrix, after the classifer has been defined
	w, h = len(steps), len(steps)+1
	global results
	results = [[-1 for x in range(w)] for y in range(h)] 
	global l1ErrorSum
	l2ErrorSum = [-1 for x in range(h)]
	
	#print("Length: "+str(len(steps)))
	
	return network
Exemple #28
0
def _createOPFNetwork(addSP=True, addTP=False):
    """Create a 'new-style' network ala OPF and return it.
  If addSP is true, an SPRegion will be added named 'level1SP'.
  If addTP is true, a TPRegion will be added named 'level1TP'
  """

    # ==========================================================================
    # Create the encoder and data source stuff we need to configure the sensor
    sensorParams = dict(verbosity=_VERBOSITY)
    encoder = _createEncoder()
    trainFile = resource_filename("nupic.datafiles", "extra/gym/gym.csv")
    dataSource = FileRecordStream(streamID=trainFile)
    dataSource.setAutoRewind(True)

    # ==========================================================================
    # Now create the network itself
    n = Network()
    n.addRegion("sensor", "py.RecordSensor", json.dumps(sensorParams))

    sensor = n.regions['sensor'].getSelf()
    sensor.encoder = encoder
    sensor.dataSource = dataSource

    # ==========================================================================
    # Add the SP if requested
    if addSP:
        print "Adding SPRegion"
        g_spRegionConfig['inputWidth'] = encoder.getWidth()
        n.addRegion("level1SP", "py.SPRegion", json.dumps(g_spRegionConfig))

        n.link("sensor", "level1SP", "UniformLink", "")
        n.link("sensor",
               "level1SP",
               "UniformLink",
               "",
               srcOutput="resetOut",
               destInput="resetIn")
        n.link("level1SP",
               "sensor",
               "UniformLink",
               "",
               srcOutput="spatialTopDownOut",
               destInput="spatialTopDownIn")
        n.link("level1SP",
               "sensor",
               "UniformLink",
               "",
               srcOutput="temporalTopDownOut",
               destInput="temporalTopDownIn")

    # ==========================================================================
    if addTP and addSP:
        # Add the TP on top of SP if requested
        # The input width of the TP is set to the column count of the SP
        print "Adding TPRegion on top of SP"
        g_tpRegionConfig['inputWidth'] = g_spRegionConfig['columnCount']
        n.addRegion("level1TP", "py.TPRegion", json.dumps(g_tpRegionConfig))
        n.link("level1SP", "level1TP", "UniformLink", "")
        n.link("level1TP",
               "level1SP",
               "UniformLink",
               "",
               srcOutput="topDownOut",
               destInput="topDownIn")
        n.link("sensor",
               "level1TP",
               "UniformLink",
               "",
               srcOutput="resetOut",
               destInput="resetIn")

    elif addTP:
        # Add a lone TPRegion if requested
        # The input width of the TP is set to the encoder width
        print "Adding TPRegion"
        g_tpRegionConfig['inputWidth'] = encoder.getWidth()
        n.addRegion("level1TP", "py.TPRegion", json.dumps(g_tpRegionConfig))

        n.link("sensor", "level1TP", "UniformLink", "")
        n.link("sensor",
               "level1TP",
               "UniformLink",
               "",
               srcOutput="resetOut",
               destInput="resetIn")

    return n
Exemple #29
0
  def testSimpleMulticlassNetwork(self):
  
    # Setup data record stream of fake data (with three categories)
    filename = _getTempFileName()
    fields = [("timestamp", "datetime", "T"),
              ("value", "float", ""),
              ("reset", "int", "R"),
              ("sid", "int", "S"),
              ("categories", "list", "C")]
    records = (
      [datetime(day=1, month=3, year=2010), 0.0, 1, 0, ""],
      [datetime(day=2, month=3, year=2010), 1.0, 0, 0, "1 2"],
      [datetime(day=3, month=3, year=2010), 1.0, 0, 0, "1 2"],
      [datetime(day=4, month=3, year=2010), 2.0, 0, 0, "0"],
      [datetime(day=5, month=3, year=2010), 3.0, 0, 0, "1 2"],
      [datetime(day=6, month=3, year=2010), 5.0, 0, 0, "1 2"],
      [datetime(day=7, month=3, year=2010), 8.0, 0, 0, "0"],
      [datetime(day=8, month=3, year=2010), 13.0, 0, 0, "1 2"])
    dataSource = FileRecordStream(streamID=filename, write=True, fields=fields)
    for r in records:
      dataSource.appendRecord(list(r))

    # Create the network and get region instances.
    net = Network()
    net.addRegion("sensor", "py.RecordSensor", "{'numCategories': 3}")
    net.addRegion("classifier","py.KNNClassifierRegion",
                  "{'k': 2,'distThreshold': 0,'maxCategoryCount': 3}")
    net.link("sensor", "classifier", "UniformLink", "",
             srcOutput = "dataOut", destInput = "bottomUpIn")
    net.link("sensor", "classifier", "UniformLink", "",
             srcOutput = "categoryOut", destInput = "categoryIn")
    sensor = net.regions["sensor"]
    classifier = net.regions["classifier"]
    
    # Setup sensor region encoder and data stream.
    dataSource.close()
    dataSource = FileRecordStream(filename)
    sensorRegion = sensor.getSelf()
    sensorRegion.encoder = MultiEncoder()
    sensorRegion.encoder.addEncoder(
        "value", ScalarEncoder(21, 0.0, 13.0, n=256, name="value"))
    sensorRegion.dataSource = dataSource
    
    # Get ready to run.
    net.initialize()

    # Train the network (by default learning is ON in the classifier, but assert
    # anyway) and then turn off learning and turn on inference mode.
    self.assertEqual(classifier.getParameter("learningMode"), 1)
    net.run(8)
    classifier.setParameter("inferenceMode", 1)
    classifier.setParameter("learningMode", 0)

    # Assert learning is OFF and that the classifier learned the dataset.
    self.assertEqual(classifier.getParameter("learningMode"), 0,
        "Learning mode is not turned off.")
    self.assertEqual(classifier.getParameter("inferenceMode"), 1,
        "Inference mode is not turned on.")
    self.assertEqual(classifier.getParameter("categoryCount"), 3,
        "The classifier should count three total categories.")
    # classififer learns 12 patterns b/c there are 12 categories amongst the
    # records:
    self.assertEqual(classifier.getParameter("patternCount"), 12,
        "The classifier should've learned 12 samples in total.")

    # Test the network on the same data as it trained on; should classify with
    # 100% accuracy.
    expectedCats = ([0.0, 0.5, 0.5],
                    [0.0, 0.5, 0.5],
                    [0.0, 0.5, 0.5],
                    [0.5, 0.5, 0.0],
                    [0.0, 0.5, 0.5],
                    [0.0, 0.5, 0.5],
                    [0.5, 0.5, 0.0],
                    [0.0, 0.5, 0.5])
    dataSource.rewind()
    for i in xrange(8):
      net.run(1)
      inferredCats = classifier.getOutputData("categoriesOut")
      self.assertSequenceEqual(expectedCats[i], inferredCats.tolist(),
          "Classififer did not infer expected category probabilites for record "
          "number {}.".format(i))
    
    # Close data stream, delete file.
    dataSource.close()
    os.remove(filename)
def createNetwork():
    network = Network()

    #
    # Sensors
    #

    # C++
    consumptionSensor = network.addRegion(
        'consumptionSensor', 'ScalarSensor',
        json.dumps({
            'n': 120,
            'w': 21,
            'minValue': 0.0,
            'maxValue': 100.0,
            'clipInput': True
        }))

    # Python
    timestampSensor = network.addRegion("timestampSensor",
                                        'py.PluggableEncoderSensor', "")
    timestampSensor.getSelf().encoder = DateEncoder(timeOfDay=(21, 9.5),
                                                    name="timestamp_timeOfDay")

    #
    # Add a SPRegion, a region containing a spatial pooler
    #
    consumptionEncoderN = consumptionSensor.getParameter('n')
    timestampEncoderN = timestampSensor.getSelf().encoder.getWidth()
    inputWidth = consumptionEncoderN + timestampEncoderN

    network.addRegion(
        "sp", "py.SPRegion",
        json.dumps({
            "spatialImp": "cpp",
            "globalInhibition": 1,
            "columnCount": 2048,
            "inputWidth": inputWidth,
            "numActiveColumnsPerInhArea": 40,
            "seed": 1956,
            "potentialPct": 0.8,
            "synPermConnected": 0.1,
            "synPermActiveInc": 0.0001,
            "synPermInactiveDec": 0.0005,
            "boostStrength": 0.0,
        }))

    #
    # Input to the Spatial Pooler
    #
    network.link("consumptionSensor", "sp", "UniformLink", "")
    network.link("timestampSensor", "sp", "UniformLink", "")

    #
    # Add a TPRegion, a region containing a Temporal Memory
    #
    network.addRegion(
        "tm", "py.TMRegion",
        json.dumps({
            "columnCount": 2048,
            "cellsPerColumn": 32,
            "inputWidth": 2048,
            "seed": 1960,
            "temporalImp": "cpp",
            "newSynapseCount": 20,
            "maxSynapsesPerSegment": 32,
            "maxSegmentsPerCell": 128,
            "initialPerm": 0.21,
            "permanenceInc": 0.1,
            "permanenceDec": 0.1,
            "globalDecay": 0.0,
            "maxAge": 0,
            "minThreshold": 9,
            "activationThreshold": 12,
            "outputType": "normal",
            "pamLength": 3,
        }))

    network.link("sp", "tm", "UniformLink", "")
    network.link("tm",
                 "sp",
                 "UniformLink",
                 "",
                 srcOutput="topDownOut",
                 destInput="topDownIn")

    # Enable anomalyMode so the tm calculates anomaly scores
    network.regions['tm'].setParameter("anomalyMode", True)
    # Enable inference mode to be able to get predictions
    network.regions['tm'].setParameter("inferenceMode", True)

    return network
def _createNetwork(inverseReadoutResolution, anchorInputSize, dualPhase=False):
  """
  Create a simple network connecting sensor and motor inputs to the location
  region. Use :meth:`RawSensor.addDataToQueue` to add sensor input and growth
  candidates. Use :meth:`RawValues.addDataToQueue` to add motor input.
  ::
                        +----------+
    [   sensor*   ] --> |          | --> [     activeCells        ]
    [ candidates* ] --> | location | --> [    learnableCells      ]
    [    motor    ] --> |          | --> [ sensoryAssociatedCells ]
                        +----------+

  :param inverseReadoutResolution:
    Specifies the diameter of the circle of phases in the rhombus encoded by a
    bump.
  :type inverseReadoutResolution: int

  :type anchorInputSize: int
  :param anchorInputSize:
    The number of input bits in the anchor input.

  .. note::
    (*) This function will only add the 'sensor' and 'candidates' regions when
    'anchorInputSize' is greater than zero. This is useful if you would like to
    compute locations ignoring sensor input

  .. seealso::
     - :py:func:`htmresearch.frameworks.location.path_integration_union_narrowing.createRatModuleFromReadoutResolution`

  """
  net = Network()

  # Create simple region to pass motor commands as displacement vectors (dx, dy)
  net.addRegion("motor", "py.RawValues", json.dumps({
    "outputWidth": 2
  }))

  if anchorInputSize > 0:
    # Create simple region to pass growth candidates
    net.addRegion("candidates", "py.RawSensor", json.dumps({
      "outputWidth": anchorInputSize
    }))

    # Create simple region to pass sensor input
    net.addRegion("sensor", "py.RawSensor", json.dumps({
      "outputWidth": anchorInputSize
    }))

  # Initialize region with 5 modules varying scale by sqrt(2) and 4 different
  # random orientations for each scale
  scale = []
  orientation = []
  for i in xrange(5):
    for _ in xrange(4):
      angle = np.radians(random.gauss(7.5, 7.5))
      orientation.append(random.choice([angle, -angle]))
      scale.append(10.0 * (math.sqrt(2) ** i))

  # Create location region
  params = computeRatModuleParametersFromReadoutResolution(inverseReadoutResolution)
  params.update({
    "moduleCount": len(scale),
    "scale": scale,
    "orientation": orientation,
    "anchorInputSize": anchorInputSize,
    "activationThreshold": 8,
    "initialPermanence": 1.0,
    "connectedPermanence": 0.5,
    "learningThreshold": 8,
    "sampleSize": 10,
    "permanenceIncrement": 0.1,
    "permanenceDecrement": 0.0,
    "dualPhase": dualPhase,
    "bumpOverlapMethod": "probabilistic"
  })
  net.addRegion("location", "py.GridCellLocationRegion", json.dumps(params))

  if anchorInputSize > 0:
    # Link sensor
    net.link("sensor", "location", "UniformLink", "",
             srcOutput="dataOut", destInput="anchorInput")
    net.link("sensor", "location", "UniformLink", "",
             srcOutput="resetOut", destInput="resetIn")
    net.link("candidates", "location", "UniformLink", "",
             srcOutput="dataOut", destInput="anchorGrowthCandidates")

  # Link motor input
  net.link("motor", "location", "UniformLink", "",
           srcOutput="dataOut", destInput="displacement")

  # Initialize network objects
  net.initialize()

  return net
    def createNet(self):
        """ Set up the structure of the network """
        net = Network()

        Network.unregisterRegion(SaccadeSensor.__name__)
        Network.registerRegion(SaccadeSensor)
        Network.unregisterRegion(ExtendedTMRegion.__name__)
        Network.registerRegion(ExtendedTMRegion)
        Network.unregisterRegion(ColumnPoolerRegion.__name__)
        Network.registerRegion(ColumnPoolerRegion)

        imageSensorParams = copy.deepcopy(DEFAULT_IMAGESENSOR_PARAMS)
        if self.loggingDir is not None:
            imageSensorParams["logDir"] = "sensorImages/" + self.loggingDir
            imageSensorParams["logOutputImages"] = 1
            imageSensorParams["logOriginalImages"] = 1
            imageSensorParams["logFilteredImages"] = 1
            imageSensorParams["logLocationImages"] = 1
            imageSensorParams["logLocationOnOriginalImage"] = 1

        net.addRegion("sensor", "py.SaccadeSensor",
                      yaml.dump(imageSensorParams))
        sensor = net.regions["sensor"].getSelf()

        DEFAULT_SP_PARAMS["columnCount"] = sensor.getOutputElementCount(
            "dataOut")
        net.addRegion("SP", "py.SPRegion", yaml.dump(DEFAULT_SP_PARAMS))
        sp = net.regions["SP"].getSelf()

        DEFAULT_TM_PARAMS["columnDimensions"] = (
            sp.getOutputElementCount("bottomUpOut"), )
        DEFAULT_TM_PARAMS["basalInputWidth"] = sensor.getOutputElementCount(
            "saccadeOut")
        net.addRegion("TM", "py.ExtendedTMRegion",
                      yaml.dump(DEFAULT_TM_PARAMS))

        net.addRegion("TP", "py.ColumnPoolerRegion",
                      yaml.dump(DEFAULT_TP_PARAMS))

        net.addRegion("classifier", "py.KNNClassifierRegion",
                      yaml.dump(DEFAULT_CLASSIFIER_PARAMS))

        net.link("sensor",
                 "SP",
                 "UniformLink",
                 "",
                 srcOutput="dataOut",
                 destInput="bottomUpIn")
        net.link("SP",
                 "TM",
                 "UniformLink",
                 "",
                 srcOutput="bottomUpOut",
                 destInput="activeColumns")
        net.link("sensor",
                 "TM",
                 "UniformLink",
                 "",
                 srcOutput="saccadeOut",
                 destInput="basalInput")
        net.link("TM",
                 "TP",
                 "UniformLink",
                 "",
                 srcOutput="predictedActiveCells",
                 destInput="feedforwardInput")
        net.link("TP",
                 "TM",
                 "UniformLink",
                 "",
                 srcOutput="feedForwardOutput",
                 destInput="apicalInput")
        net.link("TP",
                 "classifier",
                 "UniformLink",
                 "",
                 srcOutput="feedForwardOutput",
                 destInput="bottomUpIn")
        #net.link("TM", "classifier", "UniformLink", "",
        #         srcOutput="predictedActiveCells", destInput="bottomUpIn")
        net.link("sensor",
                 "classifier",
                 "UniformLink",
                 "",
                 srcOutput="categoryOut",
                 destInput="categoryIn")

        self.net = net
        self.networkSensor = self.net.regions["sensor"]
        self.networkSP = self.net.regions["SP"]
        self.networkTM = self.net.regions["TM"]
        self.networkTP = self.net.regions["TP"]
        self.networkClassifier = self.net.regions["classifier"]
Exemple #33
0
def createNetwork(dataSource):
    """Create the Network instance.

  The network has a sensor region reading data from `dataSource` and passing
  the encoded representation to an SPRegion. The SPRegion output is passed to
  a TPRegion.

  :param dataSource: a RecordStream instance to get data from
  :returns: a Network instance ready to run
  """
    network = Network()

    # Our input is sensor data from the gym file. The RecordSensor region
    # allows us to specify a file record stream as the input source via the
    # dataSource attribute.
    network.addRegion("sensor", "py.RecordSensor", json.dumps({"verbosity": _VERBOSITY}))
    sensor = network.regions["sensor"].getSelf()
    # The RecordSensor needs to know how to encode the input values
    sensor.encoder = createEncoder()
    # Specify the dataSource as a file record stream instance
    sensor.dataSource = dataSource

    # Create the spatial pooler region
    SP_PARAMS["inputWidth"] = sensor.encoder.getWidth()
    network.addRegion("spatialPoolerRegion", "py.SPRegion", json.dumps(SP_PARAMS))

    # Link the SP region to the sensor input
    network.link("sensor", "spatialPoolerRegion", "UniformLink", "")
    network.link("sensor", "spatialPoolerRegion", "UniformLink", "", srcOutput="resetOut", destInput="resetIn")
    network.link(
        "spatialPoolerRegion", "sensor", "UniformLink", "", srcOutput="spatialTopDownOut", destInput="spatialTopDownIn"
    )
    network.link(
        "spatialPoolerRegion",
        "sensor",
        "UniformLink",
        "",
        srcOutput="temporalTopDownOut",
        destInput="temporalTopDownIn",
    )

    # Add the TPRegion on top of the SPRegion
    network.addRegion("temporalPoolerRegion", "py.TPRegion", json.dumps(TP_PARAMS))

    network.link("spatialPoolerRegion", "temporalPoolerRegion", "UniformLink", "")
    network.link(
        "temporalPoolerRegion", "spatialPoolerRegion", "UniformLink", "", srcOutput="topDownOut", destInput="topDownIn"
    )

    # Add the AnomalyRegion on top of the TPRegion
    network.addRegion("anomalyRegion", "py.AnomalyRegion", json.dumps({}))

    network.link(
        "spatialPoolerRegion", "anomalyRegion", "UniformLink", "", srcOutput="bottomUpOut", destInput="activeColumns"
    )
    network.link(
        "temporalPoolerRegion", "anomalyRegion", "UniformLink", "", srcOutput="topDownOut", destInput="predictedColumns"
    )

    spatialPoolerRegion = network.regions["spatialPoolerRegion"]

    # Make sure learning is enabled
    spatialPoolerRegion.setParameter("learningMode", True)
    # We want temporal anomalies so disable anomalyMode in the SP. This mode is
    # used for computing anomalies in a non-temporal model.
    spatialPoolerRegion.setParameter("anomalyMode", False)

    temporalPoolerRegion = network.regions["temporalPoolerRegion"]

    # Enable topDownMode to get the predicted columns output
    temporalPoolerRegion.setParameter("topDownMode", True)
    # Make sure learning is enabled (this is the default)
    temporalPoolerRegion.setParameter("learningMode", True)
    # Enable inference mode so we get predictions
    temporalPoolerRegion.setParameter("inferenceMode", True)
    # Enable anomalyMode to compute the anomaly score. This actually doesn't work
    # now so doesn't matter. We instead compute the anomaly score based on
    # topDownOut (predicted columns) and SP bottomUpOut (active columns).
    temporalPoolerRegion.setParameter("anomalyMode", True)

    return network
  def testTwoNode(self):
    # =====================================================
    # Build and run the network
    # =====================================================

    net = Network()
    level1 = net.addRegion("level1", "TestNode", "{int32Param: 15}")
    dims = Dimensions([6, 4])
    level1.setDimensions(dims)

    level2 = net.addRegion("level2", "TestNode", "{real64Param: 128.23}")

    net.link("level1", "level2", "TestFanIn2", "")

    # Could call initialize here, but not necessary as net.run()
    # initializes implicitly.
    # net.initialize()

    net.run(1)
    LOGGER.info("Successfully created network and ran for one iteration")

    # =====================================================
    # Check everything
    # =====================================================
    dims = level1.getDimensions()
    self.assertEquals(len(dims), 2)
    self.assertEquals(dims[0], 6)
    self.assertEquals(dims[1], 4)

    dims = level2.getDimensions()
    self.assertEquals(len(dims), 2)
    self.assertEquals(dims[0], 3)
    self.assertEquals(dims[1], 2)

    # Check L1 output. "False" means don't copy, i.e.
    # get a pointer to the actual output
    # Actual output values are determined by the TestNode
    # compute() behavior.
    l1output = level1.getOutputData("bottomUpOut")
    self.assertEquals(len(l1output), 48) # 24 nodes; 2 values per node
    for i in xrange(24):
      self.assertEquals(l1output[2*i], 0)      # size of input to each node is 0
      self.assertEquals(l1output[2*i+1], i)    # node number

    # check L2 output.
    l2output = level2.getOutputData("bottomUpOut", )
    self.assertEquals(len(l2output), 12) # 6 nodes; 2 values per node
    # Output val = node number + sum(inputs)
    # Can compute from knowing L1 layout
    #
    #  00 01 | 02 03 | 04 05
    #  06 07 | 08 09 | 10 11
    #  ---------------------
    #  12 13 | 14 15 | 16 17
    #  18 19 | 20 21 | 22 23
    outputVals = []
    outputVals.append(0 + (0 + 1 + 6 + 7))
    outputVals.append(1 + (2 + 3 + 8 + 9))
    outputVals.append(2 + (4 + 5 + 10 + 11))
    outputVals.append(3 + (12 + 13 + 18 + 19))
    outputVals.append(4 + (14 + 15 + 20 + 21))
    outputVals.append(5 + (16 + 17 + 22 + 23))
    for i in xrange(6):
      self.assertEquals(l2output[2*i], 8) # size of input for each node is 8
      self.assertEquals(l2output[2*i+1], outputVals[i])


    # =====================================================
    # Run for one more iteration
    # =====================================================
    LOGGER.info("Running for a second iteration")
    net.run(1)

    # =====================================================
    # Check everything again
    # =====================================================

    # Outputs are all the same except that the first output is
    # incremented by the iteration number
    for i in xrange(24):
      self.assertEquals(l1output[2*i], 1)
      self.assertEquals(l1output[2*i+1], i)

    for i in xrange(6):
      self.assertEquals(l2output[2*i], 9)
      self.assertEquals(l2output[2*i+1], outputVals[i] + 4)
def createNetwork():
  # c network: create Network instance
  network = Network()

  # --------------------------------------------------
  # Add sensors to network

  # c param_f_consumptionSensor: parameter for consumptionSensor
  param_f_consumptionSensor={
    'n': 120,
    'w': 21,
    'minValue': 0.0,
    'maxValue': 100.0,
    'clipInput': True}

  # c jparam_f_cs: json param for consumptionSensor
  jparam_f_cs=json.dumps(param_f_consumptionSensor)

  # C++
  # c consumptionSensor: add consumptionSensor region into network
  consumptionSensor = network.addRegion(
    'consumptionSensor', 'ScalarSensor', jparam_f_cs)

  # --------------------------------------------------
  # Python
  # c timestampSensor: add timestampSensor region into network
  timestampSensor = network.addRegion(
    "timestampSensor",'py.PluggableEncoderSensor', "")

  # c date_encoder: create date encoder
  date_encoder=DateEncoder(timeOfDay=(21, 9.5), name="timestamp_timeOfDay")

  # c date_encoder: assing date encoder into timestampSensor
  timestampSensor.getSelf().encoder = date_encoder

  # --------------------------------------------------
  # c consumptionEncoderN: get number of bits "n" from consumptionSensor
  consumptionEncoderN = consumptionSensor.getParameter('n')
  # print("consumptionEncoderN",consumptionEncoderN)
  # ('consumptionEncoderN', 120)

  # print("timestampSensor.getSelf()",timestampSensor.getSelf())
  # <nupic.regions.pluggable_encoder_sensor.PluggableEncoderSensor object at 0x7fa428bf31d0>

  # c encoder_of_tss: encoder of timestampSensor
  encoder_of_tss=timestampSensor.getSelf().encoder

  # c timestampEncoderN: width of encoder of timestampSensor
  timestampEncoderN = encoder_of_tss.getWidth()
  # print("timestampEncoderN",timestampEncoderN)
  # ('timestampEncoderN', 54)

  # c inputWidth: width of input
  inputWidth = consumptionEncoderN + timestampEncoderN
  # print("inputWidth",inputWidth)
  # ('inputWidth', 174)

  # --------------------------------------------------
  # c param_f_SP: parameter for spatial pooler
  param_f_SP={
    # c spatialImp: spatial pooler implementation in C++
    "spatialImp": "cpp",
    # c globalInhibition: 1 -> on
    "globalInhibition": 1,
    "columnCount": 2048,
    "inputWidth": inputWidth,
    # c numActiveColumnsPerInhArea: number of active columns per inhibition area
    "numActiveColumnsPerInhArea": 40,
    "seed": 1956,
    # c potentialPct: potential pool percent
    "potentialPct": 0.8,
    # c "synPermConnected: synaptic permanence connected
    "synPermConnected": 0.1,
    # c synPermActiveInc: synaptic permanence active increment
    "synPermActiveInc": 0.0001,
    # c synPermInactiveDec: synaptic permanence inactive decrement
    "synPermInactiveDec": 0.0005,
    "boostStrength": 0.0,}

  # c param_f_SP_j: parameter for spatial pooler in JSON
  param_f_SP_j=json.dumps(param_f_SP)

  # c Add "SPRegion" into network
  # SPRegion can contain spatial pooler
  network.addRegion("sp", "py.SPRegion", param_f_SP_j)

  # --------------------------------------------------
  # Link each configured one in network
  network.link("consumptionSensor", "sp", "UniformLink", "")
  network.link("timestampSensor", "sp", "UniformLink", "")

  # --------------------------------------------------
  # c param_f_TM: parameter for temporal memory learning algorithm
  param_f_TM={
    "columnCount": 2048,
    "cellsPerColumn": 32,
    "inputWidth": 2048,
    "seed": 1960,
    "temporalImp": "cpp",
    "newSynapseCount": 20,
    # c maxSynapsesPerSegment: maximum number of synapses per segment
    "maxSynapsesPerSegment": 32,
    # c maxSegmentsPerCell: maximum number of segments per cell
    "maxSegmentsPerCell": 128,
    # c initialPerm: initial permanence value for newly created synapses
    "initialPerm": 0.21,
    # c permanenceInc: active synapses get their permanence counts incremented by this value
    "permanenceInc": 0.1,
    # c permanenceDec: all other synapses get their permanence counts decremented by this value
    "permanenceDec": 0.1,
    "globalDecay": 0.0,
    "maxAge": 0,
    "minThreshold": 9,
    # c activationThreshold: if "number of active connected synapses" on segment is 
    # c activationThreshold: at least this threshold, segment is said to be active
    "activationThreshold": 12,
    "outputType": "normal",
    "pamLength": 3,}
  
  # c param_f_TM_j: parameter for temporal memory learning algorithm in JSON
  param_f_TM_j=json.dumps(param_f_TM)

  # Add TMRegion into network 
  # TMRegion is region containing "Temporal Memory Learning algorithm"
  network.addRegion("tm", "py.TMRegion", param_f_TM_j)

  # --------------------------------------------------
  network.link("sp", "tm", "UniformLink", "")
  network.link("tm", "sp", "UniformLink", "", srcOutput="topDownOut", destInput="topDownIn")

  # --------------------------------------------------
  # Enable anomalyMode so TM calculates anomaly scores
  network.regions['tm'].setParameter("anomalyMode", True)
  
  # Enable inference mode to be able to get predictions
  network.regions['tm'].setParameter("inferenceMode", True)

  return network
def createTemporalAnomaly_chemical(recordParams, spatialParams, temporalParams,
                                   verbosity):

    inputFilePath = recordParams["inputFilePath"]
    scalarEncoder1Args = recordParams["scalarEncoder1Args"]
    scalarEncoder2Args = recordParams["scalarEncoder2Args"]
    scalarEncoder3Args = recordParams["scalarEncoder3Args"]
    scalarEncoder4Args = recordParams["scalarEncoder4Args"]
    scalarEncoder5Args = recordParams["scalarEncoder5Args"]
    scalarEncoder6Args = recordParams["scalarEncoder6Args"]
    scalarEncoder7Args = recordParams["scalarEncoder7Args"]
    dateEncoderArgs = recordParams["dateEncoderArgs"]

    scalarEncoder1 = ScalarEncoder(**scalarEncoder1Args)
    scalarEncoder2 = ScalarEncoder(**scalarEncoder2Args)
    scalarEncoder3 = ScalarEncoder(**scalarEncoder3Args)
    scalarEncoder4 = ScalarEncoder(**scalarEncoder4Args)
    scalarEncoder5 = ScalarEncoder(**scalarEncoder5Args)
    scalarEncoder6 = ScalarEncoder(**scalarEncoder6Args)
    scalarEncoder7 = ScalarEncoder(**scalarEncoder7Args)
    dateEncoder = DateEncoder(**dateEncoderArgs)

    encoder = MultiEncoder()
    encoder.addEncoder(scalarEncoder1Args["name"], scalarEncoder1)
    encoder.addEncoder(scalarEncoder2Args["name"], scalarEncoder2)
    encoder.addEncoder(scalarEncoder3Args["name"], scalarEncoder3)
    encoder.addEncoder(scalarEncoder4Args["name"], scalarEncoder4)
    encoder.addEncoder(scalarEncoder5Args["name"], scalarEncoder5)
    encoder.addEncoder(scalarEncoder6Args["name"], scalarEncoder6)
    encoder.addEncoder(scalarEncoder7Args["name"], scalarEncoder7)
    encoder.addEncoder(dateEncoderArgs["name"], dateEncoder)

    network = Network()

    network.addRegion("sensor", "py.RecordSensor",
                      json.dumps({"verbosity": verbosity}))

    sensor = network.regions["sensor"].getSelf()
    sensor.encoder = encoder
    sensor.dataSource = FileRecordStream(streamID=inputFilePath)

    # Create the spatial pooler region
    spatialParams["inputWidth"] = sensor.encoder.getWidth()
    network.addRegion("spatialPoolerRegion", "py.SPRegion",
                      json.dumps(spatialParams))

    # Link the SP region to the sensor input
    network.link("sensor", "spatialPoolerRegion", "UniformLink", "")
    network.link("sensor",
                 "spatialPoolerRegion",
                 "UniformLink",
                 "",
                 srcOutput="resetOut",
                 destInput="resetIn")
    network.link("spatialPoolerRegion",
                 "sensor",
                 "UniformLink",
                 "",
                 srcOutput="spatialTopDownOut",
                 destInput="spatialTopDownIn")
    network.link("spatialPoolerRegion",
                 "sensor",
                 "UniformLink",
                 "",
                 srcOutput="temporalTopDownOut",
                 destInput="temporalTopDownIn")

    # Add the TPRegion on top of the SPRegion
    network.addRegion("temporalPoolerRegion", "py.TMRegion",
                      json.dumps(temporalParams))

    network.link("spatialPoolerRegion", "temporalPoolerRegion", "UniformLink",
                 "")
    network.link("temporalPoolerRegion",
                 "spatialPoolerRegion",
                 "UniformLink",
                 "",
                 srcOutput="topDownOut",
                 destInput="topDownIn")

    # Add the AnomalyLikelihoodRegion on top of the TMRegion
    network.addRegion("anomalyLikelihoodRegion", "py.AnomalyLikelihoodRegion",
                      json.dumps({}))
    network.link("temporalPoolerRegion",
                 "anomalyLikelihoodRegion",
                 "UniformLink",
                 "",
                 srcOutput="anomalyScore",
                 destInput="rawAnomalyScore")
    network.link("sensor",
                 "anomalyLikelihoodRegion",
                 "UniformLink",
                 "",
                 srcOutput="sourceOut",
                 destInput="metricValue")

    spatialPoolerRegion = network.regions["spatialPoolerRegion"]

    # Make sure learning is enabled
    spatialPoolerRegion.setParameter("learningMode", True)
    # We want temporal anomalies so disable anomalyMode in the SP. This mode is
    # used for computing anomalies in a non-temporal model.
    spatialPoolerRegion.setParameter("anomalyMode", False)

    temporalPoolerRegion = network.regions["temporalPoolerRegion"]

    # Enable topDownMode to get the predicted columns output
    temporalPoolerRegion.setParameter("topDownMode", True)
    # Make sure learning is enabled (this is the default)
    temporalPoolerRegion.setParameter("learningMode", True)
    # Enable inference mode so we get predictions
    temporalPoolerRegion.setParameter("inferenceMode", True)
    # Enable anomalyMode to compute the anomaly score.
    temporalPoolerRegion.setParameter("anomalyMode", True)

    return network
Exemple #37
0
def createTemporalAnomaly(recordParams, spatialParams=_SP_PARAMS,
                          temporalParams=_TP_PARAMS,
                          verbosity=_VERBOSITY):
  """Generates a Network with connected RecordSensor, SP, TP.

  This function takes care of generating regions and the canonical links.
  The network has a sensor region reading data from a specified input and
  passing the encoded representation to an SPRegion.
  The SPRegion output is passed to a TPRegion.

  Note: this function returns a network that needs to be initialized. This
  allows the user to extend the network by adding further regions and
  connections.

  :param recordParams: a dict with parameters for creating RecordSensor region.
  :param spatialParams: a dict with parameters for creating SPRegion.
  :param temporalParams: a dict with parameters for creating TPRegion.
  :param verbosity: an integer representing how chatty the network will be.
  """
  inputFilePath= recordParams["inputFilePath"]
  scalarEncoderArgs = recordParams["scalarEncoderArgs"]
  dateEncoderArgs = recordParams["dateEncoderArgs"]

  scalarEncoder = ScalarEncoder(**scalarEncoderArgs)
  dateEncoder = DateEncoder(**dateEncoderArgs)

  encoder = MultiEncoder()
  encoder.addEncoder(scalarEncoderArgs["name"], scalarEncoder)
  encoder.addEncoder(dateEncoderArgs["name"], dateEncoder)

  network = Network()

  network.addRegion("sensor", "py.RecordSensor",
                    json.dumps({"verbosity": verbosity}))

  sensor = network.regions["sensor"].getSelf()
  sensor.encoder = encoder
  sensor.dataSource = FileRecordStream(streamID=inputFilePath)

  # Create the spatial pooler region
  spatialParams["inputWidth"] = sensor.encoder.getWidth()
  network.addRegion("spatialPoolerRegion", "py.SPRegion",
                    json.dumps(spatialParams))

  # Link the SP region to the sensor input
  network.link("sensor", "spatialPoolerRegion", "UniformLink", "")
  network.link("sensor", "spatialPoolerRegion", "UniformLink", "",
               srcOutput="resetOut", destInput="resetIn")
  network.link("spatialPoolerRegion", "sensor", "UniformLink", "",
               srcOutput="spatialTopDownOut", destInput="spatialTopDownIn")
  network.link("spatialPoolerRegion", "sensor", "UniformLink", "",
               srcOutput="temporalTopDownOut", destInput="temporalTopDownIn")

  # Add the TPRegion on top of the SPRegion
  network.addRegion("temporalPoolerRegion", "py.TPRegion",
                    json.dumps(temporalParams))

  network.link("spatialPoolerRegion", "temporalPoolerRegion", "UniformLink", "")
  network.link("temporalPoolerRegion", "spatialPoolerRegion", "UniformLink", "",
               srcOutput="topDownOut", destInput="topDownIn")


  spatialPoolerRegion = network.regions["spatialPoolerRegion"]

  # Make sure learning is enabled
  spatialPoolerRegion.setParameter("learningMode", True)
  # We want temporal anomalies so disable anomalyMode in the SP. This mode is
  # used for computing anomalies in a non-temporal model.
  spatialPoolerRegion.setParameter("anomalyMode", False)

  temporalPoolerRegion = network.regions["temporalPoolerRegion"]

  # Enable topDownMode to get the predicted columns output
  temporalPoolerRegion.setParameter("topDownMode", True)
  # Make sure learning is enabled (this is the default)
  temporalPoolerRegion.setParameter("learningMode", True)
  # Enable inference mode so we get predictions
  temporalPoolerRegion.setParameter("inferenceMode", True)
  # Enable anomalyMode to compute the anomaly score.
  temporalPoolerRegion.setParameter("anomalyMode", True)

  return network
Exemple #38
0
    def myCreateNetwork(self, networkConfig):

        suffix = '_0'
        network = Network()

        sensorInputName = "sensorInput" + suffix
        L4ColumnName = "L4Column" + suffix
        L2ColumnName = "L2Column" + suffix

        L4Params = copy.deepcopy(networkConfig["L4Params"])
        L4Params["apicalInputWidth"] = networkConfig["L2Params"]["cellCount"]

        network.addRegion(
            sensorInputName, "py.RawSensor",
            json.dumps({"outputWidth": networkConfig["sensorInputSize"]}))

        network.addRegion(L4ColumnName, networkConfig["L4RegionType"],
                          json.dumps(L4Params))
        network.addRegion(L2ColumnName, "py.ColumnPoolerRegion",
                          json.dumps(networkConfig["L2Params"]))

        network.setPhases(sensorInputName, [0])

        # L4 and L2 regions always have phases 2 and 3, respectively
        network.setPhases(L4ColumnName, [2])
        network.setPhases(L2ColumnName, [3])

        network.link(sensorInputName,
                     L4ColumnName,
                     "UniformLink",
                     "",
                     srcOutput="dataOut",
                     destInput="activeColumns")

        # Link L4 to L2
        network.link(L4ColumnName,
                     L2ColumnName,
                     "UniformLink",
                     "",
                     srcOutput="activeCells",
                     destInput="feedforwardInput")
        network.link(L4ColumnName,
                     L2ColumnName,
                     "UniformLink",
                     "",
                     srcOutput="winnerCells",
                     destInput="feedforwardGrowthCandidates")

        # Link L2 feedback to L4
        network.link(L2ColumnName,
                     L4ColumnName,
                     "UniformLink",
                     "",
                     srcOutput="feedForwardOutput",
                     destInput="apicalInput",
                     propagationDelay=1)

        # Link reset output to L2 and L4.
        network.link(sensorInputName,
                     L2ColumnName,
                     "UniformLink",
                     "",
                     srcOutput="resetOut",
                     destInput="resetIn")
        network.link(sensorInputName,
                     L4ColumnName,
                     "UniformLink",
                     "",
                     srcOutput="resetOut",
                     destInput="resetIn")

        #enableProfiling(network)
        for region in network.regions.values():
            region.enableProfiling()
        return network
Exemple #39
0
def createTemporalAnomaly(recordParams,
                          spatialParams=_SP_PARAMS,
                          temporalParams=_TP_PARAMS,
                          verbosity=_VERBOSITY):
    """Generates a Network with connected RecordSensor, SP, TP.

  This function takes care of generating regions and the canonical links.
  The network has a sensor region reading data from a specified input and
  passing the encoded representation to an SPRegion.
  The SPRegion output is passed to a TPRegion.

  Note: this function returns a network that needs to be initialized. This
  allows the user to extend the network by adding further regions and
  connections.

  :param recordParams: a dict with parameters for creating RecordSensor region.
  :param spatialParams: a dict with parameters for creating SPRegion.
  :param temporalParams: a dict with parameters for creating TPRegion.
  :param verbosity: an integer representing how chatty the network will be.
  """
    inputFilePath = recordParams["inputFilePath"]
    scalarEncoderArgs = recordParams["scalarEncoderArgs"]
    dateEncoderArgs = recordParams["dateEncoderArgs"]

    scalarEncoder = ScalarEncoder(**scalarEncoderArgs)
    dateEncoder = DateEncoder(**dateEncoderArgs)

    encoder = MultiEncoder()
    encoder.addEncoder(scalarEncoderArgs["name"], scalarEncoder)
    encoder.addEncoder(dateEncoderArgs["name"], dateEncoder)

    network = Network()

    network.addRegion("sensor", "py.RecordSensor",
                      json.dumps({"verbosity": verbosity}))

    sensor = network.regions["sensor"].getSelf()
    sensor.encoder = encoder
    sensor.dataSource = FileRecordStream(streamID=inputFilePath)

    # Create the spatial pooler region
    spatialParams["inputWidth"] = sensor.encoder.getWidth()
    network.addRegion("spatialPoolerRegion", "py.SPRegion",
                      json.dumps(spatialParams))

    # Link the SP region to the sensor input
    network.link("sensor", "spatialPoolerRegion", "UniformLink", "")
    network.link("sensor",
                 "spatialPoolerRegion",
                 "UniformLink",
                 "",
                 srcOutput="resetOut",
                 destInput="resetIn")
    network.link("spatialPoolerRegion",
                 "sensor",
                 "UniformLink",
                 "",
                 srcOutput="spatialTopDownOut",
                 destInput="spatialTopDownIn")
    network.link("spatialPoolerRegion",
                 "sensor",
                 "UniformLink",
                 "",
                 srcOutput="temporalTopDownOut",
                 destInput="temporalTopDownIn")

    # Add the TPRegion on top of the SPRegion
    network.addRegion("temporalPoolerRegion", "py.TPRegion",
                      json.dumps(temporalParams))

    network.link("spatialPoolerRegion", "temporalPoolerRegion", "UniformLink",
                 "")
    network.link("temporalPoolerRegion",
                 "spatialPoolerRegion",
                 "UniformLink",
                 "",
                 srcOutput="topDownOut",
                 destInput="topDownIn")

    spatialPoolerRegion = network.regions["spatialPoolerRegion"]

    # Make sure learning is enabled
    spatialPoolerRegion.setParameter("learningMode", True)
    # We want temporal anomalies so disable anomalyMode in the SP. This mode is
    # used for computing anomalies in a non-temporal model.
    spatialPoolerRegion.setParameter("anomalyMode", False)

    temporalPoolerRegion = network.regions["temporalPoolerRegion"]

    # Enable topDownMode to get the predicted columns output
    temporalPoolerRegion.setParameter("topDownMode", True)
    # Make sure learning is enabled (this is the default)
    temporalPoolerRegion.setParameter("learningMode", True)
    # Enable inference mode so we get predictions
    temporalPoolerRegion.setParameter("inferenceMode", True)
    # Enable anomalyMode to compute the anomaly score.
    temporalPoolerRegion.setParameter("anomalyMode", True)

    return network
def createNetwork(dataSource):
  """Create the Network instance.

  :param dataSource: a RecordStream instance to get data from
  :returns: a Network instance ready to run
  """
  
  network = Network()

  #----- SENSOR REGION -----#
  
  # Input data comes from a CSV file (scalar values, labels). The RecordSensor region
  # allows us to specify a file record stream as the input source via the
  # dataSource attribute.
  network.addRegion("sensor", "py.RecordSensor",
                    json.dumps({"verbosity": _VERBOSITY}))
  sensor = network.regions["sensor"].getSelf()

  # The RecordSensor needs to know how to encode the input values
  sensor.encoder = createScalarEncoder()

  # Specify the dataSource as a file record stream instance
  sensor.dataSource = dataSource

  # Region width
  prevRegionWidth = sensor.encoder.getWidth()


  #----- SPATIAL POOLER -----#

  # Create the spatial pooler region
  SP_PARAMS["inputWidth"] = prevRegionWidth
  network.addRegion("SP", "py.SPRegion", json.dumps(SP_PARAMS))

  # Link the SP region to the sensor input
  network.link("sensor", "SP", "UniformLink", "")
  
  # Forward the sensor region sequence reset to the SP
  network.link("sensor", "SP", "UniformLink", "", srcOutput="resetOut", destInput="resetIn")
  
  # Make sure learning is ON
  spatialPoolerRegion = network.regions["SP"]
  spatialPoolerRegion.setParameter("learningMode", True)
  
  # Inference mode outputs the current inference (e.g. active columns). 
  # It's ok to always leave inference mode on - it's only there for some corner cases.
  spatialPoolerRegion.setParameter('inferenceMode', True)

  # Region width
  prevRegionWidth = SP_PARAMS['columnCount']

  
  #----- TEMPORAL MEMORY -----#
  
  # Make sure region widths fit
  assert TM_PARAMS['columnCount'] == prevRegionWidth
  TM_PARAMS['inputWidth'] = TM_PARAMS['columnCount']
  
  # Create the TM region
  network.addRegion("TM", "py.TPRegion", json.dumps(TM_PARAMS))

  # Feed forward link from SP to TM
  network.link("SP", "TM", "UniformLink", "", srcOutput="bottomUpOut", destInput="bottomUpIn")
  
  # Feedback links (unnecessary ?)
  network.link("TM", "SP", "UniformLink", "", srcOutput="topDownOut", destInput="topDownIn")
  network.link("TM", "sensor", "UniformLink", "", srcOutput="topDownOut", destInput="temporalTopDownIn")

  # Forward the sensor region sequence reset to the TM
  network.link("sensor", "TM", "UniformLink", "", srcOutput="resetOut", destInput="resetIn")

  # Make sure learning is enabled (this is the default)
  temporalMemoryRegion = network.regions["TM"]
  temporalMemoryRegion.setParameter("learningMode", False)
  
  # Inference mode outputs the current inference (e.g. active cells). 
  # It's ok to always leave inference mode on - it's only there for some corner cases.
  temporalMemoryRegion.setParameter('inferenceMode', True)
  
  # Region width
  prevRegionWidth = TM_PARAMS['inputWidth']


  #----- CLASSIFIER REGION -----#

  # create classifier region
  network.addRegion('classifier', 'py.CLAClassifierRegion', json.dumps(CLA_CLASSIFIER_PARAMS))

  # feed the TM states to the classifier
  network.link("TM", "classifier", "UniformLink", "", srcOutput = "bottomUpOut", destInput = "bottomUpIn")
  
  
  # create a link from the sensor to the classifier to send in category labels.
  # TODO: this link is actually useless right now because the CLAclassifier region compute() function doesn't work
  # and that we are feeding TM states & categories manually to the classifier via the customCompute() function. 
  network.link("sensor", "classifier", "UniformLink", "", srcOutput = "categoryOut", destInput = "categoryIn")

  # disable learning for now (will be enables in a later training phase)
  classifier =  network.regions["classifier"]
  classifier.setParameter('learningMode', False)

  # Inference mode outputs the current inference. We can always leave it on.
  classifier.setParameter('inferenceMode', True)

  

  #------ INITIALIZE -----#  
  
  # The network until you try to run it. Make sure it's initialized right away.
  network.initialize()

  return network
Exemple #41
0
def _createOPFNetwork(addSP = True, addTP = False):
  """Create a 'new-style' network ala OPF and return it.
  If addSP is true, an SPRegion will be added named 'level1SP'.
  If addTP is true, a TPRegion will be added named 'level1TP'
  """

  # ==========================================================================
  # Create the encoder and data source stuff we need to configure the sensor
  sensorParams = dict(verbosity = _VERBOSITY)
  encoder = _createEncoder()
  trainFile = resource_filename("nupic.datafiles", "extra/gym/gym.csv")
  dataSource = FileRecordStream(streamID=trainFile)
  dataSource.setAutoRewind(True)

  # ==========================================================================
  # Now create the network itself
  n = Network()
  n.addRegion("sensor", "py.RecordSensor", json.dumps(sensorParams))

  sensor = n.regions['sensor'].getSelf()
  sensor.encoder = encoder
  sensor.dataSource = dataSource

  # ==========================================================================
  # Add the SP if requested
  if addSP:
    print "Adding SPRegion"
    g_spRegionConfig['inputWidth'] = encoder.getWidth()
    n.addRegion("level1SP", "py.SPRegion", json.dumps(g_spRegionConfig))

    n.link("sensor", "level1SP", "UniformLink", "")
    n.link("sensor", "level1SP", "UniformLink", "",
           srcOutput="resetOut", destInput="resetIn")
    n.link("level1SP", "sensor", "UniformLink", "",
           srcOutput="spatialTopDownOut", destInput="spatialTopDownIn")
    n.link("level1SP", "sensor", "UniformLink", "",
           srcOutput="temporalTopDownOut", destInput="temporalTopDownIn")

  # ==========================================================================
  if addTP and addSP:
    # Add the TP on top of SP if requested
    # The input width of the TP is set to the column count of the SP
    print "Adding TPRegion on top of SP"
    g_tpRegionConfig['inputWidth'] = g_spRegionConfig['columnCount']
    n.addRegion("level1TP", "py.TPRegion", json.dumps(g_tpRegionConfig))
    n.link("level1SP", "level1TP", "UniformLink", "")
    n.link("level1TP", "level1SP", "UniformLink", "",
           srcOutput="topDownOut", destInput="topDownIn")
    n.link("sensor", "level1TP", "UniformLink", "",
           srcOutput="resetOut", destInput="resetIn")

  elif addTP:
    # Add a lone TPRegion if requested
    # The input width of the TP is set to the encoder width
    print "Adding TPRegion"
    g_tpRegionConfig['inputWidth'] = encoder.getWidth()
    n.addRegion("level1TP", "py.TPRegion", json.dumps(g_tpRegionConfig))

    n.link("sensor", "level1TP", "UniformLink", "")
    n.link("sensor", "level1TP", "UniformLink", "",
           srcOutput="resetOut", destInput="resetIn")

  return n
Exemple #42
0
class HTMusicModel(object):
    def __init__(self, model_params):
        # Init an HTM network
        self.network = Network()

        # Getting parameters for network regions
        self.sensor_params = model_params['Sensor']
        self.spatial_pooler_params = model_params['SpatialPooler']
        self.temporal_memory_params = model_params['TemporalMemory']
        self.classifiers_params = model_params['Classifiers']
        self.encoders_params = model_params['Encoders']

        # Adding regions to HTM network
        self.network.addRegion('DurationEncoder', 'ScalarSensor',
                               json.dumps(self.encoders_params['duration']))
        self.network.addRegion('VelocityEncoder', 'ScalarSensor',
                               json.dumps(self.encoders_params['pitch']))
        self.network.addRegion('PitchEncoder', 'ScalarSensor',
                               json.dumps(self.encoders_params['velocity']))

        self.network.addRegion('SpatialPooler', 'py.SPRegion',
                               json.dumps(self.spatial_pooler_params))
        self.network.addRegion('TemporalMemory', 'py.TMRegion',
                               json.dumps(self.temporal_memory_params))

        # Creating outer classifiers for multifield prediction
        dclp = self.classifiers_params['duration']
        vclp = self.classifiers_params['pitch']
        pclp = self.classifiers_params['velocity']

        self.duration_classifier = SDRClassifier(
            steps=(1, ),
            verbosity=dclp['verbosity'],
            alpha=dclp['alpha'],
            actValueAlpha=dclp['actValueAlpha'])
        self.velocity_classifier = SDRClassifier(
            steps=(1, ),
            verbosity=vclp['verbosity'],
            alpha=vclp['alpha'],
            actValueAlpha=vclp['actValueAlpha'])
        self.pitch_classifier = SDRClassifier(
            steps=(1, ),
            verbosity=pclp['verbosity'],
            alpha=pclp['alpha'],
            actValueAlpha=pclp['actValueAlpha'])

        self._link_all_regions()
        self._enable_learning()
        self._enable_inference()

        self.network.initialize()

    def _link_all_regions(self):
        # Linking regions
        self.network.link('DurationEncoder', 'SpatialPooler', 'UniformLink',
                          '')
        self.network.link('VelocityEncoder', 'SpatialPooler', 'UniformLink',
                          '')
        self.network.link('PitchEncoder', 'SpatialPooler', 'UniformLink', '')
        self.network.link('SpatialPooler',
                          'TemporalMemory',
                          'UniformLink',
                          '',
                          srcOutput='bottomUpOut',
                          destInput='bottomUpIn')

    def _enable_learning(self):
        # Enable learning for all regions.
        self.network.regions["SpatialPooler"].setParameter("learningMode", 1)
        self.network.regions["TemporalMemory"].setParameter("learningMode", 1)

    def _enable_inference(self):
        # Enable inference for all regions.
        self.network.regions["SpatialPooler"].setParameter("inferenceMode", 1)
        self.network.regions["TemporalMemory"].setParameter("inferenceMode", 1)

    def train(self, duration, pitch, velocity):
        records_total = self.network.regions['SpatialPooler'].getSelf(
        ).getAlgorithmInstance().getIterationNum()

        self.network.regions['DurationEncoder'].setParameter(
            'sensedValue', duration)
        self.network.regions['PitchEncoder'].setParameter('sensedValue', pitch)
        self.network.regions['VelocityEncoder'].setParameter(
            'sensedValue', velocity)
        self.network.run(1)

        # Getting active cells of TM and bucket indicies of encoders to feed classifiers
        active_cells = numpy.array(
            self.network.regions['TemporalMemory'].getOutputData(
                'bottomUpOut')).nonzero()[0]
        duration_bucket = numpy.array(
            self.network.regions['DurationEncoder'].getOutputData('bucket'))
        pitch_bucket = numpy.array(
            self.network.regions['PitchEncoder'].getOutputData('bucket'))
        velocity_bucket = numpy.array(
            self.network.regions['VelocityEncoder'].getOutputData('bucket'))

        duration_classifier_result = self.duration_classifier.compute(
            recordNum=records_total,
            patternNZ=active_cells,
            classification={
                'bucketIdx': duration_bucket[0],
                'actValue': duration
            },
            learn=True,
            infer=False)

        pitch_classifier_result = self.pitch_classifier.compute(
            recordNum=records_total,
            patternNZ=active_cells,
            classification={
                'bucketIdx': pitch_bucket[0],
                'actValue': pitch
            },
            learn=True,
            infer=False)

        velocity_classifier_result = self.velocity_classifier.compute(
            recordNum=records_total,
            patternNZ=active_cells,
            classification={
                'bucketIdx': velocity_bucket[0],
                'actValue': velocity
            },
            learn=True,
            infer=False)

    def generate(self, seed, output_dir, event_amount):
        records_total = self.network.regions['SpatialPooler'].getSelf(
        ).getAlgorithmInstance().getIterationNum()

        seed = seed

        midi = pretty_midi.PrettyMIDI()
        midi_program = pretty_midi.instrument_name_to_program(
            'Acoustic Grand Piano')
        piano = pretty_midi.Instrument(program=midi_program)
        clock = 0
        for iters in tqdm(range(records_total, records_total + event_amount)):
            duration = seed[0]
            pitch = seed[1]
            velocity = seed[2]

            self.network.regions['DurationEncoder'].setParameter(
                'sensedValue', duration)
            self.network.regions['PitchEncoder'].setParameter(
                'sensedValue', pitch)
            self.network.regions['VelocityEncoder'].setParameter(
                'sensedValue', velocity)
            self.network.run(1)

            # Getting active cells of TM and bucket indicies of encoders to feed classifiers
            active_cells = numpy.array(
                self.network.regions['TemporalMemory'].getOutputData(
                    'bottomUpOut')).nonzero()[0]

            duration_bucket = numpy.array(
                self.network.regions['DurationEncoder'].getOutputData(
                    'bucket'))

            pitch_bucket = numpy.array(
                self.network.regions['PitchEncoder'].getOutputData('bucket'))

            velocity_bucket = numpy.array(
                self.network.regions['VelocityEncoder'].getOutputData(
                    'bucket'))

            # Getting up classifiers result

            duration_classifier_result = self.duration_classifier.compute(
                recordNum=records_total,
                patternNZ=active_cells,
                classification={
                    'bucketIdx': duration_bucket[0],
                    'actValue': duration
                },
                learn=False,
                infer=True)

            pitch_classifier_result = self.pitch_classifier.compute(
                recordNum=records_total,
                patternNZ=active_cells,
                classification={
                    'bucketIdx': pitch_bucket[0],
                    'actValue': pitch
                },
                learn=False,
                infer=True)

            velocity_classifier_result = self.velocity_classifier.compute(
                recordNum=records_total,
                patternNZ=active_cells,
                classification={
                    'bucketIdx': velocity_bucket[0],
                    'actValue': velocity
                },
                learn=False,
                infer=True)

            du = duration_classifier_result[1].argmax()
            pi = pitch_classifier_result[1].argmax()
            ve = velocity_classifier_result[1].argmax()

            duration_top_probs = duration_classifier_result[1][
                0:2] / duration_classifier_result[1][0:2].sum()

            predicted_duration = duration_classifier_result['actualValues'][du]

            # predicted_duration = duration_classifier_result['actualValues'][du]
            predicted_pitch = pitch_classifier_result['actualValues'][pi]
            predicted_velocity = velocity_classifier_result['actualValues'][ve]

            # print duration_classifier_result

            note = pretty_midi.Note(velocity=int(predicted_velocity),
                                    pitch=int(predicted_pitch),
                                    start=float(clock),
                                    end=float(clock + predicted_duration))

            piano.notes.append(note)

            clock = clock + 0.25

            seed[0] = predicted_duration
            seed[1] = predicted_pitch
            seed[2] = predicted_velocity

        midi.instruments.append(piano)
        midi.remove_invalid_notes()
        time = datetime.datetime.now().strftime('%Y-%m-%d %H:%m:%S')
        midi.write(output_dir + time + '.mid')

    def load_model(self, load_path):

        # Loading SpatialPooler
        print 'Loading SpatialPooler'
        with open(load_path + 'sp.bin', 'rb') as sp:
            sp_builder = SpatialPoolerProto.read(
                sp, traversal_limit_in_words=2**61)
        self.network.regions['SpatialPooler'].getSelf(
        )._sfdr = self.network.regions['SpatialPooler'].getSelf()._sfdr.read(
            sp_builder)

        # Loading TemporalMemory
        print 'Loading TemporalMemory'
        self.network.regions['TemporalMemory'].getSelf().getAlgorithmInstance(
        ).loadFromFile(load_path + 'tm.bin')

        # Loading end classifier
        print 'Loading duration classifier'
        with open(load_path + 'dcl.bin', 'rb') as dcl:
            dcl_builder = SdrClassifierProto.read(
                dcl, traversal_limit_in_words=2**61)
        self.duration_classifier = self.duration_classifier.read(dcl_builder)

        # Loading pitch classifier
        print 'Loading pitch classifier'
        with open(load_path + 'pcl.bin', 'rb') as pcl:
            pcl_builder = SdrClassifierProto.read(
                pcl, traversal_limit_in_words=2**61)
        self.pitch_classifier = self.pitch_classifier.read(pcl_builder)

        # Loading velocity classifier
        print 'Loading velocity classifier'
        with open(load_path + 'vcl.bin', 'rb') as vcl:
            vcl_builder = SdrClassifierProto.read(
                vcl, traversal_limit_in_words=2**61)
        self.velocity_classifier = self.velocity_classifier.read(vcl_builder)

    def save_model(self, save_path):

        # Saving SpatialPooler
        print 'Saving SpatialPooler'
        sp_builder = SpatialPoolerProto.new_message()
        self.network.regions['SpatialPooler'].getSelf().getAlgorithmInstance(
        ).write(sp_builder)
        with open(save_path + 'sp.bin', 'w+b') as sp:
            sp_builder.write(sp)

        # Saving TemporalMemory
        print 'Saving TemporalMemory'
        self.network.regions['TemporalMemory'].getSelf().getAlgorithmInstance(
        ).saveToFile(save_path + 'tm.bin')

        # Saving end classifier
        print 'Saving duration classifier'
        dcl_builder = SdrClassifierProto.new_message()
        self.duration_classifier.write(dcl_builder)
        with open(save_path + 'dcl.bin', 'w+b') as dcl:
            dcl_builder.write(dcl)

        # Saving pitch classifier
        print 'Saving pitch classifier'
        pcl_builder = SdrClassifierProto.new_message()
        self.pitch_classifier.write(pcl_builder)
        with open(save_path + 'pcl.bin', 'w+b') as pcl:
            pcl_builder.write(pcl)

        # Saving velocity classifier
        print 'Saving velocity classifier'
        vcl_builder = SdrClassifierProto.new_message()
        self.velocity_classifier.write(vcl_builder)
        with open(save_path + 'vcl.bin', 'w+b') as vcl:
            vcl_builder.write(vcl)
    def testSerialization(self):
        n = Network()

        imageDims = (42, 38)
        params = dict(width=imageDims[0],
                      height=imageDims[1],
                      mode="bw",
                      background=1,
                      invertOutput=1)

        sensor = n.addRegion("sensor", "py.ImageSensor", json.dumps(params))
        sensor.setDimensions(Dimensions(imageDims[0], imageDims[1]))

        params = dict(inputShape=imageDims,
                      coincidencesShape=imageDims,
                      disableTemporal=1,
                      tpSeed=43,
                      spSeed=42,
                      nCellsPerCol=1)

        l1 = n.addRegion("l1", "py.CLARegion", json.dumps(params))

        params = dict(maxCategoryCount=48,
                      SVDSampleCount=400,
                      SVDDimCount=5,
                      distanceNorm=0.6)

        _classifier = n.addRegion("classifier", "py.KNNClassifierRegion",
                                  json.dumps(params))

        # TODO: link params should not be required. Dest region dimensions are
        # already specified as [1]
        params = dict(mapping="in", rfSize=imageDims)

        n.link("sensor", "l1", "UniformLink", json.dumps(params))
        n.link("l1", "classifier", "UniformLink", "", "bottomUpOut",
               "bottomUpIn")
        n.link("sensor", "classifier", "UniformLink", "", "categoryOut",
               "categoryIn")
        n.initialize()

        n.save("fdr.nta")

        # Make sure the network bundle has all the expected files
        self.assertTrue(os.path.exists("fdr.nta/network.yaml"))
        self.assertTrue(os.path.exists("fdr.nta/R0-pkl"))
        self.assertTrue(os.path.exists("fdr.nta/R1-pkl"))
        self.assertTrue(os.path.exists("fdr.nta/R2-pkl"))

        n2 = Network("fdr.nta")
        n2.initialize()  # should not fail

        # Make sure the network is actually the same
        sensor = n2.regions['sensor']
        self.assertEqual(sensor.type, "py.ImageSensor")
        # would like to directly compare, but can't -- NPC-6
        self.assertEqual(str(sensor.dimensions), str(Dimensions(42, 38)))
        self.assertEqual(sensor.getParameter("width"), 42)
        self.assertEqual(sensor.getParameter("height"), 38)
        self.assertEqual(sensor.getParameter("mode"), "bw")
        self.assertEqual(sensor.getParameter("background"), 1)
        self.assertEqual(sensor.getParameter("invertOutput"), 1)

        l1 = n2.regions['l1']
        self.assertEqual(l1.type, "py.CLARegion")
        self.assertEqual(str(l1.dimensions), str(Dimensions(1)))
        a = l1.getParameter("inputShape")
        self.assertEqual(len(a), 2)
        self.assertEqual(a[0], 42)
        self.assertEqual(a[1], 38)

        a = l1.getParameter("coincidencesShape")
        self.assertEqual(len(a), 2)
        self.assertEqual(a[0], 42)
        self.assertEqual(a[1], 38)

        self.assertEqual(l1.getParameter("disableTemporal"), 1)
        self.assertEqual(l1.getParameter("spSeed"), 42)
        self.assertEqual(l1.getParameter("tpSeed"), 43)

        cl = n2.regions['classifier']
        self.assertEqual(cl.type, "py.KNNClassifierRegion")
        self.assertEqual(cl.getParameter("maxCategoryCount"), 48)
        self.assertEqual(cl.getParameter("SVDSampleCount"), 400)
        self.assertEqual(cl.getParameter("SVDDimCount"), 5)
        self.assertLess((cl.getParameter("distanceNorm") - 0.6), 0.0001)
        self.assertEqual(str(cl.dimensions), str(Dimensions(1)))

        n2.save("fdr2.nta")

        # now compare the two network bundles -- should be the same
        c = filecmp.dircmp("fdr.nta", "fdr2.nta")
        self.assertEqual(len(c.left_only), 0,
                         "fdr.nta has extra files: %s" % c.left_only)

        self.assertEqual(len(c.right_only), 0,
                         "fdr2.nta has extra files: %s" % c.right_only)

        if len(c.diff_files) > 0:
            _LOGGER.warn(
                "Some bundle files differ: %s\n"
                "This is expected, as pickle.load() followed by "
                "pickle.dump() doesn't produce the same file", c.diff_files)
Exemple #44
0
  def testSimpleImageNetwork(self):

    # Create the network and get region instances
    net = Network()
    net.addRegion("sensor", "py.ImageSensor", "{width: 32, height: 32}")
    net.addRegion("classifier","py.KNNClassifierRegion",
                  "{distThreshold: 0.01, maxCategoryCount: 2}")
    net.link("sensor", "classifier", "UniformLink", "",
             srcOutput = "dataOut", destInput = "bottomUpIn")
    net.link("sensor", "classifier", "UniformLink", "",
             srcOutput = "categoryOut", destInput = "categoryIn")
    net.initialize()
    sensor = net.regions['sensor']
    classifier = net.regions['classifier']

    # Create a dataset with two categories, one image in each category
    # Each image consists of a unique rectangle
    tmpDir = tempfile.mkdtemp()
    os.makedirs(os.path.join(tmpDir,'0'))
    os.makedirs(os.path.join(tmpDir,'1'))

    im0 = Image.new("L",(32,32))
    draw = ImageDraw.Draw(im0)
    draw.rectangle((10,10,20,20), outline=255)
    im0.save(os.path.join(tmpDir,'0','im0.png'))

    im1 = Image.new("L",(32,32))
    draw = ImageDraw.Draw(im1)
    draw.rectangle((15,15,25,25), outline=255)
    im1.save(os.path.join(tmpDir,'1','im1.png'))

    # Load the dataset
    sensor.executeCommand(["loadMultipleImages", tmpDir])
    numImages = sensor.getParameter('numImages')
    self.assertEqual(numImages, 2)

    # Ensure learning is turned ON
    self.assertEqual(classifier.getParameter('learningMode'), 1)

    # Train the network (by default learning is ON in the classifier)
    # and then turn off learning and turn on inference mode
    net.run(2)
    classifier.setParameter('inferenceMode', 1)
    classifier.setParameter('learningMode', 0)

    # Check to make sure learning is turned OFF and that the classifier learned
    # something
    self.assertEqual(classifier.getParameter('learningMode'), 0)
    self.assertEqual(classifier.getParameter('inferenceMode'), 1)
    self.assertEqual(classifier.getParameter('categoryCount'),2)
    self.assertEqual(classifier.getParameter('patternCount'),2)

    # Now test the network to make sure it categories the images correctly
    numCorrect = 0
    for i in range(2):
      net.run(1)
      inferredCategory = classifier.getOutputData('categoriesOut').argmax()
      if sensor.getOutputData('categoryOut') == inferredCategory:
        numCorrect += 1

    self.assertEqual(numCorrect,2)

    # Cleanup the temp files
    os.unlink(os.path.join(tmpDir,'0','im0.png'))
    os.unlink(os.path.join(tmpDir,'1','im1.png'))
    os.removedirs(os.path.join(tmpDir,'0'))
    os.removedirs(os.path.join(tmpDir,'1'))
Exemple #45
0
def createNetwork(dataSource):
    """Create the Network instance.

  The network has a sensor region reading data from `dataSource` and passing
  the encoded representation to an SPRegion. The SPRegion output is passed to
  a TPRegion.

  :param dataSource: a RecordStream instance to get data from
  :returns: a Network instance ready to run
  """
    network = Network()

    # Our input is sensor data from the gym file. The RecordSensor region
    # allows us to specify a file record stream as the input source via the
    # dataSource attribute.
    network.addRegion("sensor", "py.RecordSensor",
                      json.dumps({"verbosity": _VERBOSITY}))
    sensor = network.regions["sensor"].getSelf()
    # The RecordSensor needs to know how to encode the input values
    sensor.encoder = createEncoder()
    # Specify the dataSource as a file record stream instance
    sensor.dataSource = dataSource

    # Create the spatial pooler region
    SP_PARAMS["inputWidth"] = sensor.encoder.getWidth()
    network.addRegion("spatialPoolerRegion", "py.SPRegion",
                      json.dumps(SP_PARAMS))

    # Link the SP region to the sensor input
    network.link("sensor", "spatialPoolerRegion", "UniformLink", "")
    network.link("sensor",
                 "spatialPoolerRegion",
                 "UniformLink",
                 "",
                 srcOutput="resetOut",
                 destInput="resetIn")
    network.link("spatialPoolerRegion",
                 "sensor",
                 "UniformLink",
                 "",
                 srcOutput="spatialTopDownOut",
                 destInput="spatialTopDownIn")
    network.link("spatialPoolerRegion",
                 "sensor",
                 "UniformLink",
                 "",
                 srcOutput="temporalTopDownOut",
                 destInput="temporalTopDownIn")

    # Add the TPRegion on top of the SPRegion
    network.addRegion("temporalPoolerRegion", "py.TPRegion",
                      json.dumps(TP_PARAMS))

    network.link("spatialPoolerRegion", "temporalPoolerRegion", "UniformLink",
                 "")
    network.link("temporalPoolerRegion",
                 "spatialPoolerRegion",
                 "UniformLink",
                 "",
                 srcOutput="topDownOut",
                 destInput="topDownIn")

    network.initialize()

    spatialPoolerRegion = network.regions["spatialPoolerRegion"]

    # Make sure learning is enabled
    spatialPoolerRegion.setParameter("learningMode", True)
    # We want temporal anomalies so disable anomalyMode in the SP. This mode is
    # used for computing anomalies in a non-temporal model.
    spatialPoolerRegion.setParameter("anomalyMode", False)

    temporalPoolerRegion = network.regions["temporalPoolerRegion"]

    # Enable topDownMode to get the predicted columns output
    temporalPoolerRegion.setParameter("topDownMode", True)
    # Make sure learning is enabled (this is the default)
    temporalPoolerRegion.setParameter("learningMode", True)
    # Enable inference mode so we get predictions
    temporalPoolerRegion.setParameter("inferenceMode", True)
    # Enable anomalyMode to compute the anomaly score. This actually doesn't work
    # now so doesn't matter. We instead compute the anomaly score based on
    # topDownOut (predicted columns) and SP bottomUpOut (active columns).
    temporalPoolerRegion.setParameter("anomalyMode", True)

    return network
def createNetwork(dataSource):
  """Creates and returns a new Network with a sensor region reading data from
  'dataSource'. There are two hierarchical levels, each with one SP and one TP.
  @param dataSource - A RecordStream containing the input data
  @returns a Network ready to run
  """
  network = Network()

  # Create and add a record sensor and a SP region
  sensor = createRecordSensor(network, name=_RECORD_SENSOR,
                              dataSource=dataSource)
  createSpatialPooler(network, name=_L1_SPATIAL_POOLER,
                      inputWidth=sensor.encoder.getWidth())

  # Link the SP region to the sensor input
  linkType = "UniformLink"
  linkParams = ""
  network.link(_RECORD_SENSOR, _L1_SPATIAL_POOLER, linkType, linkParams)

  # Create and add a TP region
  l1temporalMemory = createTemporalMemory(network, _L1_TEMPORAL_MEMORY)

  # Link SP region to TP region in the feedforward direction
  network.link(_L1_SPATIAL_POOLER, _L1_TEMPORAL_MEMORY, linkType, linkParams)

  # Add a classifier
  classifierParams = {  # Learning rate. Higher values make it adapt faster.
                        'alpha': 0.005,

                        # A comma separated list of the number of steps the
                        # classifier predicts in the future. The classifier will
                        # learn predictions of each order specified.
                        'steps': '1',

                        # The specific implementation of the classifier to use
                        # See SDRClassifierFactory#create for options
                        'implementation': 'py',

                        # Diagnostic output verbosity control;
                        # 0: silent; [1..6]: increasing levels of verbosity
                        'verbosity': 0}

  l1Classifier = network.addRegion(_L1_CLASSIFIER, "py.SDRClassifierRegion",
                                   json.dumps(classifierParams))
  l1Classifier.setParameter('inferenceMode', True)
  l1Classifier.setParameter('learningMode', True)
  network.link(_L1_TEMPORAL_MEMORY, _L1_CLASSIFIER, linkType, linkParams,
               srcOutput="bottomUpOut", destInput="bottomUpIn")

  # Second Level
  l2inputWidth = l1temporalMemory.getSelf().getOutputElementCount("bottomUpOut")
  createSpatialPooler(network, name=_L2_SPATIAL_POOLER, inputWidth=l2inputWidth)
  network.link(_L1_TEMPORAL_MEMORY, _L2_SPATIAL_POOLER, linkType, linkParams)

  createTemporalMemory(network, _L2_TEMPORAL_MEMORY)
  network.link(_L2_SPATIAL_POOLER, _L2_TEMPORAL_MEMORY, linkType, linkParams)

  l2Classifier = network.addRegion(_L2_CLASSIFIER, "py.SDRClassifierRegion",
                                   json.dumps(classifierParams))
  l2Classifier.setParameter('inferenceMode', True)
  l2Classifier.setParameter('learningMode', True)
  network.link(_L2_TEMPORAL_MEMORY, _L2_CLASSIFIER, linkType, linkParams,
               srcOutput="bottomUpOut", destInput="bottomUpIn")
  return network
def createNetwork(dataSource):
    """Creates and returns a new Network with a sensor region reading data from
  'dataSource'. There are two hierarchical levels, each with one SP and one TP.
  @param dataSource - A RecordStream containing the input data
  @returns a Network ready to run
  """
    network = Network()

    # Create and add a record sensor and a SP region
    sensor = createRecordSensor(network,
                                name=_RECORD_SENSOR,
                                dataSource=dataSource)
    createSpatialPooler(network,
                        name=_L1_SPATIAL_POOLER,
                        inputWidth=sensor.encoder.getWidth())

    # Link the SP region to the sensor input
    linkType = "UniformLink"
    linkParams = ""
    network.link(_RECORD_SENSOR, _L1_SPATIAL_POOLER, linkType, linkParams)

    # Create and add a TP region
    l1temporalMemory = createTemporalMemory(network, _L1_TEMPORAL_MEMORY)

    # Link SP region to TP region in the feedforward direction
    network.link(_L1_SPATIAL_POOLER, _L1_TEMPORAL_MEMORY, linkType, linkParams)

    # Add a classifier
    classifierParams = {  # Learning rate. Higher values make it adapt faster.
        'alpha': 0.005,

        # A comma separated list of the number of steps the
        # classifier predicts in the future. The classifier will
        # learn predictions of each order specified.
        'steps': '1',

        # The specific implementation of the classifier to use
        # See SDRClassifierFactory#create for options
        'implementation': 'py',

        # Diagnostic output verbosity control;
        # 0: silent; [1..6]: increasing levels of verbosity
        'verbosity': 0
    }

    l1Classifier = network.addRegion(_L1_CLASSIFIER, "py.SDRClassifierRegion",
                                     json.dumps(classifierParams))
    l1Classifier.setParameter('inferenceMode', True)
    l1Classifier.setParameter('learningMode', True)
    network.link(_L1_TEMPORAL_MEMORY,
                 _L1_CLASSIFIER,
                 linkType,
                 linkParams,
                 srcOutput="bottomUpOut",
                 destInput="bottomUpIn")

    # Second Level
    l2inputWidth = l1temporalMemory.getSelf().getOutputElementCount(
        "bottomUpOut")
    createSpatialPooler(network,
                        name=_L2_SPATIAL_POOLER,
                        inputWidth=l2inputWidth)
    network.link(_L1_TEMPORAL_MEMORY, _L2_SPATIAL_POOLER, linkType, linkParams)

    createTemporalMemory(network, _L2_TEMPORAL_MEMORY)
    network.link(_L2_SPATIAL_POOLER, _L2_TEMPORAL_MEMORY, linkType, linkParams)

    l2Classifier = network.addRegion(_L2_CLASSIFIER, "py.SDRClassifierRegion",
                                     json.dumps(classifierParams))
    l2Classifier.setParameter('inferenceMode', True)
    l2Classifier.setParameter('learningMode', True)
    network.link(_L2_TEMPORAL_MEMORY,
                 _L2_CLASSIFIER,
                 linkType,
                 linkParams,
                 srcOutput="bottomUpOut",
                 destInput="bottomUpIn")
    return network
def createNetwork(dataSource, networkConfig, encoder=None):
  """
  Create and initialize the network instance with regions for the sensor, SP,
  TM, and classifier. Before running, be sure to init w/ network.initialize().

  @param dataSource: (RecordStream) Sensor region reads data from here.
  @param networkConfig: (dict) the configuration of this network.
  @param encoder: (Encoder) encoding object to use instead of specifying in
    networkConfig.
  @return network: (Network) HTM network. 
    E.g. Sensor -> SP -> TM -> SDRClassifier
  """
  network = Network()

  # Create sensor region (always enabled)
  sensorRegionConfig = networkConfig["sensorRegionConfig"]
  sensorRegionName = sensorRegionConfig["regionName"]
  sensorRegion = _createSensorRegion(network,
                                     sensorRegionConfig,
                                     dataSource,
                                     encoder)

  # Keep track of the previous region name and width to validate and link the
  # input/output width of two consecutive regions.
  previousRegion = sensorRegionName
  previousRegionWidth = sensorRegion.encoder.getWidth()

  networkRegions = [r for r in networkConfig.keys()
                    if networkConfig[r]["regionEnabled"]]

  if "spRegionConfig" in networkRegions:
    # create SP region, if enabled
    regionConfig = networkConfig["spRegionConfig"]
    regionName = regionConfig["regionName"]
    regionParams = regionConfig["regionParams"]
    regionParams["inputWidth"] = sensorRegion.encoder.getWidth()
    spRegion = _createRegion(network, regionConfig)
    _validateRegionWidths(previousRegionWidth, spRegion.getSelf().inputWidth)
    _linkRegions(network,
                 sensorRegionName,
                 previousRegion,
                 regionName)
    previousRegion = regionName
    previousRegionWidth = spRegion.getSelf().columnCount

  if "tmRegionConfig" in networkRegions:
    # create TM region, if enabled
    regionConfig = networkConfig["tmRegionConfig"]
    regionName = regionConfig["regionName"]
    regionParams = regionConfig["regionParams"]
    regionParams["inputWidth"] = regionParams["columnCount"]
    tmRegion = _createRegion(network, regionConfig)
    tmRegion.setParameter("computePredictedActiveCellIndices", True)
    tmRegion.setParameter("anomalyMode", True)
    _validateRegionWidths(previousRegionWidth, tmRegion.getSelf().columnCount)
    _linkRegions(network,
                 sensorRegionName,
                 previousRegion,
                 regionName)
    previousRegion = regionName
    previousRegionWidth = (tmRegion.getSelf().columnCount *
                           tmRegion.getSelf().cellsPerColumn)

  if "tpRegionConfig" in networkRegions:
    # create TP region, if enabled
    regionConfig = networkConfig["tpRegionConfig"]
    regionName = regionConfig["regionName"]
    regionParams = regionConfig["regionParams"]
    regionParams["inputWidth"] = previousRegionWidth
    tpRegion = _createRegion(network, regionConfig,
                             moduleName="htmresearch.regions.TemporalPoolerRegion")
    _validateRegionWidths(previousRegionWidth,
                          tpRegion.getSelf()._inputWidth)
    _linkRegions(network,
                 sensorRegionName,
                 previousRegion,
                 regionName)
    previousRegion = regionName

  # Create classifier region (always enabled)
  regionConfig = networkConfig["classifierRegionConfig"]
  regionName = regionConfig["regionName"]
  _createRegion(network, regionConfig)
  # Link the classifier to previous region and sensor region - to send in
  # category labels.
  network.link(previousRegion, regionName, "UniformLink", "")
  network.link(sensorRegionName,
               regionName,
               "UniformLink",
               "",
               srcOutput="categoryOut",
               destInput="categoryIn")

  # Link in sequenceId/partitionId if the appropriate input exists
  classifierSpec = network.regions[regionName].getSpec()
  if classifierSpec.inputs.contains('partitionIn'):
    network.link(sensorRegionName, regionName, "UniformLink", "",
                 srcOutput="sequenceIdOut", destInput="partitionIn")

  return network
    def runNodesTest(self, nodeType1, nodeType2):
        # =====================================================
        # Build and run the network
        # =====================================================
        LOGGER.info('test(level1: %s, level2: %s)', nodeType1, nodeType2)
        net = Network()
        level1 = net.addRegion("level1", nodeType1, "{int32Param: 15}")
        dims = Dimensions([6, 4])
        level1.setDimensions(dims)

        level2 = net.addRegion("level2", nodeType2, "{real64Param: 128.23}")

        net.link("level1", "level2", "TestFanIn2", "")

        # Could call initialize here, but not necessary as net.run()
        # initializes implicitly.
        # net.initialize()

        net.run(1)
        LOGGER.info("Successfully created network and ran for one iteration")

        # =====================================================
        # Check everything
        # =====================================================
        dims = level1.getDimensions()
        self.assertEqual(len(dims), 2)
        self.assertEqual(dims[0], 6)
        self.assertEqual(dims[1], 4)

        dims = level2.getDimensions()
        self.assertEqual(len(dims), 2)
        self.assertEqual(dims[0], 3)
        self.assertEqual(dims[1], 2)

        # Check L1 output. "False" means don't copy, i.e.
        # get a pointer to the actual output
        # Actual output values are determined by the TestNode
        # compute() behavior.
        l1output = level1.getOutputData("bottomUpOut")
        self.assertEqual(len(l1output), 48)  # 24 nodes; 2 values per node
        for i in xrange(24):
            self.assertEqual(l1output[2 * i],
                             0)  # size of input to each node is 0
            self.assertEqual(l1output[2 * i + 1], i)  # node number

        # check L2 output.
        l2output = level2.getOutputData("bottomUpOut")
        self.assertEqual(len(l2output), 12)  # 6 nodes; 2 values per node
        # Output val = node number + sum(inputs)
        # Can compute from knowing L1 layout
        #
        #  00 01 | 02 03 | 04 05
        #  06 07 | 08 09 | 10 11
        #  ---------------------
        #  12 13 | 14 15 | 16 17
        #  18 19 | 20 21 | 22 23
        outputVals = []
        outputVals.append(0 + (0 + 1 + 6 + 7))
        outputVals.append(1 + (2 + 3 + 8 + 9))
        outputVals.append(2 + (4 + 5 + 10 + 11))
        outputVals.append(3 + (12 + 13 + 18 + 19))
        outputVals.append(4 + (14 + 15 + 20 + 21))
        outputVals.append(5 + (16 + 17 + 22 + 23))
        for i in xrange(6):
            if l2output[2 * i] != 8:
                LOGGER.info(l2output[2 * i])
                # from dbgp.client import brk; brk(port=9019)

            self.assertEqual(l2output[2 * i],
                             8)  # size of input for each node is 8
            self.assertEqual(l2output[2 * i + 1], outputVals[i])

        # =====================================================
        # Run for one more iteration
        # =====================================================
        LOGGER.info("Running for a second iteration")
        net.run(1)

        # =====================================================
        # Check everything again
        # =====================================================

        # Outputs are all the same except that the first output is
        # incremented by the iteration number
        for i in xrange(24):
            self.assertEqual(l1output[2 * i], 1)
            self.assertEqual(l1output[2 * i + 1], i)

        for i in xrange(6):
            self.assertEqual(l2output[2 * i], 9)
            self.assertEqual(l2output[2 * i + 1], outputVals[i] + 4)

        # =====================================================
        # Demonstrate a few other features
        # =====================================================

        #
        # Linking can induce dimensions downward
        #

        net = Network()
        level1 = net.addRegion("level1", nodeType1, "")
        level2 = net.addRegion("level2", nodeType2, "")
        dims = Dimensions([3, 2])
        level2.setDimensions(dims)
        net.link("level1", "level2", "TestFanIn2", "")
        net.initialize()

        # Level1 should now have dimensions [6, 4]
        self.assertEqual(level1.getDimensions()[0], 6)
        self.assertEqual(level1.getDimensions()[1], 4)
  def testSerialization(self):
    n = Network()

    imageDims = (42, 38)
    params = dict(
      width=imageDims[0],
      height=imageDims[1],
      mode="bw",
      background=1,
      invertOutput=1)

    sensor = n.addRegion("sensor", "py.ImageSensor", json.dumps(params))
    sensor.setDimensions(Dimensions(imageDims[0], imageDims[1]))

    params = dict(
      inputShape=imageDims,
      coincidencesShape=imageDims,
      disableTemporal=1,
      tpSeed=43,
      spSeed=42,
      nCellsPerCol=1)

    l1 = n.addRegion("l1", "py.CLARegion", json.dumps(params))

    params = dict(
      maxCategoryCount=48,
      SVDSampleCount=400,
      SVDDimCount=5,
      distanceNorm=0.6)

    _classifier = n.addRegion("classifier", "py.KNNClassifierRegion",
                              json.dumps(params))

    # TODO: link params should not be required. Dest region dimensions are
    # already specified as [1]
    params = dict(
      mapping="in",
      rfSize=imageDims)

    n.link("sensor", "l1", "UniformLink", json.dumps(params))
    n.link("l1", "classifier", "UniformLink", "", "bottomUpOut", "bottomUpIn")
    n.link("sensor", "classifier", "UniformLink", "", "categoryOut",
           "categoryIn")
    n.initialize()

    n.save("fdr.nta")

    # Make sure the network bundle has all the expected files
    self.assertTrue(os.path.exists("fdr.nta/network.yaml"))
    self.assertTrue(os.path.exists("fdr.nta/R0-pkl"))
    self.assertTrue(os.path.exists("fdr.nta/R1-pkl"))
    self.assertTrue(os.path.exists("fdr.nta/R2-pkl"))

    n2 = Network("fdr.nta")
    n2.initialize()  # should not fail

    # Make sure the network is actually the same
    sensor = n2.regions['sensor']
    self.assertEqual(sensor.type, "py.ImageSensor")
    # would like to directly compare, but can't -- NPC-6
    self.assertEqual(str(sensor.dimensions), str(Dimensions(42, 38)))
    self.assertEqual(sensor.getParameter("width"), 42)
    self.assertEqual(sensor.getParameter("height"), 38)
    self.assertEqual(sensor.getParameter("mode"), "bw")
    self.assertEqual(sensor.getParameter("background"), 1)
    self.assertEqual(sensor.getParameter("invertOutput"), 1)

    l1 = n2.regions['l1']
    self.assertEqual(l1.type, "py.CLARegion")
    self.assertEqual(str(l1.dimensions), str(Dimensions(1)))
    a = l1.getParameter("inputShape")
    self.assertEqual(len(a), 2)
    self.assertEqual(a[0], 42)
    self.assertEqual(a[1], 38)

    a = l1.getParameter("coincidencesShape")
    self.assertEqual(len(a), 2)
    self.assertEqual(a[0], 42)
    self.assertEqual(a[1], 38)

    self.assertEqual(l1.getParameter("disableTemporal"), 1)
    self.assertEqual(l1.getParameter("spSeed"), 42)
    self.assertEqual(l1.getParameter("tpSeed"), 43)

    cl = n2.regions['classifier']
    self.assertEqual(cl.type, "py.KNNClassifierRegion")
    self.assertEqual(cl.getParameter("maxCategoryCount"), 48)
    self.assertEqual(cl.getParameter("SVDSampleCount"), 400)
    self.assertEqual(cl.getParameter("SVDDimCount"), 5)
    self.assertLess((cl.getParameter("distanceNorm") - 0.6), 0.0001)
    self.assertEqual(str(cl.dimensions), str(Dimensions(1)))

    n2.save("fdr2.nta")

    # now compare the two network bundles -- should be the same
    c = filecmp.dircmp("fdr.nta", "fdr2.nta")
    self.assertEqual(len(c.left_only), 0,
                     "fdr.nta has extra files: %s" % c.left_only)

    self.assertEqual(len(c.right_only), 0,
                     "fdr2.nta has extra files: %s" % c.right_only)

    if len(c.diff_files) > 0:
      _LOGGER.warn("Some bundle files differ: %s\n"
                   "This is expected, as pickle.load() followed by "
                   "pickle.dump() doesn't produce the same file", c.diff_files)
Exemple #51
0
def _createNetwork(inverseReadoutResolution, anchorInputSize, dualPhase=False):
    """
  Create a simple network connecting sensor and motor inputs to the location
  region. Use :meth:`RawSensor.addDataToQueue` to add sensor input and growth
  candidates. Use :meth:`RawValues.addDataToQueue` to add motor input.
  ::
                        +----------+
    [   sensor*   ] --> |          | --> [     activeCells        ]
    [ candidates* ] --> | location | --> [    learnableCells      ]
    [    motor    ] --> |          | --> [ sensoryAssociatedCells ]
                        +----------+

  :param inverseReadoutResolution:
    Specifies the diameter of the circle of phases in the rhombus encoded by a
    bump.
  :type inverseReadoutResolution: int

  :type anchorInputSize: int
  :param anchorInputSize:
    The number of input bits in the anchor input.

  .. note::
    (*) This function will only add the 'sensor' and 'candidates' regions when
    'anchorInputSize' is greater than zero. This is useful if you would like to
    compute locations ignoring sensor input

  .. seealso::
     - :py:func:`htmresearch.frameworks.location.path_integration_union_narrowing.createRatModuleFromReadoutResolution`

  """
    net = Network()

    # Create simple region to pass motor commands as displacement vectors (dx, dy)
    net.addRegion("motor", "py.RawValues", json.dumps({"outputWidth": 2}))

    if anchorInputSize > 0:
        # Create simple region to pass growth candidates
        net.addRegion("candidates", "py.RawSensor",
                      json.dumps({"outputWidth": anchorInputSize}))

        # Create simple region to pass sensor input
        net.addRegion("sensor", "py.RawSensor",
                      json.dumps({"outputWidth": anchorInputSize}))

    # Initialize region with 5 modules varying scale by sqrt(2) and 4 different
    # random orientations for each scale
    scale = []
    orientation = []
    for i in xrange(5):
        for _ in xrange(4):
            angle = np.radians(random.gauss(7.5, 7.5))
            orientation.append(random.choice([angle, -angle]))
            scale.append(10.0 * (math.sqrt(2)**i))

    # Create location region
    params = computeRatModuleParametersFromReadoutResolution(
        inverseReadoutResolution)
    params.update({
        "moduleCount": len(scale),
        "scale": scale,
        "orientation": orientation,
        "anchorInputSize": anchorInputSize,
        "activationThreshold": 8,
        "initialPermanence": 1.0,
        "connectedPermanence": 0.5,
        "learningThreshold": 8,
        "sampleSize": 10,
        "permanenceIncrement": 0.1,
        "permanenceDecrement": 0.0,
        "dualPhase": dualPhase,
        "bumpOverlapMethod": "probabilistic"
    })
    net.addRegion("location", "py.GridCellLocationRegion", json.dumps(params))

    if anchorInputSize > 0:
        # Link sensor
        net.link("sensor",
                 "location",
                 "UniformLink",
                 "",
                 srcOutput="dataOut",
                 destInput="anchorInput")
        net.link("sensor",
                 "location",
                 "UniformLink",
                 "",
                 srcOutput="resetOut",
                 destInput="resetIn")
        net.link("candidates",
                 "location",
                 "UniformLink",
                 "",
                 srcOutput="dataOut",
                 destInput="anchorGrowthCandidates")

    # Link motor input
    net.link("motor",
             "location",
             "UniformLink",
             "",
             srcOutput="dataOut",
             destInput="displacement")

    # Initialize network objects
    net.initialize()

    return net
Exemple #52
0
    def testSimpleImageNetwork(self):

        # Create the network and get region instances
        net = Network()
        net.addRegion("sensor", "py.ImageSensor", "{width: 32, height: 32}")
        net.addRegion("classifier", "py.KNNClassifierRegion",
                      "{distThreshold: 0.01, maxCategoryCount: 2}")
        net.link("sensor",
                 "classifier",
                 "UniformLink",
                 "",
                 srcOutput="dataOut",
                 destInput="bottomUpIn")
        net.link("sensor",
                 "classifier",
                 "UniformLink",
                 "",
                 srcOutput="categoryOut",
                 destInput="categoryIn")
        net.initialize()
        sensor = net.regions['sensor']
        classifier = net.regions['classifier']

        # Create a dataset with two categories, one image in each category
        # Each image consists of a unique rectangle
        tmpDir = tempfile.mkdtemp()
        os.makedirs(os.path.join(tmpDir, '0'))
        os.makedirs(os.path.join(tmpDir, '1'))

        im0 = Image.new("L", (32, 32))
        draw = ImageDraw.Draw(im0)
        draw.rectangle((10, 10, 20, 20), outline=255)
        im0.save(os.path.join(tmpDir, '0', 'im0.png'))

        im1 = Image.new("L", (32, 32))
        draw = ImageDraw.Draw(im1)
        draw.rectangle((15, 15, 25, 25), outline=255)
        im1.save(os.path.join(tmpDir, '1', 'im1.png'))

        # Load the dataset
        sensor.executeCommand(["loadMultipleImages", tmpDir])
        numImages = sensor.getParameter('numImages')
        self.assertEqual(numImages, 2)

        # Ensure learning is turned ON
        self.assertEqual(classifier.getParameter('learningMode'), 1)

        # Train the network (by default learning is ON in the classifier)
        # and then turn off learning and turn on inference mode
        net.run(2)
        classifier.setParameter('inferenceMode', 1)
        classifier.setParameter('learningMode', 0)

        # Check to make sure learning is turned OFF and that the classifier learned
        # something
        self.assertEqual(classifier.getParameter('learningMode'), 0)
        self.assertEqual(classifier.getParameter('inferenceMode'), 1)
        self.assertEqual(classifier.getParameter('categoryCount'), 2)
        self.assertEqual(classifier.getParameter('patternCount'), 2)

        # Now test the network to make sure it categories the images correctly
        numCorrect = 0
        for i in range(2):
            net.run(1)
            inferredCategory = classifier.getOutputData(
                'categoriesOut').argmax()
            if sensor.getOutputData('categoryOut') == inferredCategory:
                numCorrect += 1

        self.assertEqual(numCorrect, 2)

        # Cleanup the temp files
        os.unlink(os.path.join(tmpDir, '0', 'im0.png'))
        os.unlink(os.path.join(tmpDir, '1', 'im1.png'))
        os.removedirs(os.path.join(tmpDir, '0'))
        os.removedirs(os.path.join(tmpDir, '1'))
    def testSimpleMulticlassNetworkPY(self):
        # Setup data record stream of fake data (with three categories)
        filename = _getTempFileName()
        fields = [("timestamp", "datetime", "T"), ("value", "float", ""),
                  ("reset", "int", "R"), ("sid", "int", "S"),
                  ("categories", "list", "C")]
        records = ([datetime(day=1, month=3, year=2010), 0.0, 1, 0, "0"], [
            datetime(day=2, month=3, year=2010), 1.0, 0, 0, "1"
        ], [datetime(day=3, month=3, year=2010), 0.0, 0, 0,
            "0"], [datetime(day=4, month=3, year=2010), 1.0, 0, 0,
                   "1"], [datetime(day=5, month=3, year=2010), 0.0, 0, 0, "0"],
                   [datetime(day=6, month=3, year=2010), 1.0, 0, 0, "1"
                    ], [datetime(day=7, month=3, year=2010), 0.0, 0, 0, "0"],
                   [datetime(day=8, month=3, year=2010), 1.0, 0, 0, "1"])
        dataSource = FileRecordStream(streamID=filename,
                                      write=True,
                                      fields=fields)
        for r in records:
            dataSource.appendRecord(list(r))

        # Create the network and get region instances.
        net = Network()
        net.addRegion("sensor", "py.RecordSensor", "{'numCategories': 3}")
        net.addRegion("classifier", "py.SDRClassifierRegion",
                      "{steps: '0', alpha: 0.001, implementation: 'py'}")
        net.link("sensor",
                 "classifier",
                 "UniformLink",
                 "",
                 srcOutput="dataOut",
                 destInput="bottomUpIn")
        net.link("sensor",
                 "classifier",
                 "UniformLink",
                 "",
                 srcOutput="categoryOut",
                 destInput="categoryIn")
        sensor = net.regions["sensor"]
        classifier = net.regions["classifier"]

        # Setup sensor region encoder and data stream.
        dataSource.close()
        dataSource = FileRecordStream(filename)
        sensorRegion = sensor.getSelf()
        sensorRegion.encoder = MultiEncoder()
        sensorRegion.encoder.addEncoder(
            "value", ScalarEncoder(21, 0.0, 13.0, n=256, name="value"))
        sensorRegion.dataSource = dataSource

        # Get ready to run.
        net.initialize()

        # Train the network (by default learning is ON in the classifier, but assert
        # anyway) and then turn off learning and turn on inference mode.
        self.assertEqual(classifier.getParameter("learningMode"), 1)
        net.run(8)

        # Test the network on the same data as it trained on; should classify with
        # 100% accuracy.
        classifier.setParameter("inferenceMode", 1)
        classifier.setParameter("learningMode", 0)

        # Assert learning is OFF and that the classifier learned the dataset.
        self.assertEqual(classifier.getParameter("learningMode"), 0,
                         "Learning mode is not turned off.")
        self.assertEqual(classifier.getParameter("inferenceMode"), 1,
                         "Inference mode is not turned on.")

        # make sure we can access all the parameters with getParameter
        self.assertEqual(classifier.getParameter("maxCategoryCount"), 2000)
        self.assertAlmostEqual(float(classifier.getParameter("alpha")), 0.001)
        self.assertEqual(int(classifier.getParameter("steps")), 0)
        self.assertTrue(classifier.getParameter("implementation") == "py")
        self.assertEqual(classifier.getParameter("verbosity"), 0)

        expectedCats = (
            [0.0],
            [1.0],
            [0.0],
            [1.0],
            [0.0],
            [1.0],
            [0.0],
            [1.0],
        )
        dataSource.rewind()
        for i in xrange(8):
            net.run(1)
            inferredCats = classifier.getOutputData("categoriesOut")
            self.assertSequenceEqual(
                expectedCats[i], inferredCats.tolist(),
                "Classififer did not infer expected category "
                "for record number {}.".format(i))
        # Close data stream, delete file.
        dataSource.close()
        os.remove(filename)
  def myCreateNetwork(self, networkConfig):

        suffix = '_0'
        network = Network()

        sensorInputName = "sensorInput" + suffix
        L4ColumnName = "L4Column" + suffix
        L2ColumnName = "L2Column" + suffix

        L4Params = copy.deepcopy(networkConfig["L4Params"])
        L4Params["apicalInputWidth"] = networkConfig["L2Params"]["cellCount"]

        network.addRegion(
          sensorInputName, "py.RawSensor",
          json.dumps({"outputWidth": networkConfig["sensorInputSize"]}))

        network.addRegion(
          L4ColumnName, networkConfig["L4RegionType"],
          json.dumps(L4Params))
        network.addRegion(
          L2ColumnName, "py.ColumnPoolerRegion",
          json.dumps(networkConfig["L2Params"]))

        network.setPhases(sensorInputName,[0])


        # L4 and L2 regions always have phases 2 and 3, respectively
        network.setPhases(L4ColumnName,[2])
        network.setPhases(L2ColumnName,[3])

        network.link(sensorInputName, L4ColumnName, "UniformLink", "",
                         srcOutput="dataOut", destInput="activeColumns")

        # Link L4 to L2
        network.link(L4ColumnName, L2ColumnName, "UniformLink", "",
                     srcOutput="activeCells", destInput="feedforwardInput")
        network.link(L4ColumnName, L2ColumnName, "UniformLink", "",
                     srcOutput="winnerCells",
                     destInput="feedforwardGrowthCandidates")

        # Link L2 feedback to L4
        network.link(L2ColumnName, L4ColumnName, "UniformLink", "",
                     srcOutput="feedForwardOutput", destInput="apicalInput",
                     propagationDelay=1)

        # Link reset output to L2 and L4.
        network.link(sensorInputName, L2ColumnName, "UniformLink", "",
                     srcOutput="resetOut", destInput="resetIn")
        network.link(sensorInputName, L4ColumnName, "UniformLink", "",
                     srcOutput="resetOut", destInput="resetIn")

        #enableProfiling(network)
        for region in network.regions.values():
            region.enableProfiling()
        return network
  def testSimpleMulticlassNetworkPY(self):
    # Setup data record stream of fake data (with three categories)
    filename = _getTempFileName()
    fields = [("timestamp", "datetime", "T"),
              ("value", "float", ""),
              ("reset", "int", "R"),
              ("sid", "int", "S"),
              ("categories", "list", "C")]
    records = (
      [datetime(day=1, month=3, year=2010), 0.0, 1, 0, "0"],
      [datetime(day=2, month=3, year=2010), 1.0, 0, 0, "1"],
      [datetime(day=3, month=3, year=2010), 0.0, 0, 0, "0"],
      [datetime(day=4, month=3, year=2010), 1.0, 0, 0, "1"],
      [datetime(day=5, month=3, year=2010), 0.0, 0, 0, "0"],
      [datetime(day=6, month=3, year=2010), 1.0, 0, 0, "1"],
      [datetime(day=7, month=3, year=2010), 0.0, 0, 0, "0"],
      [datetime(day=8, month=3, year=2010), 1.0, 0, 0, "1"])
    dataSource = FileRecordStream(streamID=filename, write=True, fields=fields)
    for r in records:
      dataSource.appendRecord(list(r))

    # Create the network and get region instances.
    net = Network()
    net.addRegion("sensor", "py.RecordSensor", "{'numCategories': 3}")
    net.addRegion("classifier", "py.SDRClassifierRegion",
                  "{steps: '0', alpha: 0.001, implementation: 'py'}")
    net.link("sensor", "classifier", "UniformLink", "",
             srcOutput="dataOut", destInput="bottomUpIn")
    net.link("sensor", "classifier", "UniformLink", "",
             srcOutput="categoryOut", destInput="categoryIn")
    sensor = net.regions["sensor"]
    classifier = net.regions["classifier"]

    # Setup sensor region encoder and data stream.
    dataSource.close()
    dataSource = FileRecordStream(filename)
    sensorRegion = sensor.getSelf()
    sensorRegion.encoder = MultiEncoder()
    sensorRegion.encoder.addEncoder(
      "value", ScalarEncoder(21, 0.0, 13.0, n=256, name="value"))
    sensorRegion.dataSource = dataSource

    # Get ready to run.
    net.initialize()

    # Train the network (by default learning is ON in the classifier, but assert
    # anyway) and then turn off learning and turn on inference mode.
    self.assertEqual(classifier.getParameter("learningMode"), 1)
    net.run(8)

    # Test the network on the same data as it trained on; should classify with
    # 100% accuracy.
    classifier.setParameter("inferenceMode", 1)
    classifier.setParameter("learningMode", 0)

    # Assert learning is OFF and that the classifier learned the dataset.
    self.assertEqual(classifier.getParameter("learningMode"), 0,
                     "Learning mode is not turned off.")
    self.assertEqual(classifier.getParameter("inferenceMode"), 1,
                     "Inference mode is not turned on.")

    # make sure we can access all the parameters with getParameter
    self.assertEqual(classifier.getParameter("maxCategoryCount"), 2000)
    self.assertAlmostEqual(float(classifier.getParameter("alpha")), 0.001)
    self.assertEqual(int(classifier.getParameter("steps")), 0)
    self.assertTrue(classifier.getParameter("implementation") == "py")
    self.assertEqual(classifier.getParameter("verbosity"), 0)


    expectedCats = ([0.0], [1.0], [0.0], [1.0], [0.0], [1.0], [0.0], [1.0],)
    dataSource.rewind()
    for i in xrange(8):
      net.run(1)
      inferredCats = classifier.getOutputData("categoriesOut")
      self.assertSequenceEqual(expectedCats[i], inferredCats.tolist(),
                               "Classififer did not infer expected category "
                               "for record number {}.".format(i))
    # Close data stream, delete file.
    dataSource.close()
    os.remove(filename)
def createNetwork(dataSource, networkConfig, encoder=None):
  """
  Create and initialize the network instance with regions for the sensor, SP,
  TM, and classifier. Before running, be sure to init w/ network.initialize().

  @param dataSource: (RecordStream) Sensor region reads data from here.
  @param networkConfig: (dict) the configuration of this network.
  @param encoder: (Encoder) encoding object to use instead of specifying in
    networkConfig.
  @return network: (Network) Sample network. E.g. Sensor -> SP -> TM -> Classif.
  """
  network = Network()

  # Create sensor region (always enabled)
  sensorRegionConfig = networkConfig["sensorRegionConfig"]
  sensorRegionName = sensorRegionConfig["regionName"]
  sensorRegion = _createSensorRegion(network,
                                     sensorRegionConfig,
                                     dataSource,
                                     encoder)

  # Keep track of the previous region name and width to validate and link the
  # input/output width of two consecutive regions.
  previousRegion = sensorRegionName
  previousRegionWidth = sensorRegion.encoder.getWidth()

  networkRegions = [r for r in networkConfig.keys() if networkConfig[r]["regionEnabled"]]

  if "spRegionConfig" in networkRegions:
    # create SP region, if enabled
    regionConfig = networkConfig["spRegionConfig"]
    regionName = regionConfig["regionName"]
    regionParams = regionConfig["regionParams"]
    regionParams["inputWidth"] = sensorRegion.encoder.getWidth()
    spRegion = _createRegion(network, regionConfig)
    _validateRegionWidths(previousRegionWidth, spRegion.getSelf().inputWidth)
    _linkRegions(network,
                 sensorRegionName,
                 previousRegion,
                 regionName)
    previousRegion = regionName
    previousRegionWidth = spRegion.getSelf().columnCount

  if "tmRegionConfig" in networkRegions:
    # create TM region, if enabled
    regionConfig = networkConfig["tmRegionConfig"]
    regionName = regionConfig["regionName"]
    regionParams = regionConfig["regionParams"]
    regionParams["inputWidth"] = regionParams["columnCount"]
    tmRegion = _createRegion(network, regionConfig)
    _validateRegionWidths(previousRegionWidth, tmRegion.getSelf().columnCount)
    _linkRegions(network,
                 sensorRegionName,
                 previousRegion,
                 regionName)
    previousRegion = regionName
    previousRegionWidth = tmRegion.getSelf().cellsPerColumn

  if "upRegionConfig" in networkRegions:
    # create UP region, if enabled
    #   this req's the union_pooling dir to be on your system path
    #   add w/ >>> import sys; sys.path.append(path/to/union_pooling)
    regionConfig = networkConfig["upRegionConfig"]
    regionName = regionConfig["regionName"]
    regionParams = regionConfig["regionParams"]
    regionParams["inputWidth"] = previousRegionWidth
    upRegion = _createRegion(network, regionConfig,
      moduleName="union_pooling.PoolingRegion")
    _validateRegionWidths(previousRegionWidth,
                          upRegion.getSelf().cellsPerColumn)
    _linkRegions(network,
                 sensorRegionName,
                 previousRegion,
                 regionName)
    previousRegion = regionName

  # Create classifier region (always enabled)
  regionConfig = networkConfig["classifierRegionConfig"]
  regionName = regionConfig["regionName"]
  _createRegion(network, regionConfig)
  # Link the classifier to previous region and sensor region - to send in
  # category labels.
  network.link(previousRegion, regionName, "UniformLink", "")
  network.link(sensorRegionName,
               regionName,
               "UniformLink",
               "",
               srcOutput="categoryOut",
               destInput="categoryIn")

  return network
Exemple #57
0
def createNetwork():
  network = Network()

  #
  # Sensors
  #

  # C++
  consumptionSensor = network.addRegion('consumptionSensor', 'ScalarSensor',
                                        json.dumps({'n': 120,
                                                    'w': 21,
                                                    'minValue': 0.0,
                                                    'maxValue': 100.0,
                                                    'clipInput': True}))

  # Python
  timestampSensor = network.addRegion("timestampSensor",
                                      'py.PluggableEncoderSensor', "")
  timestampSensor.getSelf().encoder = DateEncoder(timeOfDay=(21, 9.5),
                                                  name="timestamp_timeOfDay")

  #
  # Add a SPRegion, a region containing a spatial pooler
  #
  consumptionEncoderN = consumptionSensor.getParameter('n')
  timestampEncoderN = timestampSensor.getSelf().encoder.getWidth()
  inputWidth = consumptionEncoderN + timestampEncoderN

  network.addRegion("sp", "py.SPRegion",
                    json.dumps({
                      "spatialImp": "cpp",
                      "globalInhibition": 1,
                      "columnCount": 2048,
                      "inputWidth": inputWidth,
                      "numActiveColumnsPerInhArea": 40,
                      "seed": 1956,
                      "potentialPct": 0.8,
                      "synPermConnected": 0.1,
                      "synPermActiveInc": 0.0001,
                      "synPermInactiveDec": 0.0005,
                      "maxBoost": 1.0,
                    }))

  #
  # Input to the Spatial Pooler
  #
  network.link("consumptionSensor", "sp", "UniformLink", "")
  network.link("timestampSensor", "sp", "UniformLink", "")

  #
  # Add a TPRegion, a region containing a Temporal Memory
  #
  network.addRegion("tm", "py.TPRegion",
                    json.dumps({
                      "columnCount": 2048,
                      "cellsPerColumn": 32,
                      "inputWidth": 2048,
                      "seed": 1960,
                      "temporalImp": "cpp",
                      "newSynapseCount": 20,
                      "maxSynapsesPerSegment": 32,
                      "maxSegmentsPerCell": 128,
                      "initialPerm": 0.21,
                      "permanenceInc": 0.1,
                      "permanenceDec": 0.1,
                      "globalDecay": 0.0,
                      "maxAge": 0,
                      "minThreshold": 9,
                      "activationThreshold": 12,
                      "outputType": "normal",
                      "pamLength": 3,
                    }))

  network.link("sp", "tm", "UniformLink", "")
  network.link("tm", "sp", "UniformLink", "", srcOutput="topDownOut",
               destInput="topDownIn")

  # Enable anomalyMode so the tm calculates anomaly scores
  network.regions['tm'].setParameter("anomalyMode", True)
  # Enable inference mode to be able to get predictions
  network.regions['tm'].setParameter("inferenceMode", True)

  return network
def createNetwork(dataSource):
  """Create the Network instance.

  The network has a sensor region reading data from `dataSource` and passing
  the encoded representation to an SPRegion. The SPRegion output is passed to
  a TPRegion.

  :param dataSource: a RecordStream instance to get data from
  :returns: a Network instance ready to run
  """
  network = Network()

  # Our input is sensor data from the gym file. The RecordSensor region
  # allows us to specify a file record stream as the input source via the
  # dataSource attribute.
  network.addRegion("sensor", "py.RecordSensor",
                    json.dumps({"verbosity": _VERBOSITY}))
  sensor = network.regions["sensor"].getSelf()
  # The RecordSensor needs to know how to encode the input values
  sensor.encoder = createEncoder()
  # Specify the dataSource as a file record stream instance
  sensor.dataSource = dataSource

  # Create the spatial pooler region
  SP_PARAMS["inputWidth"] = sensor.encoder.getWidth()
  network.addRegion("spatialPoolerRegion", "py.SPRegion", json.dumps(SP_PARAMS))

  # Link the SP region to the sensor input
  network.link("sensor", "spatialPoolerRegion", "UniformLink", "")
  network.link("sensor", "spatialPoolerRegion", "UniformLink", "",
               srcOutput="resetOut", destInput="resetIn")
  network.link("spatialPoolerRegion", "sensor", "UniformLink", "",
               srcOutput="spatialTopDownOut", destInput="spatialTopDownIn")
  network.link("spatialPoolerRegion", "sensor", "UniformLink", "",
               srcOutput="temporalTopDownOut", destInput="temporalTopDownIn")

  # Add the TPRegion on top of the SPRegion
  # TODO: Needs TMRegion
  network.addRegion("temporalMemoryRegion", "py.TPRegion",
                    json.dumps(TP_PARAMS))

  network.link("spatialPoolerRegion", "temporalMemoryRegion", "UniformLink", "")
  network.link("temporalMemoryRegion", "spatialPoolerRegion", "UniformLink", "",
               srcOutput="topDownOut", destInput="topDownIn")

  # Register TPRegion since we aren't in nupic
  curDirectory = os.path.dirname(os.path.abspath(__file__))
  # directory containing the union pooler directory is 2 directories above this file
  unionTemporalPoolerDirectory = os.path.split((os.path.split(curDirectory))[0])[0]
  sys.path.append(unionTemporalPoolerDirectory)
  Network.registerRegionPackage("union_temporal_pooling")

  # Add the TPRegion on top of the TPRegion
  temporal = network.regions["temporalMemoryRegion"].getSelf()
  UP_PARAMS["inputWidth"] = temporal.getOutputElementCount("bottomUpOut")
  network.addRegion("unionTemporalPoolerRegion", "py.TemporalPoolerRegion", json.dumps(UP_PARAMS))

  network.link("temporalMemoryRegion", "unionTemporalPoolerRegion", "UniformLink", "",
               srcOutput="activeCells", destInput="activeCells")
  network.link("temporalMemoryRegion", "unionTemporalPoolerRegion", "UniformLink", "",
               srcOutput="predictedActiveCells", destInput="predictedActiveCells")

  network.initialize()

  spatial = network.regions["spatialPoolerRegion"].getSelf()
  # Make sure learning is enabled (this is the default)
  spatial.setParameter("learningMode", 1, True)
  # We want temporal anomalies so disable anomalyMode in the SP. This mode is
  # used for computing anomalies in a non-temporal model.
  spatial.setParameter("anomalyMode", 1, False)

  # Enable topDownMode to get the predicted columns output
  temporal.setParameter("topDownMode", 1, True)
  # Make sure learning is enabled (this is the default)
  temporal.setParameter("learningMode", 1, True)
  # Enable inference mode so we get predictions
  temporal.setParameter("inferenceMode", 1, True)
  temporal.setParameter("computePredictedActiveCellIndices", 1, True)

  union = network.regions["unionTemporalPoolerRegion"].getSelf()
  # Make sure learning is enabled (this is the default)
  union.setParameter("learningMode", 1, True)

  return network