Exemplo n.º 1
0
    def testGetParameters(self):
        """
    A test of the getParameters( ) and getParameterJSON() functions.
    """
        expected = """{
  "activeBits": 200,
  "category": false,
  "noise": 0.010000,
  "radius": 0.030000,
  "resolution": 0.000150,
  "seed": 2019,
  "sensedValue": 0.000000,
  "size": 1000,
  "sparsity": 0.200000
}"""
        net = Network()
        encoder = net.addRegion(
            "encoder", "RDSEEncoderRegion",
            "{size: 1000, sparsity: 0.2, radius: 0.03, seed: 2019, noise: 0.01}"
        )
        json_str = encoder.getParameters()
        self.assertEqual(json_str, expected)

        json.loads(json_str)  # test if json package can load it

        json_str = encoder.getParameterJSON("activeBits", False)
        self.assertEqual(json_str, "200")
        json_str = encoder.getParameterJSON("activeBits", True)
        self.assertEqual(json_str, "{\"value\": 200, \"type\": \"UInt32\"}")
Exemplo n.º 2
0
    def testSPRegionIsCreatable(self):
        """
        Test that the SPRegion can be created in Python.
        """
        net = Network()

        # Create simple region to pass sensor commands as displacement vectors (dx, dy)
        sp = net.addRegion("sp", "SPRegion", json.dumps({}))
Exemplo n.º 3
0
    def testSPRegionParametersAreWritable(self):
        """
        Test that the SPRegion parameters can be set.
        """
        net = Network()

        # Create simple region to pass sensor commands as displacement vectors (dx, dy)
        sp = net.addRegion("sp", "SPRegion", json.dumps(spParams))
Exemplo n.º 4
0
    def testGetSpec(self):
        """
    Test of region.getSpec() function. Testing if pybind pointers are correctly handled
    """
        net = Network()
        dateRegion = net.addRegion(
            'dateEncoder', 'DateEncoderRegion',
            str(dict(timeOfDay_width=30, timeOfDay_radius=1,
                     weekend_width=21)))

        dateRegion.getSpec()  # twice times to check if no double free arises
        dateRegion.getSpec()
Exemplo n.º 5
0
def registerAdvancedRegion(regionTypeName, moduleName=None):
    """
    Register this region so that NuPIC can later find it.

    @param regionTypeName: (str) type name of the region. E.g LanguageSensor.
    @param moduleName: (str) location of the region class, only needed if
        registering a region that is outside the expected "regions/" dir.
    """
    if moduleName is None:
        # the region is located in the regions/ directory
        moduleName = "htm.advanced.regions." + regionTypeName
    # Add new region class to the network.
    module = __import__(moduleName, {},{}, [regionTypeName], 0)
    Network.registerPyRegion(module.__name__, regionTypeName)
Exemplo n.º 6
0
    def __init__(self, numColumns, L2Params, L4Params, L6aParams, repeat, logCalls=False):
        """
        Create a network consisting of multiple columns. Each column contains one L2,
        one L4 and one L6a layers. In addition all the L2 columns are fully
        connected to each other through their lateral inputs.

        :param numColumns: Number of columns to create
        :type numColumns: int
        :param L2Params: constructor parameters for :class:`ColumnPoolerRegion`
        :type L2Params: dict
        :param L4Params:    constructor parameters for :class:`ApicalTMPairRegion`
        :type L4Params: dict
        :param L6aParams: constructor parameters for :class:`GridCellLocationRegion`
        :type L6aParams: dict
        :param repeat: Number of times each pair should be seen to be learned
        :type repeat: int
        :param logCalls: If true, calls to main functions will be logged internally.
                                         The log can then be saved with saveLogs(). This allows us
                                         to recreate the complete network behavior using
                                         rerunExperimentFromLogfile which is very useful for
                                         debugging.
        :type logCalls: bool
        """
        # Handle logging - this has to be done first
        self.logCalls = logCalls

        self.numColumns = numColumns
        self.repeat = repeat

        network = Network()
        self.network = createMultipleL246aLocationColumn(network=network,
                                                         numberOfColumns=self.numColumns,
                                                         L2Params=L2Params,
                                                         L4Params=L4Params,
                                                         L6aParams=L6aParams)
        network.initialize()

        self.sensorInput = []
        self.motorInput = []
        self.L2Regions = []
        self.L4Regions = []
        self.L6aRegions = []
        for i in range(self.numColumns):
            col = str(i)
            self.sensorInput.append(network.getRegion("sensorInput_" + col))
            self.motorInput.append(network.getRegion("motorInput_" + col))
            self.L2Regions.append(network.getRegion("L2_" + col))
            self.L4Regions.append(network.getRegion("L4_" + col))
            self.L6aRegions.append(network.getRegion("L6a_" + col))

        if L6aParams is not None and "dimensions" in L6aParams:
            self.dimensions = L6aParams["dimensions"]
        else:
            self.dimensions = 2

        self.sdrSize = L2Params["sdrSize"]

        # will be populated during training
        self.learnedObjects = {}
