Example #1
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)
Example #2
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])
Example #3
0
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