Пример #1
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 = {}
Пример #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)
Пример #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])
    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)
Пример #5
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