Exemplo n.º 7
0
    def _create_network(self, L4Params, L6aParams):
        """
        Constructor.
        """
        network = Network()
        
        # Create network
        network = createL4L6aLocationColumn(network=network,
                                            L4Params=L4Params,
                                            L6aParams=L6aParams,
                                            inverseReadoutResolution=None,
                                            baselineCellsPerAxis=6,
                                            suffix="")

        network.initialize()
        return network
Exemplo n.º 8
0
    def testEmptyQueue(self):
        """
        Test that RawValues detects empty queue.
        """
        net = Network()

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

        try:
            net.run(1)
            output = list(np.array(motor.getOutputArray("dataOut")))
            self.assertEqual([0, 0], output)
            self.fail("Empty queue should throw exception")
        except:
            pass
Exemplo n.º 9
0
    def testExecuteCommandList(self):
        """
        Test that execute command executes the correct command with a list.
        """
        net = Network()

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

        net.run(1)

        output = list(np.array(motor.getOutputArray("dataOut")))
        self.assertEqual([0, 0], output)
        output = list(np.array(motor.getOutputArray("resetOut")))
        self.assertEqual([False], output)
Exemplo n.º 10
0
    def testExecuteCommandListWithReset(self):
        """
        Test that execute command executes the correct command with a list and reset set.
        """
        net = Network()

        # Create simple region to pass sensor commands as displacement vectors (dx, dy)
        sensor = net.addRegion("sensor", "py.RawSensor",
                               json.dumps({"outputWidth": 8}))
        sensor.executeCommand('addDataToQueue', [0, 1], 1, 0)

        net.run(1)

        output = list(np.array(sensor.getOutputArray("dataOut")))
        self.assertEqual([1, 1, 0, 0, 0, 0, 0, 0], output)
        output = list(np.array(sensor.getOutputArray("resetOut")))
        self.assertEqual([True], output)
Exemplo n.º 11
0
  def testNetwork(self):
    """
    A simple NetworkAPI example with three regions.
    ///////////////////////////////////////////////////////////////
    //
    //                          .------------------.
    //                         |    encoder        |
    //        sinewave data--->|(RDSEEncoderRegion)|
    //                         |                   |
    //                         `-------------------'
    //                                 |
    //                         .-----------------.
    //                         |     sp          |
    //                         |  (SPRegion)     |
    //                         |                 |
    //                         `-----------------'
    //                                 |
    //                         .-----------------.
    //                         |      tm         |
    //                         |   (TMRegion)    |---->anomaly score
    //                         |                 |
    //                         `-----------------'
    //
    //////////////////////////////////////////////////////////////////
    """
    
    """ Creating network instance. """
    config = """
        {network: [
            {addRegion: {name: "encoder", type: "RDSEEncoderRegion", params: {size: 1000, sparsity: 0.2, radius: 0.03, seed: 2019, noise: 0.01}}},
            {addRegion: {name: "sp", type: "SPRegion", params: {columnCount: 2048, globalInhibition: true}}},
            {addRegion: {name: "tm", type: "TMRegion", params: {cellsPerColumn: 8, orColumnOutputs: true}}},
            {addLink:   {src: "encoder.encoded", dest: "sp.bottomUpIn"}},
            {addLink:   {src: "sp.bottomUpOut", dest: "tm.bottomUpIn"}}
        ]}"""
    net = Network()
    net.configure(config)
    
    # iterate EPOCHS times
    x = 0.00
    for e in range(EPOCHS):
      # Data is a sine wave,    (Note: first iteration is for x=0.01, not 0)
      x += 0.01  # advance one step, 0.01 radians
      s = math.sin(x)   # compute current sine as data.
      #  feed data to RDSE encoder via its "sensedValue" parameter.
      net.getRegion('encoder').setParameterReal64('sensedValue', s)
      net.run(1)  # Execute one iteration of the Network object

    # Retreive the final anomaly score from the TM object's 'anomaly' output. (as a single element numpy array)
    score = np.array(net.getRegion('tm').getOutputArray('anomaly'))
    self.assertEqual(score, [1])
