view pylearn/sandbox/nips09stopper.py @ 1484:83d3c9ee6d65

* changed MNIST dataset to use config.get_filepath_in_roots mechanism
author gdesjardins
date Tue, 05 Jul 2011 11:01:51 -0400
parents fb9fb142098f
children
line wrap: on
line source

"""Early stopper"""

class NIPS09Stopper(object):
    """Contrary to pylearn.algorithms.stopper, this early stopper should be
    used by calling 'find_min'.

    This stoppers stops if...
        n_iter_max iterations have happened since the beginning,
        OR (training error is below train_error_min
            AND (n_iter_max * patience iterations have happened since last minimum
                OR validation error gets higher than its minimum * valid_aumentation_max))
    """

    def __init__(self, n_iter_max,
            train_error_min = float('inf'),
            patience = 0.1,
            valid_augmentation_max = 1.1,
            set_score_interval = 1):
        self.stop = False
        self.iter = 0

        self.n_iter_max = n_iter_max
        self.train_error_min = train_error_min
        self.patience = patience
        self.valid_augmentation_max = valid_augmentation_max
        self.set_score_interval = set_score_interval

        self.best_valid = float('inf')
        self.best_valid_iter = -1


    def find_min(self, step, check, save):
        best = None
        while not self.stop:
            self.iter += 1
            step()
            if (self.iter % self.set_score_interval == 0):
                self.train_score, self.valid_score = check()
                if save:
                    if (self.valid_score < self.best_valid):
                        self.best_valid = self.valid_score
                        self.best_valid_iter = self.iter
                        best = [save(), self.iter, self.valid_score, self.iter]
                    else:
                        best[3] = self.iter

            if self.iter >= self.n_iter_max:
                self.stop = True
            elif self.train_score < self.train_error_min:
                if (self.iter - self.best_valid_iter) >\
                        self.n_iter_max * self.patience:
                    self.stop = True
                elif self.valid_score > self.best_valid * self.valid_augmentation_max:
                    self.stop = True

        return best