Example #1
0
    def train(self, trainingSet):
        """
      This function train the ROM this gate is linked to. This method is aimed to agnostically understand if a "time-dependent-like" ROM needs to be constructed.
      @ In, trainingSet, dict or list, data used to train the ROM; if a list is provided a temporal ROM is generated.
      @ Out, None
    """
        if type(trainingSet).__name__ not in 'dict':
            self.raiseAnError(IOError, "The training set is not a dictionary!")
        if len(trainingSet.keys()) == 0:
            self.raiseAnError(IOError, "The training set is empty!")

        if any(type(x).__name__ == 'list' for x in trainingSet.values()):
            # we need to build a "time-dependent" ROM
            self.isADynamicModel = True
            if self.pivotParameterId not in trainingSet.keys():
                self.raiseAnError(
                    IOError, "the pivot parameter " + self.pivotParameterId +
                    " is not present in the training set. A time-dependent-like ROM cannot be created!"
                )
            if type(trainingSet[self.pivotParameterId]).__name__ != 'list':
                self.raiseAnError(
                    IOError, "the pivot parameter " + self.pivotParameterId +
                    " is not a list. Are you sure it is part of the output space of the training set?"
                )
            self.historySteps = trainingSet.get(self.pivotParameterId)[-1]
            if len(self.historySteps) == 0:
                self.raiseAnError(IOError, "the training set is empty!")
            if self.canHandleDynamicData:
                # the ROM is able to manage the time dependency on its own
                self.supervisedContainer[0].train(trainingSet)
            else:
                # we need to construct a chain of ROMs
                # the check on the number of time steps (consistency) is performed inside the historySnapShoots method
                # get the time slices
                newTrainingSet = mathUtils.historySnapShoots(
                    trainingSet, len(self.historySteps))
                if type(newTrainingSet).__name__ != 'list':
                    self.raiseAnError(IOError, newTrainingSet)
                # copy the original ROM
                originalROM = copy.deepcopy(self.supervisedContainer[0])
                # start creating and training the time-dep ROMs
                self.supervisedContainer = [
                ]  # [copy.deepcopy(originalROM) for _ in range(len(self.historySteps))]
                # train
                for ts in range(len(self.historySteps)):
                    self.supervisedContainer.append(copy.deepcopy(originalROM))
                    self.supervisedContainer[-1].train(newTrainingSet[ts])
        else:
            #self._replaceVariablesNamesWithAliasSystem(self.trainingSet, 'inout', False)
            self.supervisedContainer[0].train(trainingSet)
        self.amITrained = True
Example #2
0
    def train(self, trainingSet, assembledObjects=None):
        """
      This function train the ROM this gate is linked to. This method is aimed to agnostically understand if a "time-dependent-like" ROM needs to be constructed.
      @ In, trainingSet, dict or list, data used to train the ROM; if a list is provided a temporal ROM is generated.
      @ In, assembledObjects, dict, optional, objects that the ROM Model has assembled via the Assembler
      @ Out, None
    """
        if type(trainingSet).__name__ not in 'dict':
            self.raiseAnError(IOError, "The training set is not a dictionary!")
        if not list(trainingSet.keys()):
            self.raiseAnError(IOError, "The training set is empty!")

        # provide assembled objects to supervised container
        if assembledObjects is None:
            assembledObjects = {}

        self.supervisedContainer[0].setAssembledObjects(assembledObjects)

        # if training using ROMCollection, special treatment
        if isinstance(self.supervisedContainer[0],
                      SupervisedLearning.Collection):
            self.supervisedContainer[0].train(trainingSet)
        else:
            # not a collection # TODO move time-dependent snapshots to collection!
            ## time-dependent or static ROM?
            if any(type(x).__name__ == 'list' for x in trainingSet.values()):
                # we need to build a "time-dependent" ROM
                self.isADynamicModel = True
                if self.pivotParameterId not in list(trainingSet.keys()):
                    self.raiseAnError(
                        IOError,
                        'The pivot parameter "{}" is not present in the training set.'
                        .format(self.pivotParameterId),
                        'A time-dependent-like ROM cannot be created!')
                if type(trainingSet[self.pivotParameterId]).__name__ != 'list':
                    self.raiseAnError(
                        IOError,
                        'The pivot parameter "{}" is not a list.'.format(
                            self.pivotParameterId),
                        " Are you sure it is part of the output space of the training set?"
                    )
                self.historySteps = trainingSet.get(self.pivotParameterId)[-1]
                if not len(self.historySteps):
                    self.raiseAnError(IOError, "the training set is empty!")
                # intrinsically time-dependent or does the Gate need to handle it?
                if self.canHandleDynamicData:
                    # the ROM is able to manage the time dependency on its own
                    self.supervisedContainer[0].train(trainingSet)
                else:
                    # TODO we can probably migrate this time-dependent handling to a type of ROMCollection!
                    # we need to construct a chain of ROMs
                    # the check on the number of time steps (consistency) is performed inside the historySnapShoots method
                    # get the time slices
                    newTrainingSet = mathUtils.historySnapShoots(
                        trainingSet, len(self.historySteps))
                    assert type(newTrainingSet).__name__ == 'list'
                    # copy the original ROM
                    originalROM = self.supervisedContainer[0]
                    # start creating and training the time-dep ROMs
                    self.supervisedContainer = [
                    ]  # [copy.deepcopy(originalROM) for _ in range(len(self.historySteps))]
                    # train
                    for ts in range(len(self.historySteps)):
                        self.supervisedContainer.append(
                            copy.deepcopy(originalROM))
                        self.supervisedContainer[-1].train(newTrainingSet[ts])
            # if a static ROM ...
            else:
                #self._replaceVariablesNamesWithAliasSystem(self.trainingSet, 'inout', False)
                self.supervisedContainer[0].train(trainingSet)
        # END if ROMCollection
        self.amITrained = True