Exemplo n.º 12
0
    def testGetParametersCustomRegions(self):

        Network.cleanup()  # removes all previous registrations
        registerAllAdvancedRegions()
        json_list = Network.getRegistrations()
        #print(json_list)
        y = json.loads(json_list)
        self.assertTrue("py.ColumnPoolerRegion" in y)

        net = Network()
        #print(net.getSpecJSON("py.ColumnPoolerRegion"))
        cp = net.addRegion(
            "py.ColumnPoolerRegion", "py.ColumnPoolerRegion", """{ 
	"activationThresholdDistal": 20,
	"cellCount": 4096,
	"connectedPermanenceDistal": 0.5,
	"connectedPermanenceProximal": 0.5,
	"initialDistalPermanence": 0.51,
	"initialProximalPermanence": 0.6,
	"minThresholdProximal": 5,
	"sampleSizeDistal": 30,
	"sampleSizeProximal": 10,
	"sdrSize": 40,
	"synPermDistalDec": 0.001,
	"synPermDistalInc": 0.1,
	"synPermProximalDec": 0.001,
	"synPermProximalInc": 0.1
}""")

        # expected results from getParameters()  (in Spec order)
        expected = """{
  "learningMode": true,
  "onlineLearning": false,
  "cellCount": 4096,
  "inputWidth": 16384,
  "numOtherCorticalColumns": 0,
  "sdrSize": 40,
  "maxSdrSize": 0,
  "minSdrSize": 0,
  "synPermProximalInc": 0.100000,
  "synPermProximalDec": 0.001000,
  "initialProximalPermanence": 0.600000,
  "sampleSizeProximal": 10,
  "minThresholdProximal": 5,
  "connectedPermanenceProximal": 0.500000,
  "predictedInhibitionThreshold": 20.000000,
  "synPermDistalInc": 0.100000,
  "synPermDistalDec": 0.001000,
  "initialDistalPermanence": 0.510000,
  "sampleSizeDistal": 30,
  "activationThresholdDistal": 20,
  "connectedPermanenceDistal": 0.500000,
  "inertiaFactor": 1.000000,
  "seed": 42,
  "defaultOutputType": "active"
}"""

        json_list = cp.getParameters()
        #print(json_list)
        self.assertEqual(json_list, expected)
Exemplo n.º 13
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.getParameterUInt32("outputWidth"), l2c.getParameterUInt32("inputWidth"), "Incorrect outputWidth parameter")

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

        # Run the network and check outputs are as expected
        net.run(3)
Exemplo n.º 14
0
    def testSequence(self):
        """
        Test that RawSensor executes correctly.
        """
        net = Network()

        # Create simple region to pass sensor commands as displacement vectors (dx, dy)
        sensor = net.addRegion("sensor", "py.RawSensor",
                               json.dumps({"outputWidth": 8}))
        sensor.executeCommand('addDataToQueue', [0, 1], 1, 0)
        sensor.executeCommand('addDataToQueue', [1, 2], 0, 1)
        sensor.executeCommand('addDataToQueue', [2, 3], 0, 2)

        net.run(1)

        output = list(np.array(sensor.getOutputArray("dataOut")))
        self.assertEqual([1, 1, 0, 0, 0, 0, 0, 0], output)
        output = list(np.array(sensor.getOutputArray("resetOut")))
        self.assertEqual([True], output)
        output = list(np.array(sensor.getOutputArray("sequenceIdOut")))
        self.assertEqual([0], output)

        net.run(1)

        output = list(np.array(sensor.getOutputArray("dataOut")))
        self.assertEqual([0, 1, 1, 0, 0, 0, 0, 0], output)
        output = list(np.array(sensor.getOutputArray("resetOut")))
        self.assertEqual([False], output)
        output = list(np.array(sensor.getOutputArray("sequenceIdOut")))
        self.assertEqual([1], output)
        net.run(1)

        output = list(np.array(sensor.getOutputArray("dataOut")))
        self.assertEqual([0, 0, 1, 1, 0, 0, 0, 0], output)
        output = list(np.array(sensor.getOutputArray("resetOut")))
        self.assertEqual([False], output)
        output = list(np.array(sensor.getOutputArray("sequenceIdOut")))
        self.assertEqual([2], output)
