From cf55efe5ef340e9acb3c60937dd2f03287b92637 Mon Sep 17 00:00:00 2001 From: Sandeep Bhat Date: Thu, 16 Jul 2015 18:32:37 -0700 Subject: [PATCH] TerminationCriterion improvements [chg] Added support to enable termination using channels whose value increases as learning progresses e.g AUROC This can be controlled by setting the higher_is_better flag to True in MonitorBased class constructor. By default this flag has been set to False to maintain backward compatibility. --- pylearn2/termination_criteria/__init__.py | 25 ++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/pylearn2/termination_criteria/__init__.py b/pylearn2/termination_criteria/__init__.py index 98b5df90aa..1e18c8b815 100644 --- a/pylearn2/termination_criteria/__init__.py +++ b/pylearn2/termination_criteria/__init__.py @@ -41,8 +41,8 @@ def continue_learning(self, model): class MonitorBased(TerminationCriterion): """ A termination criterion that pulls out the specified channel in - the model's monitor and checks to see if it has decreased by a - certain proportion of the lowest value in the last N epochs. + the model's monitor and checks to see if it has decreased/increased + by a certain proportion of the lowest value in the last N epochs. Parameters ---------- @@ -55,14 +55,23 @@ class MonitorBased(TerminationCriterion): Name of the channel to examine. If None and the monitor has only one channel, this channel will be used; otherwise, an error will be raised. + higher_is_better : bool, optional + Whether a higher value of channel_name indicates a better model. + When True, prop_decrease is treated like prop_increase. """ - - def __init__(self, prop_decrease=.01, N=5, channel_name=None): + def __init__(self, prop_decrease=.01, N=5, channel_name=None, + higher_is_better=False): self._channel_name = channel_name self.prop_decrease = prop_decrease self.N = N self.countdown = N - self.best_value = np.inf + self.higher_is_better = higher_is_better + if higher_is_better: + self.coeff = -1. + else: + self.coeff = 1. + + self.best_value = self.coeff * np.inf def continue_learning(self, model): """ @@ -94,12 +103,14 @@ def continue_learning(self, model): # called unless the channel value is lower than the best value times # the prop_decrease factor, in which case the countdown is reset to N # and the best value is updated - if v[-1] < (1. - self.prop_decrease) * self.best_value: + if self.coeff * v[-1] < (self.coeff * + (1. - (self.coeff * self.prop_decrease)) * + self.best_value): self.countdown = self.N else: self.countdown = self.countdown - 1 - if v[-1] < self.best_value: + if self.coeff * v[-1] < self.coeff * self.best_value: self.best_value = v[-1] # The optimization continues until the countdown has reached 0,