Exemple #1
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)
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)
def _createNetwork():
  """Create a network with a RecordSensor region and a SDRClassifier region"""

  network = Network()
  network.addRegion('sensor', 'py.RecordSensor', '{}')
  network.addRegion('classifier', 'py.SDRClassifierRegion', '{}')
  _createSensorToClassifierLinks(network, 'sensor', 'classifier')

  # Add encoder to sensor region.
  sensorRegion = network.regions['sensor'].getSelf()
  encoderParams = {'consumption': {'fieldname': 'consumption',
                                   'resolution': 0.88,
                                   'seed': 1,
                                   'name': 'consumption',
                                   'type': 'RandomDistributedScalarEncoder'}}

  encoder = MultiEncoder()
  encoder.addMultipleEncoders(encoderParams)
  sensorRegion.encoder = encoder

  # Add data source.
  testDir = os.path.dirname(os.path.abspath(__file__))
  inputFile = os.path.join(testDir, 'fixtures', 'gymdata-test.csv')
  dataSource = FileRecordStream(streamID=inputFile)
  sensorRegion.dataSource = dataSource

  # Get and set what field index we want to predict.
  predictedIdx = dataSource.getFieldNames().index('consumption')
  network.regions['sensor'].setParameter('predictedFieldIdx', predictedIdx)

  return network
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
Exemple #5
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 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 #7
0
def _createNetwork():
    """Create network with one RecordSensor region."""
    network = Network()
    network.addRegion('sensor', 'py.RecordSensor', '{}')
    sensorRegion = network.regions['sensor'].getSelf()

    # Add an encoder.
    encoderParams = {
        'consumption': {
            'fieldname': 'consumption',
            'resolution': 0.88,
            'seed': 1,
            'name': 'consumption',
            'type': 'RandomDistributedScalarEncoder'
        }
    }

    encoder = MultiEncoder()
    encoder.addMultipleEncoders(encoderParams)
    sensorRegion.encoder = encoder

    # Add a data source.
    testDir = os.path.dirname(os.path.abspath(__file__))
    inputFile = os.path.join(testDir, 'fixtures', 'gymdata-test.csv')
    dataSource = FileRecordStream(streamID=inputFile)
    sensorRegion.dataSource = dataSource

    # Get and set what field index we want to predict.
    predictedIdx = dataSource.getFieldNames().index('consumption')
    network.regions['sensor'].setParameter('predictedFieldIdx', predictedIdx)

    return network
Exemple #8
0
def _createNetwork():
    """Create a network with a RecordSensor region and a SDRClassifier region"""

    network = Network()
    network.addRegion('sensor', 'py.RecordSensor', '{}')
    network.addRegion('classifier', 'py.SDRClassifierRegion', '{}')
    _createSensorToClassifierLinks(network, 'sensor', 'classifier')

    # Add encoder to sensor region.
    sensorRegion = network.regions['sensor'].getSelf()
    encoderParams = {
        'consumption': {
            'fieldname': 'consumption',
            'resolution': 0.88,
            'seed': 1,
            'name': 'consumption',
            'type': 'RandomDistributedScalarEncoder'
        }
    }

    encoder = MultiEncoder()
    encoder.addMultipleEncoders(encoderParams)
    sensorRegion.encoder = encoder

    # Add data source.
    testDir = os.path.dirname(os.path.abspath(__file__))
    inputFile = os.path.join(testDir, 'fixtures', 'gymdata-test.csv')
    dataSource = FileRecordStream(streamID=inputFile)
    sensorRegion.dataSource = dataSource

    # Get and set what field index we want to predict.
    network.regions['sensor'].setParameter('predictedField', 'consumption')

    return network
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")
def _createNetwork():
  """Create network with one RecordSensor region."""
  network = Network()
  network.addRegion('sensor', 'py.RecordSensor', '{}')
  sensorRegion = network.regions['sensor'].getSelf()

  # Add an encoder.
  encoderParams = {'consumption': {'fieldname': 'consumption',
                                   'resolution': 0.88,
                                   'seed': 1,
                                   'name': 'consumption',
                                   'type': 'RandomDistributedScalarEncoder'}}

  encoder = MultiEncoder()
  encoder.addMultipleEncoders(encoderParams)
  sensorRegion.encoder = encoder

  # Add a data source.
  testDir = os.path.dirname(os.path.abspath(__file__))
  inputFile = os.path.join(testDir, 'fixtures', 'gymdata-test.csv')
  dataSource = FileRecordStream(streamID=inputFile)
  sensorRegion.dataSource = dataSource

  # Get and set what field index we want to predict.
  network.regions['sensor'].setParameter('predictedField', 'consumption')

  return network
