Exemplo n.º 1
0
    def testQueue(self):
        """
        Test that RawValues executes correctly.
        """
        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], 1)
        motor.executeCommand('addDataToQueue', [1, 1])

        net.run(1)

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

        net.run(1)

        output = list(np.array(motor.getOutputArray("dataOut")))
        self.assertEqual([1, 1], output)
        output = list(np.array(motor.getOutputArray("resetOut")))
        self.assertEqual([False], output)
Exemplo n.º 2
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.º 3
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.º 4
0
    def testExecuteCommandListWithNoReset(self):
        """
        Test that execute command executes the correct command with a list with reset 0.
        """
        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], 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.º 5
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.º 6
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.º 7
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.º 8
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)
    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.º 10
0
""" 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

print(net.getRegion(
    'tm').getAlgorithmInstance())  # cannot call this, not accessible
Exemplo n.º 11
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