class OneLevelModel(LearningModel): """ Structure: WordEncoder, ActionEncoder -> GeneralSP -> GeneralTM """ def __init__(self, wordEncoder, actionEncoder, trainingSet, modulesParams=None): """ @param wordEncoder @param actionEncoder @param dataSet: A module containing the trainingData, all of its categories and the inputIdx dict that maps each index in categories to an input name. """ super(OneLevelModel, self).__init__(wordEncoder, actionEncoder, trainingSet, modulesParams) self.initModules(trainingSet.categories, trainingSet.inputIdx) self.structure = { 'wordInput': 'wordEnc', 'wordEnc': 'generalSP', ### 'actionInput': 'actionEnc', 'actionEnc': 'generalSP', ### 'generalSP': 'generalTM', 'generalTM': None } self.modules = { 'generalTM': self.generalTM, 'generalSP': self.generalSP, 'wordEnc': self.wordEncoder, 'actionEnc': self.actionEncoder } self.layer = Layer(self.structure, self.modules, self.classifier) def initModules(self, categories, inputIdx): modulesNames = {'generalSP', 'generalTM'} nWords = len(categories[inputIdx['wordInput']]) nActions = len(categories[inputIdx['actionInput']]) inputDimensions = max( self.wordEncoder.getWidth(), self.actionEncoder.getWidth() ) columnDimensions = (4 * max((nWords + nActions), len(self.trainingData)), ) defaultGeneralSPParams = { 'inputDimensions': inputDimensions, 'columnDimensions': columnDimensions, 'seed': self.spSeed } defaultGeneralTMParams = { 'columnDimensions': columnDimensions, 'seed': self.tmSeed } if (self.modulesParams is not None) and\ (set(self.modulesParams) == modulesNames): self.modulesParams['generalSP'].update(defaultGeneralSPParams) self.modulesParams['generalTM'].update(defaultGeneralTMParams) self.generalSP = SpatialPooler(**self.modulesParams['generalSP']) self.generalTM = TemporalMemory(**self.modulesParams['generalTM']) else: self.generalSP = SpatialPooler(**defaultGeneralSPParams) self.generalTM = TemporalMemory(**defaultGeneralTMParams) self.classifier = CLAClassifierCond( steps=[1, 2, 3], alpha=0.1, actValueAlpha=0.3, verbosity=0 ) def inputSentence(self, sentence, verbosity=1, learn=False): inputData = [('wordInput', sentence)] return self.layer.processInput(inputData, verbosity, learn)
class ClassicModel(LearningModel): """ Structure: WordEncoder -> WordsSP -> SentencesTM ActionEncoder -> ActionsSP -> ActionsSeqTM SentencesTM + ActionsSeqTM -> generalTM """ def __init__(self, wordEncoder, actionEncoder, trainingSet, modulesParams=None): """ @param wordEncoder @param actionEncoder @param trainingSet: A module containing the trainingData, all of its categories and the inputIdx dict that maps each index in categories to an input name. """ super(ClassicModel, self).__init__(wordEncoder, actionEncoder, trainingSet, modulesParams) self.initModules(trainingSet.categories, trainingSet.inputIdx) self.structure = { 'wordInput': 'wordEnc', 'wordEnc': 'wordSP', 'wordSP': 'wordTM', 'wordTM': 'generalSP', ### 'actionInput': 'actionEnc', 'actionEnc': 'actionSP', 'actionSP': 'actionTM', 'actionTM': 'generalSP', ### 'generalSP': 'generalTM', 'generalTM': None } self.modules = { 'generalTM': self.generalTM, 'generalSP': self.generalSP, 'wordTM': self.wordTM, 'wordSP': self.wordSP, 'wordEnc': self.wordEncoder, 'actionTM': self.actionTM, 'actionSP': self.actionSP, 'actionEnc': self.actionEncoder } self.layer = Layer(self.structure, self.modules, self.classifier) def initModules(self, categories, inputIdx): modulesNames = {'wordSP', 'wordTM', 'actionSP', 'actionTM', 'generalSP', 'generalTM'} if (self.modulesParams is not None) and\ (set(self.modulesParams) == modulesNames): self.modulesParams['wordSP'].update(self.defaultWordSPParams) self.modulesParams['wordTM'].update(self.defaultWordTMParams) self.modulesParams['actionSP'].update(self.defaultActionSPParams) self.modulesParams['actionTM'].update(self.defaultActionTMParams) self.wordSP = SpatialPooler(**self.modulesParams['wordSP']) self.wordTM = TemporalMemory(**self.modulesParams['wordTM']) self.actionSP = SpatialPooler(**self.modulesParams['actionSP']) self.actionTM = TemporalMemory(**self.modulesParams['actionTM']) generalInputDimensions = max( self.wordTM.numberOfCells() + 1, self.actionTM.numberOfCells() + 1 ) generalColumnDimensions = (len(self.trainingData) * 3,) defaultGeneralSPParams = { 'inputDimensions': generalInputDimensions, 'columnDimensions': generalColumnDimensions, 'seed': self.spSeed } defaultGeneralTMParams = { 'columnDimensions': generalColumnDimensions, 'seed': self.tmSeed } self.modulesParams['generalSP'].update(defaultGeneralSPParams) self.modulesParams['generalTM'].update(defaultGeneralTMParams) self.generalSP = SpatialPooler(**self.modulesParams['generalSP']) self.generalTM = TemporalMemory(**self.modulesParams['generalTM']) print("Using external Parameters!") else: self.wordSP = SpatialPooler(**self.defaultWordSPParams) self.wordTM = TemporalMemory(**self.defaultWordTMParams) self.actionSP = SpatialPooler(**self.defaultActionSPParams) self.actionTM = TemporalMemory(**self.defaultActionTMParams) print("External parameters invalid or not found, using"\ " the default ones") generalInputDimensions = max( self.wordTM.numberOfCells() + 1, self.actionTM.numberOfCells() + 1 ) generalColumnDimensions = (len(self.trainingData) * 3,) defaultGeneralSPParams = { 'inputDimensions': generalInputDimensions, 'columnDimensions': generalColumnDimensions, 'seed': self.spSeed } defaultGeneralTMParams = { 'columnDimensions': generalColumnDimensions, 'seed': self.tmSeed } self.generalSP = SpatialPooler(**defaultGeneralSPParams) self.generalTM = TemporalMemory(**defaultGeneralTMParams) self.classifier = CLAClassifierCond( steps=[1, 2, 3], alpha=0.1, actValueAlpha=0.3, verbosity=0 ) def inputSentence(self, sentence, verbosity=1, learn=False): inputData = [('wordInput', sentence)] return self.layer.processInput(inputData, verbosity, learn)