Exemple #12
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")
  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 #14
0
def createNetwork(dataSource):

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

    return network
Exemple #15
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(dataSource, rdse_resolution, cellsPerMiniColumn=32):
    """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 TMRegion.

    :param dataSource: a RecordStream instance to get data from
    :param cellsPerMiniColumn: int, number of cells per mini-column. Default=32
    :returns: a Network instance ready to run
    """
    try:
        with open(_PARAMS_PATH, "r") as f:
            modelParams = yaml.safe_load(f)["modelParams"]
    except:
        with open(os.path.join("..", _PARAMS_PATH), "r") as f:
            modelParams = yaml.safe_load(f)["modelParams"]

    # Create a network that will hold the regions.
    network = Network()

    # Add a sensor region.
    network.addRegion("sensor", "py.RecordSensor", '{}')

    # Set the encoder and data source of the sensor region.
    sensorRegion = network.regions["sensor"].getSelf()
    #sensorRegion.encoder = createEncoder(modelParams["sensorParams"]["encoders"])
    sensorRegion.encoder = createEncoder(rdse_resolution)
    sensorRegion.dataSource = dataSource

    # Make sure the SP input width matches the sensor region output width.
    modelParams["spParams"]["inputWidth"] = sensorRegion.encoder.getWidth()
    modelParams["tmParams"]["cellsPerColumn"] = cellsPerMiniColumn

    # Add SP and TM regions.
    network.addRegion("spatialPoolerRegion", "py.SPRegion",
                      json.dumps(modelParams["spParams"]))
    network.addRegion("temporalPoolerRegion", "py.TMRegion",
                      json.dumps(modelParams["tmParams"]))

    # Add a classifier region.
    clName = "py.%s" % modelParams["clParams"].pop("regionName")
    network.addRegion("classifier", clName,
                      json.dumps(modelParams["clParams"]))

    # Add all links
    createSensorToClassifierLinks(network, "sensor", "classifier")
    createDataOutLink(network, "sensor", "spatialPoolerRegion")
    createFeedForwardLink(network, "spatialPoolerRegion",
                          "temporalPoolerRegion")
    createFeedForwardLink(network, "temporalPoolerRegion", "classifier")
    # Reset links are optional, since the sensor region does not send resets.
    createResetLink(network, "sensor", "spatialPoolerRegion")
    createResetLink(network, "sensor", "temporalPoolerRegion")

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

    return network
Exemple #17
0
  def testGetSelf(self):
    # Create network
    net = Network()

    # Add sensor
    sensor = net.addRegion("sensor", "py.ImageSensor",
        "{width: 100, height: 50}")
    pysensor = sensor.getSelf()

    # Verify set parameters
    self.assertEqual(type(pysensor), ImageSensor)
    self.assertEqual(pysensor.height, 50)
    self.assertEqual(pysensor.width, 100)

    self.assertEqual(pysensor.width, sensor.getParameter('width'))
    self.assertEqual(pysensor.height, sensor.getParameter('height'))

    sensor.setParameter('width', 444)
    sensor.setParameter('height', 444)
    self.assertEqual(pysensor.width, 444)
    self.assertEqual(pysensor.height, 444)

    # Verify py object is not a copy
    sensor.getSelf().height = 100
    sensor.getSelf().width = 200
    self.assertEqual(pysensor.height, 100)
    self.assertEqual(pysensor.width, 200)

    pysensor.height = 50
    pysensor.width = 100
    self.assertEqual(sensor.getSelf().height, 50)
    self.assertEqual(sensor.getSelf().width, 100)
