def _createLPFNetwork(addSP = True, addTP = False): """Create an 'old-style' network ala LPF and return it.""" # ========================================================================== # Create the encoder and data source stuff we need to configure the sensor sensorParams = dict(verbosity = _VERBOSITY) encoder = _createEncoder() trainFile = findDataset("extra/gym/gym.csv") dataSource = FileRecordStream(streamID=trainFile) dataSource.setAutoRewind(True) # Create all the stuff we need to configure the CLARegion g_claConfig['spEnable'] = addSP g_claConfig['tpEnable'] = addTP claParams = _getCLAParams(encoder = encoder, config= g_claConfig) claParams['spSeed'] = g_claConfig['spSeed'] claParams['tpSeed'] = g_claConfig['tpSeed'] # ========================================================================== # Now create the network itself n = Network() n.addRegion("sensor", "py.RecordSensor", json.dumps(sensorParams)) sensor = n.regions['sensor'].getSelf() sensor.encoder = encoder sensor.dataSource = dataSource n.addRegion("level1", "py.CLARegion", json.dumps(claParams)) n.link("sensor", "level1", "UniformLink", "") n.link("sensor", "level1", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") return n
def _createLPFNetwork(addSP=True, addTP=False): """Create an 'old-style' network ala LPF and return it.""" # ========================================================================== # Create the encoder and data source stuff we need to configure the sensor sensorParams = dict(verbosity=_VERBOSITY) encoder = _createEncoder() trainFile = findDataset("extra/gym/gym.csv") dataSource = FileRecordStream(streamID=trainFile) dataSource.setAutoRewind(True) # Create all the stuff we need to configure the CLARegion g_claConfig["spEnable"] = addSP g_claConfig["tpEnable"] = addTP claParams = _getCLAParams(encoder=encoder, config=g_claConfig) claParams["spSeed"] = g_claConfig["spSeed"] claParams["tpSeed"] = g_claConfig["tpSeed"] # ========================================================================== # Now create the network itself n = Network() n.addRegion("sensor", "py.RecordSensor", json.dumps(sensorParams)) sensor = n.regions["sensor"].getSelf() sensor.encoder = encoder sensor.dataSource = dataSource n.addRegion("level1", "py.CLARegion", json.dumps(claParams)) n.link("sensor", "level1", "UniformLink", "") n.link("sensor", "level1", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") return n
def train(self, training_file, num_records): """Create a network and training it on a CSV data source""" dataSource = FileRecordStream(streamID=training_file) dataSource.setAutoRewind(True) self._network = configureNetwork(dataSource, self.network_config) for i in xrange(num_records): # Equivalent to: network.run(num_records) self._network.run(1) self._network.save(self.trained_network_path)
def _createOPFNetwork(addSP=True, addTP=False): """Create a 'new-style' network ala OPF and return it. If addSP is true, an SPRegion will be added named 'level1SP'. If addTP is true, a TPRegion will be added named 'level1TP' """ # ========================================================================== # Create the encoder and data source stuff we need to configure the sensor sensorParams = dict(verbosity=_VERBOSITY) encoder = _createEncoder() trainFile = findDataset("extra/gym/gym.csv") dataSource = FileRecordStream(streamID=trainFile) dataSource.setAutoRewind(True) # ========================================================================== # Now create the network itself n = Network() n.addRegion("sensor", "py.RecordSensor", json.dumps(sensorParams)) sensor = n.regions["sensor"].getSelf() sensor.encoder = encoder sensor.dataSource = dataSource # ========================================================================== # Add the SP if requested if addSP: print "Adding SPRegion" g_spRegionConfig["inputWidth"] = encoder.getWidth() n.addRegion("level1SP", "py.SPRegion", json.dumps(g_spRegionConfig)) n.link("sensor", "level1SP", "UniformLink", "") n.link("sensor", "level1SP", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") n.link("level1SP", "sensor", "UniformLink", "", srcOutput="spatialTopDownOut", destInput="spatialTopDownIn") n.link("level1SP", "sensor", "UniformLink", "", srcOutput="temporalTopDownOut", destInput="temporalTopDownIn") # ========================================================================== if addTP and addSP: # Add the TP on top of SP if requested # The input width of the TP is set to the column count of the SP print "Adding TPRegion on top of SP" g_tpRegionConfig["inputWidth"] = g_spRegionConfig["columnCount"] n.addRegion("level1TP", "py.TPRegion", json.dumps(g_tpRegionConfig)) n.link("level1SP", "level1TP", "UniformLink", "") n.link("level1TP", "level1SP", "UniformLink", "", srcOutput="topDownOut", destInput="topDownIn") n.link("sensor", "level1TP", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") elif addTP: # Add a lone TPRegion if requested # The input width of the TP is set to the encoder width print "Adding TPRegion" g_tpRegionConfig["inputWidth"] = encoder.getWidth() n.addRegion("level1TP", "py.TPRegion", json.dumps(g_tpRegionConfig)) n.link("sensor", "level1TP", "UniformLink", "") n.link("sensor", "level1TP", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") return n
def createSensors(network, sensors): for sensor in sensors: dataSource = FileRecordStream(streamID=sensor["source"]) dataSource.setAutoRewind(True) encoder = MultiEncoder() encoder.addMultipleEncoders(fieldEncodings=sensor["encodings"]) s = createRegion(network, sensor) s = s.getSelf() s.dataSource = dataSource s.encoder = encoder return network
def _createOPFNetwork(addSP=True, addTP=False): """Create a 'new-style' network ala OPF and return it. If addSP is true, an SPRegion will be added named 'level1SP'. If addTP is true, a TPRegion will be added named 'level1TP' """ # ========================================================================== # Create the encoder and data source stuff we need to configure the sensor sensorParams = dict(verbosity=_VERBOSITY) encoder = _createEncoder() trainFile = resource_filename("nupic.datafiles", "extra/gym/gym.csv") dataSource = FileRecordStream(streamID=trainFile) dataSource.setAutoRewind(True) # ========================================================================== # Now create the network itself n = Network() n.addRegion("sensor", "py.RecordSensor", json.dumps(sensorParams)) sensor = n.regions['sensor'].getSelf() sensor.encoder = encoder sensor.dataSource = dataSource # ========================================================================== # Add the SP if requested if addSP: print "Adding SPRegion" g_spRegionConfig['inputWidth'] = encoder.getWidth() n.addRegion("level1SP", "py.SPRegion", json.dumps(g_spRegionConfig)) n.link("sensor", "level1SP", "UniformLink", "") n.link("sensor", "level1SP", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") n.link("level1SP", "sensor", "UniformLink", "", srcOutput="spatialTopDownOut", destInput="spatialTopDownIn") n.link("level1SP", "sensor", "UniformLink", "", srcOutput="temporalTopDownOut", destInput="temporalTopDownIn") # ========================================================================== if addTP and addSP: # Add the TP on top of SP if requested # The input width of the TP is set to the column count of the SP print "Adding TPRegion on top of SP" g_tpRegionConfig['inputWidth'] = g_spRegionConfig['columnCount'] n.addRegion("level1TP", "py.TPRegion", json.dumps(g_tpRegionConfig)) n.link("level1SP", "level1TP", "UniformLink", "") n.link("level1TP", "level1SP", "UniformLink", "", srcOutput="topDownOut", destInput="topDownIn") n.link("sensor", "level1TP", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") elif addTP: # Add a lone TPRegion if requested # The input width of the TP is set to the encoder width print "Adding TPRegion" g_tpRegionConfig['inputWidth'] = encoder.getWidth() n.addRegion("level1TP", "py.TPRegion", json.dumps(g_tpRegionConfig)) n.link("sensor", "level1TP", "UniformLink", "") n.link("sensor", "level1TP", "UniformLink", "", srcOutput="resetOut", destInput="resetIn") return n