Ejemplo n.º 1
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.")
Ejemplo n.º 2
0
    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 _deSerializeExtraData(self, extraDataDir):
    """
    Protected method that is called during deserialization (after __setstate__)
    with an external directory path. We override it here to load the Network API
    instance.

    @param extraDataDir (string) Model's extra data directory path
    """

    # Must call this before loading any regions in the research repo
    registerAllResearchRegions()

    self.network = Network(os.path.join(extraDataDir, "network.nta"))
    self._initializeRegionHelpers()
def plotPermanences(network = None, savedNetworkFile = "mnist_net.nta",
                    columnList = None, iteration=0):
  """
  Plots the permanences of the top columns into a single master image
  If columnList is specified, uses those columns otherwise extracts the
  most active columns from the spatial pooler using duty cycle.
  """
  # Get the spatial pooler from the network, otherwise read it from checkpoint.
  if network is None:
    network = Network(savedNetworkFile)
  spRegion = network.regions["SP"]
  spSelf = spRegion.getSelf()
  sp = spSelf._sfdr

  # If we are not given a column list, retrieve columns with highest duty cycles
  dutyCycles = numpy.zeros(sp.getNumColumns(), dtype=GetNTAReal())
  sp.getActiveDutyCycles(dutyCycles)
  if columnList is None:
    mostActiveColumns = list(dutyCycles.argsort())
    mostActiveColumns.reverse()
    columnList = mostActiveColumns[0:400]
    #print columnList

  # Create empty master image with the top 25 columns. We will paste
  # individual column images into this image
  numImagesPerRowInMaster = 20
  masterImage = Image.new("L",((32+2)*numImagesPerRowInMaster,
                               (32+2)*numImagesPerRowInMaster),255)

  for rank,col in enumerate(columnList):
    #print "Col=",col,"rank=",rank,"dutyCycle=",dutyCycles[col]
    pyPerm = numpy.zeros(sp.getNumInputs(), dtype=GetNTAReal())
    sp.getPermanence(col,pyPerm)

    # Create small image for each column
    pyPerm = pyPerm/pyPerm.max()
    pyPerm = (pyPerm*255.0)
    pyPerm = pyPerm.reshape((32,32))
    pyPerm = (pyPerm).astype('uint8')
    img = Image.fromarray(pyPerm)

    # Paste it into master image
    if rank < numImagesPerRowInMaster*numImagesPerRowInMaster:
      x = rank%numImagesPerRowInMaster*(32+2)
      y = (rank/numImagesPerRowInMaster)*(32+2)
      masterImage.paste(img,(x,y))

  # Save master image
  masterImage.save("master_%05d.png"%(iteration))
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
def createNetwork(networkConfig):
    """
  Create and initialize the specified network instance.

  @param networkConfig: (dict) the configuration of this network.
  @return network: (Network) The actual network
  """

    registerAllResearchRegions()

    network = Network()

    if networkConfig["networkType"] == "L4L2Column":
        return createL4L2Column(network, networkConfig, "_0")
    elif networkConfig["networkType"] == "MultipleL4L2Columns":
        return createMultipleL4L2Columns(network, networkConfig)
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
    def testSaveAndReload(self):
        """
    This function tests saving and loading. It will train a network for 500
    iterations, then save it and reload it as a second network instance. It will
    then run both networks for 100 iterations and ensure they return identical
    results.
    """

        print "Creating network..."

        netOPF = _createOPFNetwork()
        level1OPF = netOPF.regions['level1SP']

        # ==========================================================================
        print "Training network for 500 iterations"
        level1OPF.setParameter('learningMode', 1)
        level1OPF.setParameter('inferenceMode', 0)
        netOPF.run(500)
        level1OPF.setParameter('learningMode', 0)
        level1OPF.setParameter('inferenceMode', 1)

        # ==========================================================================
        # Save network and reload as a second instance. We need to reset the data
        # source for the unsaved network so that both instances start at the same
        # place
        print "Saving and reload network"
        _, tmpNetworkFilename = _setupTempDirectory("trained.nta")
        netOPF.save(tmpNetworkFilename)
        netOPF2 = Network(tmpNetworkFilename)
        level1OPF2 = netOPF2.regions['level1SP']

        sensor = netOPF.regions['sensor'].getSelf()
        trainFile = resource_filename("nupic.datafiles", "extra/gym/gym.csv")
        sensor.dataSource = FileRecordStream(streamID=trainFile)
        sensor.dataSource.setAutoRewind(True)

        # ==========================================================================
        print "Running inference on the two networks for 100 iterations"
        for _ in xrange(100):
            netOPF2.run(1)
            netOPF.run(1)
            l1outputOPF2 = level1OPF2.getOutputData("bottomUpOut")
            l1outputOPF = level1OPF.getOutputData("bottomUpOut")
            opfHash2 = l1outputOPF2.nonzero()[0].sum()
            opfHash = l1outputOPF.nonzero()[0].sum()

            self.assertEqual(opfHash2, opfHash)