Exemplo n.º 15
0
    def testGetParametersGridCell(self):
        # a test of arrays in parameters
        Network.cleanup()  # removes all previous registrations
        registerAllAdvancedRegions()
        json_list = Network.getRegistrations()
        #print(json_list)
        y = json.loads(json_list)
        self.assertTrue("py.GridCellLocationRegion" in y)

        # only provide one orentation to shorten the test.  Full test in location_region_test.py
        net = Network()
        #print(net.getSpecJSON("py.GridCellLocationRegion"))
        cp = net.addRegion(
            "grid", "py.GridCellLocationRegion", """{ 
  "moduleCount": 1,
  "cellsPerAxis": 10,
  "scale": [1],
  "orientation": [0.5],
  "anchorInputSize": 1,
  "activeFiringRate": 10 
}""")

        # expected results from getParameters()  (in Spec order)
        expected = """{
  "moduleCount": 1,
  "cellsPerAxis": 10,
  "scale": [1],
  "orientation": [0.5],
  "anchorInputSize": 1,
  "activeFiringRate": 10.000000,
  "bumpSigma": 0.181720,
  "activationThreshold": 10,
  "initialPermanence": 0.210000,
  "connectedPermanence": 0.500000,
  "learningThreshold": 10,
  "sampleSize": 20,
  "permanenceIncrement": 0.100000,
  "permanenceDecrement": 0.000000,
  "maxSynapsesPerSegment": -1,
  "bumpOverlapMethod": "probabilistic",
  "learningMode": false,
  "dualPhase": true,
  "dimensions": 2,
  "seed": 42
}"""

        json_list = cp.getParameters()
        #print(json_list)
        self.assertEqual(json_list, expected)
    def testCreateL4L6aLocationColumn(self):
        """
        Test 'createL4L6aLocationColumn' by inferring a set of hand crafted objects
        """
        scale = []
        orientation = []
        # Initialize L6a location region with 5 modules varying scale by sqrt(2) and
        # 4 different random orientations for each scale
        for i in range(5):
            for _ in range(4):
                angle = np.radians(random.gauss(7.5, 7.5))
                orientation.append(random.choice([angle, -angle]))
                scale.append(10.0 * (math.sqrt(2)**i))

        net = Network()
        createL4L6aLocationColumn(net,
                                  L4Params={
                                      "columnCount": NUM_OF_COLUMNS,
                                      "cellsPerColumn": CELLS_PER_COLUMN,
                                      "activationThreshold": 15,
                                      "minThreshold": 15,
                                      "initialPermanence": 1.0,
                                      "implementation": "ApicalTiebreak",
                                      "maxSynapsesPerSegment": -1
                                  },
                                  L6aParams={
                                      "moduleCount": len(scale),
                                      "scale": scale,
                                      "orientation": orientation,
                                      "anchorInputSize": NUM_OF_CELLS,
                                      "activationThreshold": 8,
                                      "initialPermanence": 1.0,
                                      "connectedPermanence": 0.5,
                                      "learningThreshold": 8,
                                      "sampleSize": 10,
                                      "permanenceIncrement": 0.1,
                                      "permanenceDecrement": 0.0,
                                      "bumpOverlapMethod": "probabilistic"
                                  },
                                  inverseReadoutResolution=8)
        net.initialize()

        L6a = net.getRegion('L6a')
        sensor = net.getRegion('sensorInput')
        motor = net.getRegion('motorInput')

        # Keeps a list of learned objects
        learnedRepresentations = defaultdict(list)

        # Learn Objects
        self._setLearning(net, True)

        for objectDescription in OBJECTS:
            reset = True
            previousLocation = None
            L6a.executeCommand("activateRandomLocation")

            for iFeature, feature in enumerate(objectDescription["features"]):
                # Move the sensor to the center of the object
                locationOnObject = np.array([
                    feature["top"] + feature["height"] / 2.,
                    feature["left"] + feature["width"] / 2.
                ])

                # Calculate displacement from previous location
                if previousLocation is not None:
                    motor.executeCommand(
                        'addDataToQueue',
                        list(locationOnObject - previousLocation))
                else:
                    motor.executeCommand('addDataToQueue', [0, 0])
                previousLocation = locationOnObject

                # Sense feature at location
                sensor.executeCommand('addDataToQueue',
                                      FEATURE_ACTIVE_COLUMNS[feature["name"]],
                                      reset, 0)
                net.run(1)
                reset = False

                # Save learned representations
                representation = L6a.getOutputArray("sensoryAssociatedCells")
                representation = np.array(representation).nonzero()[0]
                learnedRepresentations[(objectDescription["name"],
                                        iFeature)] = representation

        # Infer objects
        self._setLearning(net, False)

        for objectDescription in OBJECTS:
            reset = True
            previousLocation = None
            inferred = False

            features = objectDescription["features"]
            touchSequence = list(range(len(features)))
            random.shuffle(touchSequence)

            for iFeature in touchSequence:
                feature = features[iFeature]

                # Move the sensor to the center of the object
                locationOnObject = np.array([
                    feature["top"] + feature["height"] / 2.,
                    feature["left"] + feature["width"] / 2.
                ])

                # Calculate displacement from previous location
                if previousLocation is not None:
                    motor.executeCommand('addDataToQueue',
                                         locationOnObject - previousLocation)
                else:
                    motor.executeCommand('addDataToQueue', [0, 0])
                previousLocation = locationOnObject

                # Sense feature at location
                sensor.executeCommand('addDataToQueue',
                                      FEATURE_ACTIVE_COLUMNS[feature["name"]],
                                      reset, 0)
                net.run(1)
                reset = False

                representation = L6a.getOutputArray("sensoryAssociatedCells")
                representation = np.array(representation).nonzero()[0]
                target_representations = set(
                    learnedRepresentations[(objectDescription["name"],
                                            iFeature)])

                inferred = (set(representation) <= target_representations)
                if inferred:
                    break

            self.assertTrue(inferred)