Exemple #18
0
    def testArrays(self):
        arrays = [
            ("real32ArrayParam",
             [0 * 32, 1 * 32, 2 * 32, 3 * 32, 4 * 32, 5 * 32, 6 * 32,
              7 * 32], "Real32"),
            ("int64ArrayParam", [0 * 64, 1 * 64, 2 * 64, 3 * 64], "Int64")
        ]

        n = Network()
        l1 = n.addRegion("l1", "TestNode", "")

        for paramName, initval, paramtype in arrays:
            x = l1.getParameter(paramName)
            self.assertTrue(isinstance(x,
                                       nupic.bindings.engine_internal.Array))
            self.assertEqual(x.getType(), paramtype)
            self.assertEqual(len(x), len(initval))
            for i in range(len(x)):
                self.assertEqual(x[i], initval[i])

            for i in range(len(x)):
                x[i] = x[i] * 2
            l1.setParameter(paramName, x)

            x = l1.getParameter(paramName)
            self.assertTrue(isinstance(x,
                                       nupic.bindings.engine_internal.Array))
            self.assertEqual(x.getType(), paramtype)
            self.assertEqual(len(x), len(initval))
            for i in range(len(x)):
                self.assertEqual(x[i], 2 * initval[i])
    def testGetSelf(self):
        # Create network
        net = Network()

        # Register ImageSensor region
        Network.registerRegion(ImageSensor)

        # Add sensor
        sensor = net.addRegion("sensor", "py.ImageSensor", "{width: 100, height: 50}")
        pysensor = sensor.getSelf()

        # Verify set parameters
        self.assertTrue(isinstance(pysensor, ImageSensor))
        self.assertEqual(pysensor.height, 50)
        self.assertEqual(pysensor.width, 100)

        self.assertEqual(pysensor.width, sensor.getParameter("width"))
        self.assertEqual(pysensor.height, sensor.getParameter("height"))

        sensor.setParameter("width", 444)
        sensor.setParameter("height", 444)
        self.assertEqual(pysensor.width, 444)
        self.assertEqual(pysensor.height, 444)

        # Verify py object is not a copy
        sensor.getSelf().height = 100
        sensor.getSelf().width = 200
        self.assertEqual(pysensor.height, 100)
        self.assertEqual(pysensor.width, 200)

        pysensor.height = 50
        pysensor.width = 100
        self.assertEqual(sensor.getSelf().height, 50)
        self.assertEqual(sensor.getSelf().width, 100)
Exemple #20
0
    def testGetSelf(self):
        # Create network
        net = Network()

        # Register ImageSensor region
        Network.registerRegion(ImageSensor)

        # Add sensor
        sensor = net.addRegion("sensor", "py.ImageSensor",
                               "{width: 100, height: 50}")
        pysensor = sensor.getSelf()

        # Verify set parameters
        self.assertTrue(isinstance(pysensor, ImageSensor))
        self.assertEqual(pysensor.height, 50)
        self.assertEqual(pysensor.width, 100)

        self.assertEqual(pysensor.width, sensor.getParameter('width'))
        self.assertEqual(pysensor.height, sensor.getParameter('height'))

        sensor.setParameter('width', 444)
        sensor.setParameter('height', 444)
        self.assertEqual(pysensor.width, 444)
        self.assertEqual(pysensor.height, 444)

        # Verify py object is not a copy
        sensor.getSelf().height = 100
        sensor.getSelf().width = 200
        self.assertEqual(pysensor.height, 100)
        self.assertEqual(pysensor.width, 200)

        pysensor.height = 50
        pysensor.width = 100
        self.assertEqual(sensor.getSelf().height, 50)
        self.assertEqual(sensor.getSelf().width, 100)
  def testScalars(self):
    scalars = [
      ("int32Param", 32, int, 35),
      ("uint32Param", 33, int, 36),
      ("int64Param", 64, long, 74),
      ("uint64Param", 65, long, 75),
      ("real32Param", 32.1, float, 33.1),
      ("real64Param", 64.1, float, 65.1),
      ("stringParam", "nodespec value", str, "new value")]

    n = Network()
    l1= n.addRegion("l1", "TestNode", "")
    x = l1.getParameter("uint32Param")

    for paramName, initval, paramtype, newval in scalars:
      # Check the initial value for each parameter.
      x = l1.getParameter(paramName)
      self.assertEqual(type(x), paramtype)
      if initval is None:
        continue
      if type(x) == float:
        self.assertTrue(abs(x  - initval) < 0.00001)
      else:
        self.assertEqual(x, initval)

      # Now set the value, and check to make sure the value is updated
      l1.setParameter(paramName, newval)
      x = l1.getParameter(paramName)
      self.assertEqual(type(x), paramtype)
      if type(x) == float:
        self.assertTrue(abs(x  - newval) < 0.00001)
      else:
        self.assertEqual(x, newval)
