def _deSerializeExtraData(self, extraDataDir): """ Protected method that is called during deserialization (after __setstate__) with an external directory path. We override it here to load the Network API instance. @param extraDataDir (string) Model's extra data directory path """ # Must call this before loading any regions in the research repo registerAllResearchRegions() self.network = Network(os.path.join(extraDataDir, "network.nta")) self._initializeRegionHelpers()
def createNetwork(networkConfig): """ Create and initialize the specified network instance. @param networkConfig: (dict) the configuration of this network. @return network: (Network) The actual network """ registerAllResearchRegions() network = Network() if networkConfig["networkType"] == "L4L2Column": return createL4L2Column(network, networkConfig, "_0") elif networkConfig["networkType"] == "MultipleL4L2Columns": return createMultipleL4L2Columns(network, networkConfig)
def __init__(self, name, numCorticalColumns=1, inputSize=1024, numInputBits=20, externalInputSize=1024, numExternalInputBits=20, L2Overrides=None, L4Overrides=None, seed=42, logCalls=False, objectNamesAreIndices=False, ): """ Creates the network. Parameters: ---------------------------- @param TMOverrides (dict) Parameters to override in the TM region """ # Handle logging - this has to be done first self.logCalls = logCalls registerAllResearchRegions() self.name = name self.numLearningPoints = 1 self.numColumns = numCorticalColumns self.inputSize = inputSize self.externalInputSize = externalInputSize self.numInputBits = numInputBits self.objectNamesAreIndices = objectNamesAreIndices self.numExternalInputBits = numExternalInputBits # seed self.seed = seed random.seed(seed) # Create default parameters and then update with overrides self.config = { "networkType": "CombinedSequenceColumn", "numCorticalColumns": numCorticalColumns, "externalInputSize": externalInputSize, "sensorInputSize": inputSize, "enableFeedback": False, "L2Params": self.getDefaultL2Params(inputSize, numInputBits), } self.config["L4Params"] = self._getDefaultCombinedL4Params( self.numInputBits, self.inputSize, self.numExternalInputBits, self.externalInputSize, self.config["L2Params"]["cellCount"]) if L2Overrides is not None: self.config["L2Params"].update(L2Overrides) if L4Overrides is not None: self.config["L4Params"].update(L4Overrides) pprint.pprint(self.config) # Recreate network including TM parameters self.network = createNetwork(self.config) self.sensorInputs = [] self.externalInputs = [] self.L2Regions = [] self.L4Regions = [] for i in xrange(self.numColumns): self.sensorInputs.append( self.network.regions["sensorInput_" + str(i)].getSelf() ) self.externalInputs.append( self.network.regions["externalInput_" + str(i)].getSelf() ) self.L2Regions.append( self.network.regions["L2Column_" + str(i)] ) self.L4Regions.append( self.network.regions["L4Column_" + str(i)] ) self.L2Columns = [region.getSelf() for region in self.L2Regions] self.L4Columns = [region.getSelf() for region in self.L4Regions] # will be populated during training self.objectL2Representations = {} self.objectL2RepresentationsMatrices = [ SparseMatrix(0, self.config["L2Params"]["cellCount"]) for _ in xrange(self.numColumns)] self.objectNameToIndex = {} self.statistics = []
def __init__(self, name, numCorticalColumns=1, inputSize=1024, numInputBits=20, externalInputSize=1024, numExternalInputBits=20, L2Overrides=None, L4RegionType="py.ExtendedTMRegion", L4Overrides=None, numLearningPoints=3, seed=42, logCalls=False, enableLateralSP=False, lateralSPOverrides=None, enableFeedForwardSP=False, feedForwardSPOverrides=None, objectNamesAreIndices=False ): """ Creates the network. Parameters: ---------------------------- @param name (str) Experiment name @param numCorticalColumns (int) Number of cortical columns in the network @param inputSize (int) Size of the sensory input @param numInputBits (int) Number of ON bits in the generated input patterns @param externalInputSize (int) Size of the lateral input to L4 regions @param numExternalInputBits (int) Number of ON bits in the external input patterns @param L2Overrides (dict) Parameters to override in the L2 region @param L4RegionType (string) The type of region to use for L4 @param L4Overrides (dict) Parameters to override in the L4 region @param numLearningPoints (int) Number of times each pair should be seen to be learnt @param logCalls (bool) 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. @param enableLateralSP (bool) If true, Spatial Pooler will be added between external input and L4 lateral input @param lateralSPOverrides Parameters to override in the lateral SP region @param enableFeedForwardSP (bool) If true, Spatial Pooler will be added between external input and L4 feed-forward input @param feedForwardSPOverrides Parameters to override in the feed-forward SP region @param objectNamesAreIndices (bool) If True, object names are used as indices in the getCurrentObjectOverlaps method. Object names must be positive integers. If False, object names can be strings, and indices will be assigned to each object name. """ # Handle logging - this has to be done first self.logCalls = logCalls registerAllResearchRegions() self.name = name self.numLearningPoints = numLearningPoints self.numColumns = numCorticalColumns self.inputSize = inputSize self.externalInputSize = externalInputSize self.numInputBits = numInputBits self.objectNamesAreIndices = objectNamesAreIndices # seed self.seed = seed random.seed(seed) # update parameters with overrides self.config = { "networkType": "MultipleL4L2Columns", "numCorticalColumns": numCorticalColumns, "externalInputSize": externalInputSize, "sensorInputSize": inputSize, "L4RegionType": L4RegionType, "L4Params": self.getDefaultL4Params(L4RegionType, inputSize, numExternalInputBits), "L2Params": self.getDefaultL2Params(inputSize, numInputBits), } if enableLateralSP: self.config["lateralSPParams"] = self.getDefaultLateralSPParams(inputSize) if lateralSPOverrides: self.config["lateralSPParams"].update(lateralSPOverrides) if enableFeedForwardSP: self.config["feedForwardSPParams"] = self.getDefaultFeedForwardSPParams(inputSize) if feedForwardSPOverrides: self.config["feedForwardSPParams"].update(feedForwardSPOverrides) if L2Overrides is not None: self.config["L2Params"].update(L2Overrides) if L4Overrides is not None: self.config["L4Params"].update(L4Overrides) # create network self.network = createNetwork(self.config) self.sensorInputs = [] self.externalInputs = [] self.L4Regions = [] self.L2Regions = [] for i in xrange(self.numColumns): self.sensorInputs.append( self.network.regions["sensorInput_" + str(i)].getSelf() ) self.externalInputs.append( self.network.regions["externalInput_" + str(i)].getSelf() ) self.L4Regions.append( self.network.regions["L4Column_" + str(i)] ) self.L2Regions.append( self.network.regions["L2Column_" + str(i)] ) self.L4Columns = [region.getSelf() for region in self.L4Regions] self.L2Columns = [region.getSelf() for region in self.L2Regions] # will be populated during training self.objectL2Representations = {} self.objectL2RepresentationsMatrices = [ SparseMatrix(0, self.config["L2Params"]["cellCount"]) for _ in xrange(self.numColumns)] self.objectNameToIndex = {} self.statistics = []
def setUpClass(cls): registerAllResearchRegions()
def __init__(self, name, numCorticalColumns=1, L2Overrides={}, L4Overrides={}, L5Overrides={}, L6Overrides={}, numLearningPoints=3, seed=42, logCalls=False): """ Creates the network. Parameters: ---------------------------- @param name (str) Experiment name @param numCorticalColumns (int) Number of cortical columns in the network @param L2Overrides (dict) Parameters to override in the L2 region @param L4Overrides (dict) Parameters to override in the L4 region @param L5Overrides (dict) Parameters to override in the L5 region @param L6Overrides (dict) Parameters to override in the L6 region @param numLearningPoints (int) Number of times each pair should be seen to be learnt @param logCalls (bool) 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. """ # Handle logging - this has to be done first self.logCalls = logCalls registerAllResearchRegions() self.name = name self.numLearningPoints = numLearningPoints self.numColumns = numCorticalColumns self.sensorInputSize = 2048 self.numInputBits = 40 # seed self.seed = seed random.seed(seed) # Get network parameters and update with overrides self.config = { "networkType": "L2456Columns", "numCorticalColumns": numCorticalColumns, "randomSeedBase": self.seed, } self.config.update(self.getDefaultParams()) self.config["L2Params"].update(L2Overrides) self.config["L4Params"].update(L4Overrides) self.config["L5Params"].update(L5Overrides) self.config["L6Params"].update(L6Overrides) # create network and retrieve regions self.network = createNetwork(self.config) self._retrieveRegions() # will be populated during training self.objectRepresentationsL2 = {} self.objectRepresentationsL5 = {} self.statistics = []
def setUpClass(cls): random.seed(42) registerAllResearchRegions()
values.append(bits) ax.imshow(np.array(values).T, aspect='auto', cmap=plt.cm.binary, interpolation='nearest') # format plt.title("L2 SDR for col {} ({} features, {} columns)".format( c, features, cols), loc='right') # save plotPath = os.path.join( path, "Full L2 SDR_{}_{}_{}.pdf".format(features, cols, c)) plt.savefig(plotPath) plt.close() if __name__ == "__main__": registerAllResearchRegions() suite = MultiColumnExperiment() suite.start() experiments = suite.options.experiments if experiments is None: experiments = suite.cfgparser.sections() for exp in experiments: plotSensationByColumn(suite, exp) plotDebugStatistics(suite, exp)
def __init__(self, numCorticalColumns=1, inputSize=2048, numInputBits=40, L2Overrides=None, L4Overrides=None, numLearningPasses=4, onlineLearning=False, seed=42): """ Creates the network and initialize the experiment. Parameters: ---------------------------- @param numCorticalColumns (int) Number of cortical columns in the network @param inputSize (int) Size of the sensory input @param numInputBits (int) Number of ON bits in the generated input patterns @param L2Overrides (dict) Parameters to override in the L2 region @param L4Overrides Parameters to override in the L4 region @param numLearningPasses (int) Number of times each pair should be seen to be learnt """ registerAllResearchRegions() self.numLearningPoints = numLearningPasses self.numColumns = numCorticalColumns self.inputSize = inputSize self.numInputBits = numInputBits self.onlineLearning = onlineLearning # Select the type of region to use for layer 4. self.L4RegionType = "py.ApicalTMSequenceRegion" # seed self.seed = seed random.seed(seed) # update parameters with overrides self.config = { #"networkType": "MultipleL4L2Columns", "numCorticalColumns": numCorticalColumns, "externalInputSize": 0, "sensorInputSize": inputSize, "L4RegionType": self.L4RegionType, "L4Params": self.getDefaultL4Params(inputSize), "L2Params": self.getDefaultL2Params(inputSize), } if L2Overrides is not None: self.config["L2Params"].update(L2Overrides) if L4Overrides is not None: self.config["L4Params"].update(L4Overrides) # create network self.network = self.myCreateNetwork(self.config) # We have to explicitly initialize if we are going to change the phases self.network.initialize() self.sensorInputs = [] self.L4Columns = [] self.L2Columns = [] for i in xrange(self.numColumns): self.sensorInputs.append( self.network.regions["sensorInput_" + str(i)].getSelf() ) self.L4Columns.append( self.network.regions["L4Column_" + str(i)].getSelf() ) self.L2Columns.append( self.network.regions["L2Column_" + str(i)].getSelf() ) # will be populated during training self.objectL2Representations = {} self.statistics = []
def __init__(self, name, numCorticalColumns=1, inputSize=1024, numInputBits=20, externalInputSize=1024, numExternalInputBits=20, L2Overrides=None, L4RegionType="py.ApicalTMPairRegion", networkType = "MultipleL4L2Columns", longDistanceConnections = 0, maxConnectionDistance = 1, columnPositions = None, L4Overrides=None, numLearningPoints=3, seed=42, logCalls=False, enableLateralSP=False, lateralSPOverrides=None, enableFeedForwardSP=False, feedForwardSPOverrides=None, objectNamesAreIndices=False, enableFeedback=True ): """ Creates the network. Parameters: ---------------------------- @param name (str) Experiment name @param numCorticalColumns (int) Number of cortical columns in the network @param inputSize (int) Size of the sensory input @param numInputBits (int) Number of ON bits in the generated input patterns @param externalInputSize (int) Size of the lateral input to L4 regions @param numExternalInputBits (int) Number of ON bits in the external input patterns @param L2Overrides (dict) Parameters to override in the L2 region @param L4RegionType (string) The type of region to use for L4 @param networkType (string) Which type of L2L4 network to create. If topology is being used, it should be specified here. Possible values for this parameter are "MultipleL4L2Columns", "MultipleL4L2ColumnsWithTopology" and "L4L2Column" @param longDistanceConnections (float) The probability that a column will randomly connect to a distant column. Should be in [0, 1). Only relevant when using multiple columns with topology. @param L4Overrides (dict) Parameters to override in the L4 region @param numLearningPoints (int) Number of times each pair should be seen to be learnt @param logCalls (bool) 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. @param enableLateralSP (bool) If true, Spatial Pooler will be added between external input and L4 lateral input @param lateralSPOverrides Parameters to override in the lateral SP region @param enableFeedForwardSP (bool) If true, Spatial Pooler will be added between external input and L4 feed-forward input @param feedForwardSPOverrides Parameters to override in the feed-forward SP region @param objectNamesAreIndices (bool) If True, object names are used as indices in the getCurrentObjectOverlaps method. Object names must be positive integers. If False, object names can be strings, and indices will be assigned to each object name. @param enableFeedback (bool) If True, enable feedback between L2 and L4 """ # Handle logging - this has to be done first self.logCalls = logCalls registerAllResearchRegions() self.name = name self.numLearningPoints = numLearningPoints self.numColumns = numCorticalColumns self.inputSize = inputSize self.externalInputSize = externalInputSize self.numInputBits = numInputBits self.objectNamesAreIndices = objectNamesAreIndices # seed self.seed = seed random.seed(seed) # update parameters with overrides self.config = { "networkType": networkType, "longDistanceConnections": longDistanceConnections, "enableFeedback": enableFeedback, "numCorticalColumns": numCorticalColumns, "externalInputSize": externalInputSize, "sensorInputSize": inputSize, "L4RegionType": L4RegionType, "L4Params": self.getDefaultL4Params(inputSize, numExternalInputBits), "L2Params": self.getDefaultL2Params(inputSize, numInputBits), } if enableLateralSP: self.config["lateralSPParams"] = self.getDefaultLateralSPParams(inputSize) if lateralSPOverrides: self.config["lateralSPParams"].update(lateralSPOverrides) if enableFeedForwardSP: self.config["feedForwardSPParams"] = self.getDefaultFeedForwardSPParams(inputSize) if feedForwardSPOverrides: self.config["feedForwardSPParams"].update(feedForwardSPOverrides) if "Topology" in self.config["networkType"]: self.config["maxConnectionDistance"] = maxConnectionDistance # Generate a grid for cortical columns. Will attempt to generate a full # square grid, and cut out positions starting from the bottom-right if the # number of cortical columns is not a perfect square. if columnPositions is None: columnPositions = [] side_length = int(np.ceil(np.sqrt(numCorticalColumns))) for i in range(side_length): for j in range(side_length): columnPositions.append((i, j)) self.config["columnPositions"] = columnPositions[:numCorticalColumns] self.config["longDistanceConnections"] = longDistanceConnections if L2Overrides is not None: self.config["L2Params"].update(L2Overrides) if L4Overrides is not None: self.config["L4Params"].update(L4Overrides) # create network self.network = createNetwork(self.config) self.sensorInputs = [] self.externalInputs = [] self.L4Regions = [] self.L2Regions = [] for i in xrange(self.numColumns): self.sensorInputs.append( self.network.regions["sensorInput_" + str(i)].getSelf() ) self.externalInputs.append( self.network.regions["externalInput_" + str(i)].getSelf() ) self.L4Regions.append( self.network.regions["L4Column_" + str(i)] ) self.L2Regions.append( self.network.regions["L2Column_" + str(i)] ) self.L4Columns = [region.getSelf() for region in self.L4Regions] self.L2Columns = [region.getSelf() for region in self.L2Regions] # will be populated during training self.objectL2Representations = {} self.objectL2RepresentationsMatrices = [ SparseMatrix(0, self.config["L2Params"]["cellCount"]) for _ in xrange(self.numColumns)] self.objectNameToIndex = {} self.resetStatistics()
def __init__( self, name, numCorticalColumns=1, inputSize=1024, numInputBits=20, externalInputSize=1024, numExternalInputBits=20, L2Overrides=None, L4Overrides=None, seed=42, logCalls=False, objectNamesAreIndices=False, ): """ Creates the network. Parameters: ---------------------------- @param TMOverrides (dict) Parameters to override in the TM region """ # Handle logging - this has to be done first self.logCalls = logCalls registerAllResearchRegions() self.name = name self.numLearningPoints = 1 self.numColumns = numCorticalColumns self.inputSize = inputSize self.externalInputSize = externalInputSize self.numInputBits = numInputBits self.objectNamesAreIndices = objectNamesAreIndices self.numExternalInputBits = numExternalInputBits # seed self.seed = seed random.seed(seed) # Create default parameters and then update with overrides self.config = { "networkType": "CombinedSequenceColumn", "numCorticalColumns": numCorticalColumns, "externalInputSize": externalInputSize, "sensorInputSize": inputSize, "enableFeedback": False, "L2Params": self.getDefaultL2Params(inputSize, numInputBits), } self.config["L4Params"] = self._getDefaultCombinedL4Params( self.numInputBits, self.inputSize, self.numExternalInputBits, self.externalInputSize, self.config["L2Params"]["cellCount"]) if L2Overrides is not None: self.config["L2Params"].update(L2Overrides) if L4Overrides is not None: self.config["L4Params"].update(L4Overrides) pprint.pprint(self.config) # Recreate network including TM parameters self.network = createNetwork(self.config) self.sensorInputs = [] self.externalInputs = [] self.L2Regions = [] self.L4Regions = [] for i in xrange(self.numColumns): self.sensorInputs.append(self.network.regions["sensorInput_" + str(i)].getSelf()) self.externalInputs.append(self.network.regions["externalInput_" + str(i)].getSelf()) self.L2Regions.append(self.network.regions["L2Column_" + str(i)]) self.L4Regions.append(self.network.regions["L4Column_" + str(i)]) self.L2Columns = [region.getSelf() for region in self.L2Regions] self.L4Columns = [region.getSelf() for region in self.L4Regions] # will be populated during training self.objectL2Representations = {} self.objectL2RepresentationsMatrices = [ SparseMatrix(0, self.config["L2Params"]["cellCount"]) for _ in xrange(self.numColumns) ] self.objectNameToIndex = {} self.statistics = []
def setUpClass(cls): registerAllResearchRegions() cls.tmpDir = tempfile.mkdtemp() cls.encoder = CoordinateEncoder(n=1029, w=21, verbosity=0)
def __init__(self, name, numCorticalColumns=1, L2Overrides={}, L4Overrides={}, L5Overrides={}, L6Overrides={}, numLearningPoints=3, seed=42, logCalls = False ): """ Creates the network. Parameters: ---------------------------- @param name (str) Experiment name @param numCorticalColumns (int) Number of cortical columns in the network @param L2Overrides (dict) Parameters to override in the L2 region @param L4Overrides (dict) Parameters to override in the L4 region @param L5Overrides (dict) Parameters to override in the L5 region @param L6Overrides (dict) Parameters to override in the L6 region @param numLearningPoints (int) Number of times each pair should be seen to be learnt @param logCalls (bool) 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. """ # Handle logging - this has to be done first self.logCalls = logCalls registerAllResearchRegions() self.name = name self.numLearningPoints = numLearningPoints self.numColumns = numCorticalColumns self.sensorInputSize = 2048 self.numInputBits = 40 # seed self.seed = seed random.seed(seed) # Get network parameters and update with overrides self.config = { "networkType": "L2456Columns", "numCorticalColumns": numCorticalColumns, "randomSeedBase": self.seed, } self.config.update(self.getDefaultParams()) self.config["L2Params"].update(L2Overrides) self.config["L4Params"].update(L4Overrides) self.config["L5Params"].update(L5Overrides) self.config["L6Params"].update(L6Overrides) # create network and retrieve regions self.network = createNetwork(self.config) self._retrieveRegions() # will be populated during training self.objectRepresentationsL2 = {} self.objectRepresentationsL5 = {} self.statistics = []
def __init__(self, numCorticalColumns=1, inputSize=2048, numInputBits=40, L2Overrides=None, L4Overrides=None, numLearningPasses=4, seed=42): """ Creates the network and initialize the experiment. Parameters: ---------------------------- @param numCorticalColumns (int) Number of cortical columns in the network @param inputSize (int) Size of the sensory input @param numInputBits (int) Number of ON bits in the generated input patterns @param L2Overrides (dict) Parameters to override in the L2 region @param L4Overrides Parameters to override in the L4 region @param numLearningPasses (int) Number of times each pair should be seen to be learnt """ registerAllResearchRegions() self.numLearningPoints = numLearningPasses self.numColumns = numCorticalColumns self.inputSize = inputSize self.numInputBits = numInputBits # seed self.seed = seed random.seed(seed) # update parameters with overrides self.config = { "networkType": "MultipleL4L2Columns", "numCorticalColumns": numCorticalColumns, "externalInputSize": 0, "sensorInputSize": inputSize, "L4RegionType": "py.ExtendedTMRegion", "L4Params": self.getDefaultL4Params(inputSize), "L2Params": self.getDefaultL2Params(inputSize), } if L2Overrides is not None: self.config["L2Params"].update(L2Overrides) if L4Overrides is not None: self.config["L4Params"].update(L4Overrides) # create network self.network = createNetwork(self.config) # We have to explicitly initialize if we are going to change the phases self.network.initialize() self.sensorInputs = [] self.L4Columns = [] self.L2Columns = [] for i in xrange(self.numColumns): self.sensorInputs.append(self.network.regions["sensorInput_" + str(i)].getSelf()) self.L4Columns.append(self.network.regions["L4Column_" + str(i)].getSelf()) self.L2Columns.append(self.network.regions["L2Column_" + str(i)].getSelf()) # will be populated during training self.objectL2Representations = {} self.statistics = []
def setUpClass(cls): registerAllResearchRegions() cls.tmpDir = tempfile.mkdtemp()
bits = np.zeros(cellCount) bits[sdr] = 1 values.append(bits) ax.imshow(np.array(values).T, aspect='auto', cmap=plt.cm.binary, interpolation='nearest') # format plt.title("L2 SDR for col {} ({} features, {} columns)". format(c, features, cols), loc='right') # save plotPath = os.path.join(path, "Full L2 SDR_{}_{}_{}.pdf". format(features, cols, c)) plt.savefig(plotPath) plt.close() if __name__ == "__main__": registerAllResearchRegions() suite = MultiColumnExperiment() suite.start() experiments = suite.options.experiments if experiments is None: experiments = suite.cfgparser.sections() for exp in experiments: plotSensationByColumn(suite, exp) plotDebugStatistics(suite, exp)
def __init__(self, name, numCorticalColumns=1, inputSize=1024, numInputBits=20, externalInputSize=1024, L2Overrides=None, L4Overrides=None, numLearningPoints=4, seed=42): """ Creates the network. Parameters: ---------------------------- @param name (str) Experiment name @param numCorticalColumns (int) Number of cortical columns in the network @param inputSize (int) Size of the sensory input @param numInputBits (int) Number of ON bits in the generated input patterns @param externalInputSize (int) Size of the lateral input to L4 regions @param L2Overrides (dict) Parameters to override in the L2 region @param L4Overrides Parameters to override in the L4 region @param numLearningPoints (int) Number of times each pair should be seen to be learnt """ registerAllResearchRegions() self.name = name self.numLearningPoints = numLearningPoints self.numColumns = numCorticalColumns self.inputSize = inputSize self.externalInputSize = externalInputSize self.numInputBits = numInputBits # seed self.seed = seed random.seed(seed) # update parameters with overrides self.config = { "networkType": "MultipleL4L2Columns", "numCorticalColumns": numCorticalColumns, "externalInputSize": externalInputSize, "sensorInputSize": inputSize, "L4Params": self.getDefaultL4Params(inputSize), "L2Params": self.getDefaultL2Params(inputSize), } if L2Overrides is not None: self.config["L2Params"].update(L2Overrides) if L4Overrides is not None: self.config["L4Params"].update(L4Overrides) # create network self.network = createNetwork(self.config) self.sensorInputs = [] self.externalInputs = [] self.L4Columns = [] self.L2Columns = [] for i in xrange(self.numColumns): self.sensorInputs.append( self.network.regions["sensorInput_" + str(i)].getSelf() ) self.externalInputs.append( self.network.regions["externalInput_" + str(i)].getSelf() ) self.L4Columns.append( self.network.regions["L4Column_" + str(i)].getSelf() ) self.L2Columns.append( self.network.regions["L2Column_" + str(i)].getSelf() ) # will be populated during training self.objectL2Representations = {} self.statistics = [] if not os.path.exists(self.PLOT_DIRECTORY): os.makedirs(self.PLOT_DIRECTORY)
def __init__( self, name, numCorticalColumns=1, inputSize=1024, numInputBits=20, externalInputSize=1024, numExternalInputBits=20, L2Overrides=None, L2RegionType="py.ColumnPoolerRegion", L4RegionType="py.ApicalTMPairRegion", networkType="MultipleL4L2Columns", implementation=None, longDistanceConnections=0, maxConnectionDistance=1, columnPositions=None, L4Overrides=None, numLearningPoints=3, seed=42, logCalls=False, enableLateralSP=False, lateralSPOverrides=None, enableFeedForwardSP=False, feedForwardSPOverrides=None, objectNamesAreIndices=False, enableFeedback=True, maxSegmentsPerCell=10, ): """ Creates the network. Parameters: ---------------------------- @param name (str) Experiment name @param numCorticalColumns (int) Number of cortical columns in the network @param inputSize (int) Size of the sensory input @param numInputBits (int) Number of ON bits in the generated input patterns @param externalInputSize (int) Size of the lateral input to L4 regions @param numExternalInputBits (int) Number of ON bits in the external input patterns @param L2Overrides (dict) Parameters to override in the L2 region @param L2RegionType (string) The type of region to use for L2 @param L4RegionType (string) The type of region to use for L4 @param networkType (string) Which type of L2L4 network to create. If topology is being used, it should be specified here. Possible values for this parameter are "MultipleL4L2Columns", "MultipleL4L2ColumnsWithTopology" and "L4L2Column" @param longDistanceConnections (float) The probability that a column will randomly connect to a distant column. Should be in [0, 1). Only relevant when using multiple columns with topology. @param L4Overrides (dict) Parameters to override in the L4 region @param numLearningPoints (int) Number of times each pair should be seen to be learnt @param logCalls (bool) 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. @param enableLateralSP (bool) If true, Spatial Pooler will be added between external input and L4 lateral input @param lateralSPOverrides Parameters to override in the lateral SP region @param enableFeedForwardSP (bool) If true, Spatial Pooler will be added between external input and L4 feed-forward input @param feedForwardSPOverrides Parameters to override in the feed-forward SP region @param objectNamesAreIndices (bool) If True, object names are used as indices in the getCurrentObjectOverlaps method. Object names must be positive integers. If False, object names can be strings, and indices will be assigned to each object name. @param enableFeedback (bool) If True, enable feedback between L2 and L4 """ # Handle logging - this has to be done first self.logCalls = logCalls registerAllResearchRegions() self.name = name self.numLearningPoints = numLearningPoints self.numColumns = numCorticalColumns self.inputSize = inputSize self.externalInputSize = externalInputSize self.numInputBits = numInputBits self.objectNamesAreIndices = objectNamesAreIndices # seed self.seed = seed random.seed(seed) # update parameters with overrides if implementation is None: self.config = { "networkType": networkType, "longDistanceConnections": longDistanceConnections, "enableFeedback": enableFeedback, "numCorticalColumns": numCorticalColumns, "externalInputSize": externalInputSize, "sensorInputSize": inputSize, "L2RegionType": L2RegionType, "L4RegionType": L4RegionType, "L4Params": self.getDefaultL4Params(inputSize, numExternalInputBits), "L2Params": self.getDefaultL2Params(inputSize, numInputBits), } else: if "Bayesian" in implementation: self.config = { "networkType": networkType, "longDistanceConnections": longDistanceConnections, "enableFeedback": enableFeedback, "numCorticalColumns": numCorticalColumns, "externalInputSize": externalInputSize, "sensorInputSize": inputSize, "L2RegionType": L2RegionType, "L4RegionType": L4RegionType, "L4Params": self.getBayesianL4Params(inputSize, numExternalInputBits), "L2Params": self.getBayesianL2Params(inputSize, numInputBits), } self.config["L4Params"][ "maxSegmentsPerCell"] = maxSegmentsPerCell self.config["L4Params"]["implementation"] = implementation self.config["L2Params"]["implementation"] = implementation if enableLateralSP: self.config["lateralSPParams"] = self.getDefaultLateralSPParams( inputSize) if lateralSPOverrides: self.config["lateralSPParams"].update(lateralSPOverrides) if enableFeedForwardSP: self.config[ "feedForwardSPParams"] = self.getDefaultFeedForwardSPParams( inputSize) if feedForwardSPOverrides: self.config["feedForwardSPParams"].update( feedForwardSPOverrides) if "Topology" in self.config["networkType"]: self.config["maxConnectionDistance"] = maxConnectionDistance # Generate a grid for cortical columns. Will attempt to generate a full # square grid, and cut out positions starting from the bottom-right if the # number of cortical columns is not a perfect square. if columnPositions is None: columnPositions = [] side_length = int(np.ceil(np.sqrt(numCorticalColumns))) for i in range(side_length): for j in range(side_length): columnPositions.append((i, j)) self.config[ "columnPositions"] = columnPositions[:numCorticalColumns] self.config["longDistanceConnections"] = longDistanceConnections if L2Overrides is not None: self.config["L2Params"].update(L2Overrides) if L4Overrides is not None: self.config["L4Params"].update(L4Overrides) # create network self.network = createNetwork(self.config) self.sensorInputs = [] self.externalInputs = [] self.L4Regions = [] self.L2Regions = [] for i in xrange(self.numColumns): self.sensorInputs.append(self.network.regions["sensorInput_" + str(i)].getSelf()) self.externalInputs.append(self.network.regions["externalInput_" + str(i)].getSelf()) self.L4Regions.append(self.network.regions["L4Column_" + str(i)]) self.L2Regions.append(self.network.regions["L2Column_" + str(i)]) self.L4Columns = [region.getSelf() for region in self.L4Regions] self.L2Columns = [region.getSelf() for region in self.L2Regions] # will be populated during training self.objectL2Representations = {} self.objectL2RepresentationsMatrices = [ SparseMatrix(0, self.config["L2Params"]["cellCount"]) for _ in xrange(self.numColumns) ] self.objectNameToIndex = {} self.statistics = []
def __init__(self, name, numCorticalColumns=1, inputSize=1024, numInputBits=20, externalInputSize=1024, L2Overrides=None, L4Overrides=None, numLearningPoints=3, seed=42, logCalls=False): """ Creates the network. Parameters: ---------------------------- @param name (str) Experiment name @param numCorticalColumns (int) Number of cortical columns in the network @param inputSize (int) Size of the sensory input @param numInputBits (int) Number of ON bits in the generated input patterns @param externalInputSize (int) Size of the lateral input to L4 regions @param L2Overrides (dict) Parameters to override in the L2 region @param L4Overrides Parameters to override in the L4 region @param numLearningPoints (int) Number of times each pair should be seen to be learnt @param logCalls (bool) 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. """ # Handle logging - this has to be done first self.callLog = [] self.logCalls = logCalls if self.logCalls: frame = inspect.currentframe() args, _, _, values = inspect.getargvalues(frame) values.pop('frame') values.pop('self') self.callLog.append([inspect.getframeinfo(frame)[2], values]) registerAllResearchRegions() self.name = name self.numLearningPoints = numLearningPoints self.numColumns = numCorticalColumns self.inputSize = inputSize self.externalInputSize = externalInputSize self.numInputBits = numInputBits # seed self.seed = seed random.seed(seed) # update parameters with overrides self.config = { "networkType": "MultipleL4L2Columns", "numCorticalColumns": numCorticalColumns, "externalInputSize": externalInputSize, "sensorInputSize": inputSize, "L4Params": self.getDefaultL4Params(inputSize), "L2Params": self.getDefaultL2Params(inputSize), } if L2Overrides is not None: self.config["L2Params"].update(L2Overrides) if L4Overrides is not None: self.config["L4Params"].update(L4Overrides) # create network self.network = createNetwork(self.config) self.sensorInputs = [] self.externalInputs = [] self.L4Columns = [] self.L2Columns = [] for i in xrange(self.numColumns): self.sensorInputs.append(self.network.regions["sensorInput_" + str(i)].getSelf()) self.externalInputs.append(self.network.regions["externalInput_" + str(i)].getSelf()) self.L4Columns.append(self.network.regions["L4Column_" + str(i)].getSelf()) self.L2Columns.append(self.network.regions["L2Column_" + str(i)].getSelf()) # will be populated during training self.objectL2Representations = {} self.statistics = [] if not os.path.exists(self.PLOT_DIRECTORY): os.makedirs(self.PLOT_DIRECTORY)
def __init__(self, name, numCorticalColumns=1, inputSize=1024, numInputBits=20, externalInputSize=1024, L2Overrides=None, L4Overrides=None, numLearningPoints=3, seed=42, logCalls = False, enableLateralSP=False, lateralSPOverrides=None, enableFeedForwardSP=False, feedForwardSPOverrides=None ): """ Creates the network. Parameters: ---------------------------- @param name (str) Experiment name @param numCorticalColumns (int) Number of cortical columns in the network @param inputSize (int) Size of the sensory input @param numInputBits (int) Number of ON bits in the generated input patterns @param externalInputSize (int) Size of the lateral input to L4 regions @param L2Overrides (dict) Parameters to override in the L2 region @param L4Overrides Parameters to override in the L4 region @param numLearningPoints (int) Number of times each pair should be seen to be learnt @param logCalls (bool) 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. @param enableLateralSP (bool) If true, Spatial Pooler will be added between external input and L4 lateral input @param lateralSPOverrides Parameters to override in the lateral SP region @param enableFeedForwardSP (bool) If true, Spatial Pooler will be added between external input and L4 feed-forward input @param feedForwardSPOverrides Parameters to override in the feed-forward SP region """ # Handle logging - this has to be done first self.callLog = [] self.logCalls = logCalls if self.logCalls: frame = inspect.currentframe() args, _, _, values = inspect.getargvalues(frame) values.pop('frame') values.pop('self') self.callLog.append([inspect.getframeinfo(frame)[2], values]) registerAllResearchRegions() self.name = name self.numLearningPoints = numLearningPoints self.numColumns = numCorticalColumns self.inputSize = inputSize self.externalInputSize = externalInputSize self.numInputBits = numInputBits # seed self.seed = seed random.seed(seed) # update parameters with overrides self.config = { "networkType": "MultipleL4L2Columns", "numCorticalColumns": numCorticalColumns, "externalInputSize": externalInputSize, "sensorInputSize": inputSize, "L4Params": self.getDefaultL4Params(inputSize), "L2Params": self.getDefaultL2Params(inputSize), } if enableLateralSP: self.config["lateralSPParams"] = self.getDefaultLateralSPParams(inputSize) if lateralSPOverrides: self.config["lateralSPParams"].update(lateralSPOverrides) if enableFeedForwardSP: self.config["feedForwardSPParams"] = self.getDefaultFeedForwardSPParams(inputSize) if feedForwardSPOverrides: self.config["feedForwardSPParams"].update(feedForwardSPOverrides) if L2Overrides is not None: self.config["L2Params"].update(L2Overrides) if L4Overrides is not None: self.config["L4Params"].update(L4Overrides) # create network self.network = createNetwork(self.config) self.sensorInputs = [] self.externalInputs = [] self.L4Columns = [] self.L2Columns = [] for i in xrange(self.numColumns): self.sensorInputs.append( self.network.regions["sensorInput_" + str(i)].getSelf() ) self.externalInputs.append( self.network.regions["externalInput_" + str(i)].getSelf() ) self.L4Columns.append( self.network.regions["L4Column_" + str(i)].getSelf() ) self.L2Columns.append( self.network.regions["L2Column_" + str(i)].getSelf() ) # will be populated during training self.objectL2Representations = {} self.statistics = [] if not os.path.exists(self.PLOT_DIRECTORY): os.makedirs(self.PLOT_DIRECTORY)
def __init__( self, name, numCorticalColumns=1, inputSize=1024, numInputBits=20, externalInputSize=1024, numExternalInputBits=20, L2Overrides=None, networkType="L4L2TMColumn", L4Overrides=None, seed=42, logCalls=False, objectNamesAreIndices=False, TMOverrides=None, ): """ Creates the network. Parameters: ---------------------------- @param TMOverrides (dict) Parameters to override in the TM region """ # Handle logging - this has to be done first self.logCalls = logCalls registerAllResearchRegions() self.name = name self.numLearningPoints = 1 self.numColumns = numCorticalColumns self.inputSize = inputSize self.externalInputSize = externalInputSize self.numInputBits = numInputBits self.objectNamesAreIndices = objectNamesAreIndices # seed self.seed = seed random.seed(seed) # update parameters with overrides self.config = { "networkType": networkType, "numCorticalColumns": numCorticalColumns, "externalInputSize": externalInputSize, "sensorInputSize": inputSize, "enableFeedback": False, "L4Params": self.getDefaultL4Params(inputSize, numExternalInputBits), "L2Params": self.getDefaultL2Params(inputSize, numInputBits), "TMParams": self.getDefaultTMParams(self.inputSize, self.numInputBits), } if L2Overrides is not None: self.config["L2Params"].update(L2Overrides) if L4Overrides is not None: self.config["L4Params"].update(L4Overrides) if TMOverrides is not None: self.config["TMParams"].update(TMOverrides) # Recreate network including TM parameters self.network = createNetwork(self.config) self.sensorInputs = [] self.externalInputs = [] self.L4Regions = [] self.L2Regions = [] self.TMRegions = [] for i in xrange(self.numColumns): self.sensorInputs.append(self.network.regions["sensorInput_" + str(i)].getSelf()) self.externalInputs.append(self.network.regions["externalInput_" + str(i)].getSelf()) self.L4Regions.append(self.network.regions["L4Column_" + str(i)]) self.L2Regions.append(self.network.regions["L2Column_" + str(i)]) self.TMRegions.append(self.network.regions["TMColumn_" + str(i)]) self.L4Columns = [region.getSelf() for region in self.L4Regions] self.L2Columns = [region.getSelf() for region in self.L2Regions] self.TMColumns = [region.getSelf() for region in self.TMRegions] # will be populated during training self.objectL2Representations = {} self.objectL2RepresentationsMatrices = [ SparseMatrix(0, self.config["L2Params"]["cellCount"]) for _ in xrange(self.numColumns) ] self.objectNameToIndex = {} self.statistics = [] # Create classifier to hold supposedly unique TM states self.classifier = KNNClassifier(distanceMethod="rawOverlap") self.numTMCells = (self.TMColumns[0].cellsPerColumn * self.TMColumns[0].columnCount)