

def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None, continueEpochs=10, validationProportion=0.25, trainingData=None, validationData=None, convergence_threshold=10):
    'Train the module on the dataset until it converges.\n\n        Return the module with the parameters that gave the minimal validation\n        error.\n\n        If no dataset is given, the dataset passed during Trainer\n        initialization is used. validationProportion is the ratio of the dataset\n        that is used for the validation dataset.\n        \n        If the training and validation data is already set, the splitPropotion is ignored\n\n        If maxEpochs is given, at most that many epochs\n        are trained. Each time validation error hits a minimum, try for\n        continueEpochs epochs to find a better one.'
    epochs = 0
    if (dataset is None):
        dataset = self.ds
    if (verbose is None):
        verbose = self.verbose
    if ((trainingData is None) or (validationData is None)):
        (trainingData, validationData) = dataset.splitWithProportion((1 - validationProportion))
    if (not ((len(trainingData) > 0) and len(validationData))):
        raise ValueError((('Provided dataset too small to be split into training ' + 'and validation sets with proportion ') + str(validationProportion)))
    self.ds = trainingData
    bestweights = self.module.params.copy()
    bestverr = self.testOnData(validationData)
    bestepoch = 0
    self.trainingErrors = []
    self.validationErrors = [bestverr]
    while True:
        trainingError = self.train()
        validationError = self.testOnData(validationData)
        if (isnan(trainingError) or isnan(validationError)):
            raise Exception('Training produced NaN results')
        self.trainingErrors.append(trainingError)
        self.validationErrors.append(validationError)
        if ((epochs == 0) or (self.validationErrors[(- 1)] < bestverr)):
            bestverr = self.validationErrors[(- 1)]
            bestweights = self.module.params.copy()
            bestepoch = epochs
        if ((maxEpochs != None) and (epochs >= maxEpochs)):
            self.module.params[:] = bestweights
            break
        epochs += 1
        if (len(self.validationErrors) >= (continueEpochs * 2)):
            old = self.validationErrors[((- continueEpochs) * 2):(- continueEpochs)]
            new = self.validationErrors[(- continueEpochs):]
            if (min(new) > max(old)):
                self.module.params[:] = bestweights
                break
            lastnew = round(new[(- 1)], convergence_threshold)
            if (sum(((round(y, convergence_threshold) - lastnew) for y in new)) == 0):
                self.module.params[:] = bestweights
                break
    self.ds = dataset
    if verbose:
        print(('train-errors:', fListToString(self.trainingErrors, 6)))
        print(('valid-errors:', fListToString(self.validationErrors, 6)))
    return (self.trainingErrors[:bestepoch], self.validationErrors[:(1 + bestepoch)])

dataset/ETHPy150Open pybrain/pybrain/pybrain/supervised/trainers/backprop.py BackpropTrainer.trainUntilConvergence/original