Exemple #22
0
    def testScalars(self):
        scalars = [("int32Param", 32, int, 35), ("uint32Param", 33, int, 36),
                   ("int64Param", 64, int, 74), ("uint64Param", 65, int, 75),
                   ("real32Param", 32.1, float, 33.1),
                   ("real64Param", 64.1, float, 65.1),
                   ("stringParam", "nodespec value", str, "new value")]

        n = Network()
        l1 = n.addRegion("l1", "TestNode", "")
        x = l1.getParameter("uint32Param")

        for paramName, initval, paramtype, newval in scalars:
            # Check the initial value for each parameter.
            x = l1.getParameter(paramName)
            self.assertEqual(type(x), paramtype)
            if initval is None:
                continue
            if type(x) == float:
                self.assertTrue(abs(x - initval) < 0.00001)
            else:
                self.assertEqual(x, initval)

            # Now set the value, and check to make sure the value is updated
            l1.setParameter(paramName, newval)
            x = l1.getParameter(paramName)
            self.assertEqual(type(x), paramtype)
            if type(x) == float:
                self.assertTrue(abs(x - newval) < 0.00001)
            else:
                self.assertEqual(x, newval)
  def testArrays(self):
    arrays = [
      ("real32ArrayParam",
        [0*32, 1*32, 2*32, 3*32, 4*32, 5*32, 6*32, 7*32],
       "Real32"),
      ("int64ArrayParam",
        [0*64, 1*64, 2*64, 3*64],
        "Int64")
    ]

    n = Network()
    l1= n.addRegion("l1", "TestNode", "")

    for paramName, initval, paramtype in arrays:
      x = l1.getParameter(paramName)
      self.assertTrue(isinstance(x, nupic.bindings.engine_internal.Array))
      self.assertEqual(x.getType(), paramtype)
      self.assertEqual(len(x), len(initval))
      for i in xrange(len(x)):
        self.assertEqual(x[i], initval[i])

      for i in xrange(len(x)):
        x[i] = x[i] * 2
      l1.setParameter(paramName, x)

      x = l1.getParameter(paramName)
      self.assertTrue(isinstance(x, nupic.bindings.engine_internal.Array))
      self.assertEqual(x.getType(), paramtype)
      self.assertEqual(len(x), len(initval))
      for i in xrange(len(x)):
        self.assertEqual(x[i], 2 * initval[i])