Exemplo n.º 17
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 range(5):
        for _ in range(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
Exemplo n.º 18
0
  def testRun(self):
    """
    A simple NetworkAPI example with three regions.
    ///////////////////////////////////////////////////////////////
    //
    //                          .------------------.
    //                         |    encoder        |          
    //                         |(RDSEEncoderRegion)|<------ inital input
    //                         |                   |          (INPUT.begin)
    //                         `-------------------'   
    //                                 | --------------. sp.bottomUpIn
    //                                 |/              | 
    //                         .-----------------.     |
    //                         |     sp          |     |
    //                         |  (SPRegion)     |     |
    //                         |                 |     |
    //                         `-----------------'     |
    //                                 |               ^
    //                         .-----------------.     |
    //                         |      tm         |     |
    //                         |   (TMRegion)    |-----'  (tm.bottomUpOut)
    //                         |                 |
    //                         `-----------------'
    //
    //////////////////////////////////////////////////////////////////
    """
    
    """ Creating network instance. """
    config = """
     {network: [
         {addRegion: {name: "encoder", type: "RDSEEncoderRegion", params: {size: 1000, sparsity: 0.2, radius: 0.03, seed: 2019, noise: 0.01}, phase: [1]}},
         {addRegion: {name: "sp", type: "SPRegion", params: {columnCount: 1000, globalInhibition: true}, phase: [2]}},
         {addRegion: {name: "tm", type: "TMRegion", params: {cellsPerColumn: 8, orColumnOutputs: true}, phase: [2]}},
         {addLink:   {src: "INPUT.begin", dest: "encoder.values", dim: [1]}},
         {addLink:   {src: "encoder.encoded", dest: "sp.bottomUpIn", mode: "overwrite"}},
         {addLink:   {src: "sp.bottomUpOut", dest: "tm.bottomUpIn"}},
         {addLink:   {src: "tm.bottomUpOut", dest: "sp.bottomUpIn"}}
      ]}"""
    net = Network()
    net.configure(config)
    
    net.initialize()
    # for debugging: print(net.getExecutionMap())
    
    """ Force initial data. """
    net.setInputData("begin", np.array([10]))

    """ Execute encoder once, the loop (sp and tm) 4 times """
    net.run(1, [1])      # execute initial entry (phase 1) 
    net.run(4, [2])      # execute loop 4 times  (phase 2)
    
    # Here is how you access the buffers
    sp_input_buffer = np.array(net.getRegion('sp').getInputArray('bottomUpIn'))
    tn_output_buffer = np.array(net.getRegion('tm').getOutputArray('bottomUpOut'))
    self.assertEqual(sp_input_buffer.size, tn_output_buffer.size)
Exemplo n.º 19
0
from htm.bindings.engine_internal import Network
from htm.advanced.support.register_regions import registerAllAdvancedRegions

registerAllAdvancedRegions()
""" Creating network instance. """
config = """
        {network: [
            {addRegion: {name: "encoder", type: "RDSEEncoderRegion", params: {size: 1000, sparsity: 0.2, radius: 0.03, seed: 2019, noise: 0.01}}},
            {addRegion: {name: "sp", type: "SPRegion", params: {columnCount: 2048, globalInhibition: true}}},
            {addRegion: {name: "tm", type: "TMRegion", params: {cellsPerColumn: 8, orColumnOutputs: true}}},
            {addRegion: {name: "apicalTM", type: "py.ApicalTMPairRegion", params: {columnCount : 2048, basalInputWidth : 10, cellsPerColumn: 8, implementation: ApicalTiebreak}}},
            {addLink:   {src: "encoder.encoded", dest: "sp.bottomUpIn"}},
            {addLink:   {src: "sp.bottomUpOut", dest: "tm.bottomUpIn"}},
            {addLink:   {src: "sp.bottomUpOut", dest: "apicalTM.activeColumns"}}
        ]}"""
net = Network()
net.configure(config)

#  feed data to RDSE encoder via its "sensedValue" parameter.
net.getRegion('encoder').setParameterReal64('sensedValue', 100)
net.run(10)  # Execute iteration of the Network object

print(net.getRegion('tm'))
print(net.getRegion('apicalTM'))

print(net.getRegion('tm').getConnections(
    ""))  # can be called because of this draft PR

print(
    net.getRegion('apicalTM').getConnections("")
)  # returns always None, it is region implemented in python, but it has not override getConnections
Exemplo n.º 20
0
def main(parameters=default_parameters, argv=None, verbose=True):
    if verbose:
        import pprint
        print("Parameters:")
        pprint.pprint(parameters, indent=4)
        print("")

    # Read the input file.
    records = []
    with open(_INPUT_FILE_PATH, "r") as fin:
        reader = csv.reader(fin)
        headers = next(reader)
        next(reader)
        next(reader)
        for record in reader:
            records.append(record)

    net = Network()
    # Make the Encoders.  These will convert input data into binary representations.
    dateEncoderRegion = net.addRegion(
        'dateEncoder', 'DateEncoderRegion',
        str(
            dict(timeOfDay_width=parameters["enc"]["time"]["timeOfDay"][0],
                 timeOfDay_radius=parameters["enc"]["time"]["timeOfDay"][1],
                 weekend_width=parameters["enc"]["time"]["weekend"])))

    valueEncoderRegion = net.addRegion(
        'valueEncoder', 'RDSEEncoderRegion',
        str(
            dict(size=parameters["enc"]["value"]["size"],
                 sparsity=parameters["enc"]["value"]["sparsity"],
                 resolution=parameters["enc"]["value"]["resolution"])))

    # Make the HTM.  SpatialPooler & TemporalMemory & associated tools.
    spParams = parameters["sp"]
    spRegion = net.addRegion(
        'sp',
        'SPRegion',
        str(
            dict(
                columnCount=spParams['columnCount'],
                potentialPct=spParams["potentialPct"],
                potentialRadius=0,  # 0 is auto assign as inputWith
                globalInhibition=True,
                localAreaDensity=spParams["localAreaDensity"],
                synPermInactiveDec=spParams["synPermInactiveDec"],
                synPermActiveInc=spParams["synPermActiveInc"],
                synPermConnected=spParams["synPermConnected"],
                boostStrength=spParams["boostStrength"],
                wrapAround=True)))

    tmParams = parameters["tm"]
    tmRegion = net.addRegion(
        'tm', 'TMRegion',
        str(
            dict(columnCount=spParams['columnCount'],
                 cellsPerColumn=tmParams["cellsPerColumn"],
                 activationThreshold=tmParams["activationThreshold"],
                 initialPermanence=tmParams["initialPerm"],
                 connectedPermanence=spParams["synPermConnected"],
                 minThreshold=tmParams["minThreshold"],
                 maxNewSynapseCount=tmParams["newSynapseCount"],
                 permanenceIncrement=tmParams["permanenceInc"],
                 permanenceDecrement=tmParams["permanenceDec"],
                 predictedSegmentDecrement=0.0,
                 maxSegmentsPerCell=tmParams["maxSegmentsPerCell"],
                 maxSynapsesPerSegment=tmParams["maxSynapsesPerSegment"])))

    net.link('dateEncoder', 'sp', '', '', 'encoded', 'bottomUpIn')
    net.link('valueEncoder', 'sp', '', '', 'encoded', 'bottomUpIn')
    net.link('sp', 'tm', '', '', 'bottomUpOut', 'bottomUpIn')

    net.initialize()

    # Iterate through every datum in the dataset, record the inputs & outputs.
    inputs = []
    anomaly = []

    for count, record in enumerate(records):

        # Convert date string into Python date object.
        dateString = datetime.datetime.strptime(record[0], "%m/%d/%y %H:%M")
        # Convert data value string into float.
        consumption = float(record[1])
        inputs.append(consumption)

        # Call the encoders to create bit representations for each value.  These are SDR objects.
        dateEncoderRegion.setParameterInt64('sensedTime',
                                            int(dateString.timestamp()))
        valueEncoderRegion.setParameterReal64('sensedValue', consumption)

        net.run(1)
        anomaly.append(np.array(tmRegion.getOutputArray("anomaly"))[0])

    try:
        import matplotlib.pyplot as plt
    except:
        print("WARNING: failed to import matplotlib, plots cannot be shown.")
        return

    plt.title("Anomaly Score")
    plt.xlabel("Time")
    plt.ylabel("Power Consumption")
    inputs = np.array(inputs) / max(inputs)
    plt.plot(
        np.arange(len(inputs)),
        inputs,
        'red',
        np.arange(len(inputs)),
        anomaly,
        'blue',
    )
    plt.legend(labels=('Input', 'Anomaly Score'))
    plt.show()
    return
Exemplo n.º 21
0
    def testGetSpec(self):
        """
    A test of the Network.getSpecJSON( ) function.
    """
        expected = """{"spec": "RDSEEncoderRegion",
  "parameters": {
    "activeBits": {
      "type": "UInt32",
      "count": 1,
      "access": "Create",
      "defaultValue": "0"
    },
    "category": {
      "type": "Bool",
      "count": 1,
      "access": "Create",
      "defaultValue": "false"
    },
    "noise": {
      "description": "amount of noise to add to the output SDR. 0.01 is 1%",
      "type": "Real32",
      "count": 1,
      "access": "ReadWrite",
      "defaultValue": "0.0"
    },
    "radius": {
      "type": "Real32",
      "count": 1,
      "access": "Create",
      "defaultValue": "0.0"
    },
    "resolution": {
      "type": "Real32",
      "count": 1,
      "access": "Create",
      "defaultValue": "0.0"
    },
    "seed": {
      "type": "UInt32",
      "count": 1,
      "access": "Create",
      "defaultValue": "0"
    },
    "sensedValue": {
      "description": "The value to encode. Overriden by input 'values'.",
      "type": "Real64",
      "count": 1,
      "access": "ReadWrite",
      "defaultValue": "0.0"
    },
    "size": {
      "type": "UInt32",
      "count": 1,
      "access": "Create",
      "defaultValue": "0"
    },
    "sparsity": {
      "type": "Real32",
      "count": 1,
      "access": "Create",
      "defaultValue": "0.0"
    }
  },
  "inputs": {
    "values": {
      "description": "Values to encode. Overrides sensedValue.",
      "type": "Real64",
      "count": 1,
      "required": 0,
      "regionLevel": 1,
      "isDefaultInput": 1
    }
  },
  "outputs": {
    "bucket": {
      "description": "Quantized sample based on the radius. Becomes the title for this sample in Classifier.",
      "type": "Real64",
      "count": 1,
      "regionLevel": 0,
      "isDefaultOutput": 0
    },
    "encoded": {
      "description": "Encoded bits. Not a true Sparse Data Representation (SP does that).",
      "type": "SDR",
      "count": 0,
      "regionLevel": 1,
      "isDefaultOutput": 1
    }
  }
}"""
        net = Network()
        json_str = net.getSpecJSON("RDSEEncoderRegion")
        self.assertEqual(json_str, expected)