Ejemplo n.º 1
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 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__)))
  # Add custom region package to network
  Network.registerRegionPackage("custom_region")

  # Create a custom region
  network.addRegion("identityRegion", "py.IdentityRegion", json.dumps(I_PARAMS))

  # Link the Identity region to the sensor input
  network.link("sensor", "identityRegion", "UniformLink", "",
               srcOutput="sourceOut", destInput="in")

  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 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
  # TODO: Needs TMRegion
  network.addRegion("temporalMemoryRegion", "py.TPRegion",
                    json.dumps(TP_PARAMS))

  network.link("spatialPoolerRegion", "temporalMemoryRegion", "UniformLink", "")
  network.link("temporalMemoryRegion", "spatialPoolerRegion", "UniformLink", "",
               srcOutput="topDownOut", destInput="topDownIn")

  # Register TPRegion since we aren't in nupic
  curDirectory = os.path.dirname(os.path.abspath(__file__))
  # directory containing the union pooler directory is 2 directories above this file
  unionTemporalPoolerDirectory = os.path.split((os.path.split(curDirectory))[0])[0]
  sys.path.append(unionTemporalPoolerDirectory)
  Network.registerRegionPackage("union_temporal_pooling")

  # Add the TPRegion on top of the TPRegion
  temporal = network.regions["temporalMemoryRegion"].getSelf()
  UP_PARAMS["inputWidth"] = temporal.getOutputElementCount("bottomUpOut")
  network.addRegion("unionTemporalPoolerRegion", "py.TemporalPoolerRegion", json.dumps(UP_PARAMS))

  network.link("temporalMemoryRegion", "unionTemporalPoolerRegion", "UniformLink", "",
               srcOutput="activeCells", destInput="activeCells")
  network.link("temporalMemoryRegion", "unionTemporalPoolerRegion", "UniformLink", "",
               srcOutput="predictedActiveCells", destInput="predictedActiveCells")

  network.initialize()

  spatial = network.regions["spatialPoolerRegion"].getSelf()
  # Make sure learning is enabled (this is the default)
  spatial.setParameter("learningMode", 1, True)
  # We want temporal anomalies so disable anomalyMode in the SP. This mode is
  # used for computing anomalies in a non-temporal model.
  spatial.setParameter("anomalyMode", 1, False)

  # Enable topDownMode to get the predicted columns output
  temporal.setParameter("topDownMode", 1, True)
  # Make sure learning is enabled (this is the default)
  temporal.setParameter("learningMode", 1, True)
  # Enable inference mode so we get predictions
  temporal.setParameter("inferenceMode", 1, True)
  temporal.setParameter("computePredictedActiveCellIndices", 1, True)

  union = network.regions["unionTemporalPoolerRegion"].getSelf()
  # Make sure learning is enabled (this is the default)
  union.setParameter("learningMode", 1, True)

  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 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
  # TODO: Needs TMRegion
  network.addRegion("temporalMemoryRegion", "py.TPRegion",
                    json.dumps(TP_PARAMS))

  network.link("spatialPoolerRegion", "temporalMemoryRegion", "UniformLink", "")
  network.link("temporalMemoryRegion", "spatialPoolerRegion", "UniformLink", "",
               srcOutput="topDownOut", destInput="topDownIn")

  # Register UPRegion since we aren't in nupic
  curDirectory = os.path.dirname(os.path.abspath(__file__))
  # directory containing the union pooler directory is 2 directories above this file
  unionPoolerDirectory = os.path.split((os.path.split(curDirectory))[0])[0]
  sys.path.append(unionPoolerDirectory)
  Network.registerRegionPackage("union_pooling")

  # Add the UPRegion on top of the TPRegion
  temporal = network.regions["temporalMemoryRegion"].getSelf()
  UP_PARAMS["inputWidth"] = temporal.getOutputElementCount("bottomUpOut")
  network.addRegion("unionPoolerRegion", "py.PoolingRegion", json.dumps(UP_PARAMS))

  network.link("temporalMemoryRegion", "unionPoolerRegion", "UniformLink", "",
               srcOutput="activeCells", destInput="activeCells")
  network.link("temporalMemoryRegion", "unionPoolerRegion", "UniformLink", "",
               srcOutput="predictedActiveCells", destInput="predictedActiveCells")

  network.initialize()

  spatial = network.regions["spatialPoolerRegion"].getSelf()
  # Make sure learning is enabled (this is the default)
  spatial.setParameter("learningMode", 1, True)
  # We want temporal anomalies so disable anomalyMode in the SP. This mode is
  # used for computing anomalies in a non-temporal model.
  spatial.setParameter("anomalyMode", 1, False)

  # Enable topDownMode to get the predicted columns output
  temporal.setParameter("topDownMode", 1, True)
  # Make sure learning is enabled (this is the default)
  temporal.setParameter("learningMode", 1, True)
  # Enable inference mode so we get predictions
  temporal.setParameter("inferenceMode", 1, True)
  temporal.setParameter("computePredictedActiveCellIndices", 1, True)

  union = network.regions["unionPoolerRegion"].getSelf()
  # Make sure learning is enabled (this is the default)
  union.setParameter("learningMode", 1, True)

  return network