Exemple #24
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
Exemple #25
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 = findDataset("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 #26
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
  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 #29
0
    def testLoadImages(self):
        # Create a simple network with an ImageSensor. You can't actually run
        # the network because the region isn't connected to anything
        net = Network()
        Network.registerRegion(ImageSensor)
        net.addRegion("sensor", "py.ImageSensor", "{width: 32, height: 32}")
        sensor = net.regions['sensor']

        # 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 and check we loaded the correct number
        sensor.executeCommand(["loadMultipleImages", tmpDir])
        numImages = sensor.getParameter('numImages')
        self.assertEqual(numImages, 2)

        # Load a single image (this will replace the previous images)
        sensor.executeCommand(
            ["loadSingleImage",
             os.path.join(tmpDir, '1', 'im1.png')])
        numImages = sensor.getParameter('numImages')
        self.assertEqual(numImages, 1)

        # 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 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 #31
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 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 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 #34
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():
    """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
  def testLoadImages(self):
    # Create a simple network with an ImageSensor. You can't actually run
    # the network because the region isn't connected to anything
    net = Network()
    Network.registerRegion(ImageSensor)
    net.addRegion("sensor", "py.ImageSensor", "{width: 32, height: 32}")
    sensor = net.regions['sensor']

    # 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 and check we loaded the correct number
    sensor.executeCommand(["loadMultipleImages", tmpDir])
    numImages = sensor.getParameter('numImages')
    self.assertEqual(numImages, 2)

    # Load a single image (this will replace the previous images)
    sensor.executeCommand(["loadSingleImage",
                           os.path.join(tmpDir,'1','im1.png')])
    numImages = sensor.getParameter('numImages')
    self.assertEqual(numImages, 1)

    # 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 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)
Exemple #38
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 #39
0
    def testVaryingNumberOfCategories(self):
        # Setup network with sensor; max number of categories = 2
        net = Network()
        sensorRegion = net.addRegion("sensor", "py.RecordSensor",
                                     "{'numCategories': 2}")
        sensor = sensorRegion.getSelf()

        # Test for # of output categories = max
        data = {
            "_timestamp": None,
            "_category": [0, 1],
            "label": "0 1",
            "_sequenceId": 0,
            "y": 2.624902024,
            "x": 0.0,
            "_timestampRecordIdx": None,
            "_reset": 0
        }
        sensorOutput = numpy.array([0, 0], dtype="int32")
        sensor.populateCategoriesOut(data["_category"], sensorOutput)

        self.assertSequenceEqual([0, 1], sensorOutput.tolist(
        ), "Sensor failed to populate the array with record of two categories."
                                 )

        # Test for # of output categories > max
        data["_category"] = [1, 2, 3]
        sensorOutput = numpy.array([0, 0], dtype="int32")
        sensor.populateCategoriesOut(data["_category"], sensorOutput)

        self.assertSequenceEqual([1, 2], sensorOutput.tolist(
        ), "Sensor failed to populate the array w/ record of three categories."
                                 )

        # Test for # of output categories < max
        data["_category"] = [3]
        sensorOutput = numpy.array([0, 0], dtype="int32")
        sensor.populateCategoriesOut(data["_category"], sensorOutput)

        self.assertSequenceEqual(
            [3, -1], sensorOutput.tolist(),
            "Sensor failed to populate the array w/ record of one category.")

        # Test for no output categories
        data["_category"] = [None]
        sensorOutput = numpy.array([0, 0], dtype="int32")
        sensor.populateCategoriesOut(data["_category"], sensorOutput)

        self.assertSequenceEqual([-1, -1], sensorOutput.tolist(
        ), "Sensor failed to populate the array w/ record of zero categories.")
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 #41
0
  def testParameters(self):
    # Test setting and getting parameters
    net = Network()

    # Add sensor to the network
    sensor = net.addRegion("sensor", "py.ImageSensor",
        "{width: 100, height: 50}")

    # Verify get parameters
    self.assertEqual(sensor.getParameter('height'), 50)
    self.assertEqual(sensor.getParameter('width'), 100)

    # Verify set parameters
    sensor.setParameter('width', 42)
    self.assertEqual(sensor.getParameter('width'), 42)
Exemple #42
0
    def testParameters(self):
        # Test setting and getting parameters
        net = Network()

        # Add sensor to the network
        sensor = net.addRegion("sensor", "py.ImageSensor",
                               "{width: 100, height: 50}")

        # Verify get parameters
        self.assertEqual(sensor.getParameter('height'), 50)
        self.assertEqual(sensor.getParameter('width'), 100)

        # Verify set parameters
        sensor.setParameter('width', 42)
        self.assertEqual(sensor.getParameter('width'), 42)
Exemple #43
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")))
Exemple #44
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
    def testParameters(self):
        # Test setting and getting parameters
        net = Network()

        # Register ImageSensor region
        Network.registerRegion(ImageSensor)

        # Add sensor to the network
        sensor = net.addRegion("sensor", "py.ImageSensor", "{width: 100, height: 50}")

        # Verify get parameters
        self.assertEqual(sensor.getParameter("height"), 50)
        self.assertEqual(sensor.getParameter("width"), 100)

        # Verify set parameters
        sensor.setParameter("width", 42)
        self.assertEqual(sensor.getParameter("width"), 42)
Exemple #46
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
        }))

    return network
  def _testNetLoad(self):
    """Test loading a network with this sensor in it."""
    n = Network()
    r = n.addRegion(self.nodeName, self.sensorName, '{ activeOutputCount: 11}')
    r.dimensions = Dimensions([1])
    n.save(self.filename)

    n = Network(self.filename)
    n.initialize()
    self.testsPassed += 1

    # Check that vectorCount parameter is zero
    r = n.regions[self.nodeName]

    res = r.getParameter('vectorCount')
    self.assertEqual(
        res, 0, "getting vectorCount:\n Expected '0',  got back  '%d'\n" % res)

    self.sensor = r
Exemple #48
0
  def _testNetLoad(self):
    """Test loading a network with this sensor in it."""
    n = Network()
    r = n.addRegion(self.nodeName, self.sensorName, '{ activeOutputCount: 11}')
    r.dimensions = Dimensions([1])
    n.save(self.filename)

    n = Network(self.filename)
    n.initialize()
    self.testsPassed += 1

    # Check that vectorCount parameter is zero
    r = n.regions[self.nodeName]

    res = r.getParameter('vectorCount')
    self.assertEqual(
        res, 0, "getting vectorCount:\n Expected '0',  got back  '%d'\n" % res)

    self.sensor = r
  def testVaryingNumberOfCategories(self):
    # Setup network with sensor; max number of categories = 2
    net = Network()
    sensorRegion = net.addRegion(
        "sensor", "py.RecordSensor", "{'numCategories': 2}")
    sensor = sensorRegion.getSelf()

    # Test for # of output categories = max
    data = {"_timestamp": None, "_category": [0, 1], "label": "0 1",
            "_sequenceId": 0, "y": 2.624902024, "x": 0.0,
            "_timestampRecordIdx": None, "_reset": 0}
    sensorOutput = numpy.array([0, 0], dtype="int32")
    sensor.populateCategoriesOut(data["_category"], sensorOutput)
    
    self.assertSequenceEqual([0, 1], sensorOutput.tolist(),
        "Sensor failed to populate the array with record of two categories.")

    # Test for # of output categories > max
    data["_category"] = [1, 2, 3]
    sensorOutput = numpy.array([0, 0], dtype="int32")
    sensor.populateCategoriesOut(data["_category"], sensorOutput)
    
    self.assertSequenceEqual([1, 2], sensorOutput.tolist(),
        "Sensor failed to populate the array w/ record of three categories.")
    
    # Test for # of output categories < max
    data["_category"] = [3]
    sensorOutput = numpy.array([0, 0], dtype="int32")
    sensor.populateCategoriesOut(data["_category"], sensorOutput)
    
    self.assertSequenceEqual([3, -1], sensorOutput.tolist(),
        "Sensor failed to populate the array w/ record of one category.")
    
    # Test for no output categories
    data["_category"] = [None]
    sensorOutput = numpy.array([0, 0], dtype="int32")
    sensor.populateCategoriesOut(data["_category"], sensorOutput)

    self.assertSequenceEqual([-1, -1], sensorOutput.tolist(),
        "Sensor failed to populate the array w/ record of zero categories.")
Exemple #50
0
def createNetwork(dataSource):
    '''
  Create and initialize a network.
  '''

    with open(_PARAMS_PATH, "r") as f:
        modelParams = yaml.safe_load(f)["modelParams"]

    # Create a network that will hold the regions.
    network = Network()

    # Add a sensor region.
    network.addRegion("sensor", "py.RecordSensor", "{}")

    # Set the encoder and data source of the sensor region.
    sensorRegion = network.regions["sensor"].getSelf()
    sensorRegion.encoder = createEncoder(
        modelParams["sensorParams"]["encoders"])
    sensorRegion.dataSource = dataSource

    # Make sure the SP input width matches the sensor region output width.
    modelParams["spParams"]["inputWidth"] = sensorRegion.encoder.getWidth()

    # Add SP and TP regions.
    network.addRegion("SP", "py.SPRegion", json.dumps(modelParams["spParams"]))
    network.addRegion("TM", "py.TMRegion", json.dumps(modelParams["tmParams"]))

    # Add a classifier region.
    clName = "py.%s" % modelParams["clParams"].pop("regionName")
    network.addRegion("classifier", clName,
                      json.dumps(modelParams["clParams"]))

    classifierRegion = network.regions["classifier"].getSelf()

    # Add all links
    createSensorToClassifierLinks(network, "sensor", "classifier")
    createDataOutLink(network, "sensor", "SP")
    createFeedForwardLink(network, "SP", "TM")
    createFeedForwardLink(network, "TM", "classifier")
    # Reset links are optional, since the sensor region does not send resets.
    createResetLink(network, "sensor", "SP")
    createResetLink(network, "sensor", "TM")

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

    return network
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 createNetwork(dataSource):
  """Create and initialize a network."""
  with open(_PARAMS_PATH, "r") as f:
    modelParams = yaml.safe_load(f)["modelParams"]

  # Create a network that will hold the regions.
  network = Network()

  # Add a sensor region.
  network.addRegion("sensor", "py.RecordSensor", '{}')

  # Set the encoder and data source of the sensor region.
  sensorRegion = network.regions["sensor"].getSelf()
  sensorRegion.encoder = createEncoder(modelParams["sensorParams"]["encoders"])
  sensorRegion.dataSource = dataSource

  # Make sure the SP input width matches the sensor region output width.
  modelParams["spParams"]["inputWidth"] = sensorRegion.encoder.getWidth()

  # Add SP and TM regions.
  network.addRegion("SP", "py.SPRegion", json.dumps(modelParams["spParams"]))
  network.addRegion("TM", "py.TMRegion", json.dumps(modelParams["tmParams"]))

  # Add a classifier region.
  clName = "py.%s" % modelParams["clParams"].pop("regionName")
  network.addRegion("classifier", clName, json.dumps(modelParams["clParams"]))

  # Add all links
  createSensorToClassifierLinks(network, "sensor", "classifier")
  createDataOutLink(network, "sensor", "SP")
  createFeedForwardLink(network, "SP", "TM")
  createFeedForwardLink(network, "TM", "classifier")
  # Reset links are optional, since the sensor region does not send resets.
  createResetLink(network, "sensor", "SP")
  createResetLink(network, "sensor", "TM")

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

  return network
Exemple #53
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 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 #55
0

  def getOutputElementCount(self, outputName):
    """Returns the width of dataOut."""

    # Check if classifier has a 'maxCategoryCount' attribute
    if not hasattr(self, "maxCategoryCount"):
      # Large default value for backward compatibility 
      self.maxCategoryCount = 1000

    if outputName == "categoriesOut":
      return len(self.stepsList)
    elif outputName == "probabilities":
      return len(self.stepsList) * self.maxCategoryCount
    elif outputName == "actualValues":
      return self.maxCategoryCount
    else:
      raise ValueError("Unknown output {}.".format(outputName))



if __name__ == "__main__":
  from nupic.engine import Network

  n = Network()
  classifier = n.addRegion(
    'classifier',
    'py.CLAClassifierRegion',
    '{ steps: "1,2", maxAge: 1000}'
  )
    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)