Ejemplo n.º 9
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 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
Ejemplo n.º 11
0
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
Ejemplo n.º 12
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
Ejemplo n.º 13
0
  def loadFromFile(self, filename):
    """ Load a serialized network
    :param filename: Where the network should be loaded from
    """
    print "Loading network from {file}...".format(file=filename)
    Network.unregisterRegion(SaccadeSensor.__name__)
    Network.registerRegion(SaccadeSensor)

    Network.registerRegion(ExtendedTMRegion)

    self.net = Network(filename)

    self.networkSensor = self.net.regions["sensor"]
    self.networkSensor.setParameter("numSaccades", SACCADES_PER_IMAGE_TESTING)

    self.networkSP = self.net.regions["SP"]
    self.networkClassifier = self.net.regions["classifier"]

    self.numCorrect = 0
  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)
Ejemplo n.º 15
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
Ejemplo n.º 17
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 createNetwork(args):
  """
  Create the network instance with regions for the sensor, SP, TM, and
  classifier. Before running, be sure to init w/ network.initialize().

  @param args                   (dataSource, sensorType, encoders), more info:
    dataSource   (RecordStream) Sensor region reads data from here.
    sensorType   (str)          Specific type of region, e.g. "py.RecordSensor";
                                possible options can be found in nupic/regions/.
    classifierType   (str)      Specific type of classifier region, e.g. "py.CLAClassifier";
                                possible options can be found in nupic/regions/.
    encoders     (dict)         See createEncoder() docstring for format.

  @return        (Network)      sensor -> SP -> TM -> CLA classifier
  """
  network = Network()

  createRegions(network, args)

  linkRegions(network)

  return network
Ejemplo n.º 19
0
def inspect(element, showRun=True, icon=None):
    """
  Launch an Inspector for the provided element.

  element -- A network, region or a path to a network directory.
  showRun -- Whether to show the RuntimeInspector in the dropdown, which lets
             the user run the network.
  """
    if isinstance(element, basestring):
        element = Network(element)
    else:
        assert isinstance(element, Network)

    if len(element.regions) == 0:
        raise Exception('Unable to inspect an empty network')

    # Network must be initialized before it can be inspected
    element.initialize()

    from wx import GetApp, PySimpleApp

    if GetApp():
        useApp = True
    else:
        useApp = False

    from nupic.analysis.inspectors.MultiInspector import MultiInspector

    if not useApp:
        app = PySimpleApp()

    inspector = MultiInspector(element=element, showRun=showRun, icon=icon)

    if not useApp:
        app.MainLoop()
        app.Destroy()
    else:
        return inspector
Ejemplo n.º 20
0
def testNetwork(testPath="mnist/testing", savedNetworkFile="mnist_net.nta"):
    net = Network(savedNetworkFile)
    sensor = net.regions["sensor"]
    sp = net.regions["SP"]
    classifier = net.regions["classifier"]

    print "Reading test images"
    sensor.executeCommand(["loadMultipleImages", testPath])
    numTestImages = sensor.getParameter("numImages")
    print "Number of test images", numTestImages

    start = time.time()

    # Various region parameters
    sensor.setParameter("explorer",
                        yaml.dump(["RandomFlash", {
                            "replacement": False
                        }]))
    classifier.setParameter("inferenceMode", 1)
    classifier.setParameter("learningMode", 0)
    sp.setParameter("inferenceMode", 1)
    sp.setParameter("learningMode", 0)

    numCorrect = 0
    for i in range(numTestImages):
        net.run(1)
        inferredCategory = classifier.getOutputData("categoriesOut").argmax()
        if sensor.getOutputData("categoryOut") == inferredCategory:
            numCorrect += 1
        if i % (numTestImages / 100) == 0:
            print "Iteration", i, "numCorrect=", numCorrect

    # Some interesting statistics
    print "Testing time:", time.time() - start
    print "Number of test images", numTestImages
    print "num correct=", numCorrect
    print "pct correct=", (100.0 * numCorrect) / numTestImages
Ejemplo n.º 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")))
Ejemplo n.º 22
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)
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
Ejemplo n.º 25
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
Ejemplo n.º 26
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
    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)
Ejemplo n.º 28
0
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
Ejemplo n.º 29
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
Ejemplo n.º 30
0
    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)