Skip to content

Commit

Permalink
Merge pull request #102 from GilesStrong/0.8_deprec
Browse files Browse the repository at this point in the history
removing deprecations
  • Loading branch information
GilesStrong committed Mar 17, 2021
2 parents 00c5f9f + b3acdf4 commit 7f5dbba
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 2,483 deletions.
34 changes: 34 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,40 @@

## Removals

- Scheduled removal of depreciated methods and functions from old model and callback system:
- `OldAbsCallback`
- `OldCallback`
- `OldAbsCyclicCallback`
- `OldCycleLR`
- `OldCycleMom`
- `OldOneCycle`
- `OldBinaryLabelSmooth`
- `OldBinaryLabelSmooth`
- `SequentialReweight`
- `SequentialReweightClasses`
- `OldBootstrapResample`
- `OldParametrisedPrediction`
- `OldGradClip`
- `OldLsuvInit`
- `OldAbsModelCallback`
- `OldSWA`
- `OldLRFinder`
- `OldEnsemble`
- `OldAMS`
- `OldMultiAMS`
- `OldBinaryAccuracy`
- `OldRocAucScore`
- `OldEvalMetric`
- `OldRegPull`
- `OldRegAsProxyPull`
- `OldAbsModel`
- `OldModel`
- `fold_train_ensemble`
- `OldMetricLogger`
- `fold_lr_find`
- `old_plot_train_history`
- `_get_folds`

## Fixes

## Changes
Expand Down
26 changes: 0 additions & 26 deletions lumin/nn/callbacks/abs_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,6 @@


__all__ = []


class OldAbsCallback(ABC):
r'''
.. Attention:: This class is depreciated in favour of :class:`~lumin.nn.callbacks.abs_callback.AbsCallback`.
It is a copy of the old `AbsCallback` class used in lumin<=0.6.
It will be removed in V0.8
'''

# XXX remove in V0.8

def __init__(self): pass
def set_model(self, **kargs): pass
def set_plot_settings(self, **kargs): pass
def on_train_begin(self, **kargs): pass
def on_train_end(self, **kargs): pass
def on_epoch_begin(self, **kargs): pass
def on_epoch_end(self, **kargs): pass
def on_batch_begin(self, **kargs): pass
def on_batch_end(self, **kargs): pass
def on_eval_begin(self, **kargs): pass
def on_eval_end(self, **kargs): pass
def on_backwards_begin(self, **kargs): pass
def on_backwards_end(self, **kargs): pass
def on_pred_begin(self, **kargs): pass
def on_pred_end(self, **kargs): pass


class AbsCallback(ABC):
Expand Down
40 changes: 2 additions & 38 deletions lumin/nn/callbacks/callback.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,10 @@
from typing import Optional

from .abs_callback import AbsCallback, OldAbsCallback
from ..models.abs_model import AbsModel, OldAbsModel
from .abs_callback import AbsCallback
from ..models.abs_model import AbsModel
from ...plotting.plot_settings import PlotSettings

__all__ = ['Callback']


class OldCallback(OldAbsCallback):
r'''
.. Attention:: This class is depreciated in favour of :class:`~lumin.nn.callbacks.callback.Callback`.
It is a copy of the old `Callback` class used in lumin<=0.6.
It will be removed in V0.8
'''

# XXX remove in V0.8

def __init__(self, model:Optional[AbsModel]=None, plot_settings:PlotSettings=PlotSettings()):
if model is not None: self.set_model(model)
self.set_plot_settings(plot_settings)

def set_model(self, model:OldAbsModel) -> None:
r'''
Sets the callback's model in order to allow the callback to access and adjust model parameters
Arguments:
model: model to refer to during training
'''

self.model = model

def set_plot_settings(self, plot_settings:PlotSettings) -> None:
r'''
Sets the plot settings for any plots produced by the callback
Arguments:
plot_settings: PlotSettings class
'''

self.plot_settings = plot_settings


class Callback(AbsCallback):
r'''
Base callback class from which other callbacks should inherit.
Expand Down
203 changes: 2 additions & 201 deletions lumin/nn/callbacks/cyclic_callbacks.py
Original file line number Diff line number Diff line change
@@ -1,104 +1,15 @@
import numpy as np
from typing import Optional, Tuple, Union, List
from typing import Tuple, Union
from fastcore.all import store_attr

from .callback import Callback, OldCallback
from ..models.abs_model import OldAbsModel
from ...plotting.plot_settings import PlotSettings
from .callback import Callback

import seaborn as sns
import matplotlib.pyplot as plt

__all__ = ['AbsCyclicCallback', 'CycleLR', 'CycleMom', 'OneCycle']


class OldAbsCyclicCallback(OldCallback):
r'''
.. Attention:: This class is depreciated in favour of :class:`~lumin.nn.callbacks.cyclic_callbacks.AbsCyclicCallback`.
It is a copy of the old `AbsCyclicCallback` class used in lumin<=0.6.
It will be removed in V0.8
'''

# XXX remove in V0.8

def __init__(self, interp:str, param_range:Tuple[float,float], cycle_mult:int=1, decrease_param:bool=False, scale:int=1,
model:Optional[OldAbsModel]=None, nb:Optional[int]=None, plot_settings:PlotSettings=PlotSettings()):
super().__init__(model=model, plot_settings=plot_settings)
self.param_range,self.cycle_mult,self.decrease_param,self.scale = param_range,cycle_mult,decrease_param,scale
self.interp,self.cycle_iter,self.cycle_count,self.cycle_end,self.hist = interp.lower(),0,0,False,[]
if nb is not None: self.nb = self.scale*nb

def set_nb(self, nb:int) -> None:
r'''
Sets the callback's internal number of iterations per cycle equal to `nb*scale`
Arguments:
nb: number of minibatches per epoch
'''

self.nb = self.scale*nb

def _incr_cycle(self) -> None:
self.cycle_iter += 1
if self.cycle_iter == self.nb:
self.cycle_iter = 0
self.nb *= self.cycle_mult
self.cycle_count += 1
self.cycle_end = True

def plot(self) -> None:
r'''
Plots the history of the parameter evolution as a function of iterations
'''

with sns.axes_style(self.plot_settings.style), sns.color_palette(self.plot_settings.cat_palette):
plt.figure(figsize=(self.plot_settings.w_mid, self.plot_settings.h_mid))
plt.xlabel("Iterations", fontsize=self.plot_settings.lbl_sz, color=self.plot_settings.lbl_col)
plt.ylabel(self.param_name, fontsize=self.plot_settings.lbl_sz, color=self.plot_settings.lbl_col)
plt.plot(range(len(self.hist)), self.hist)
plt.xticks(fontsize=self.plot_settings.tk_sz, color=self.plot_settings.tk_col)
plt.yticks(fontsize=self.plot_settings.tk_sz, color=self.plot_settings.tk_col)
plt.show()

def _calc_param(self) -> float:
if self.interp == 'cosine':
x = np.cos(np.pi*(self.cycle_iter)/self.nb)+1
dx = (self.param_range[1]-self.param_range[0])*x/2
return self.param_range[1]-dx if not self.decrease_param else dx+self.param_range[0]
elif self.interp == 'linear':
x = np.abs((2*self.cycle_iter/self.nb)-1)
dx = (self.param_range[1]-self.param_range[0])*np.max([0, 1-x])
return self.param_range[1]-dx if self.decrease_param else dx+self.param_range[0]
else:
raise ValueError(f"Interpolation mode {self.interp} not implemented")

def on_epoch_begin(self, **kargs) -> None:
r'''
Ensures the `cycle_end` flag is false when the epoch starts
'''

self.cycle_end = False

def on_batch_end(self, **kargs) -> None:
r'''
Increments the callback's progress through the cycle
'''

self._incr_cycle()

def on_batch_begin(self, **kargs) -> float:
r'''
Computes the new value for the optimiser parameter and returns it
Returns:
new value for optimiser parameter
'''

param = self._calc_param()
self.hist.append(param)
return param


class AbsCyclicCallback(Callback):
r'''
Abstract class for callbacks affecting lr or mom
Expand Down Expand Up @@ -203,31 +114,6 @@ def on_batch_begin(self) -> None:
self._set_param(param)


class OldCycleLR(OldAbsCyclicCallback):
r'''
.. Attention:: This class is depreciated in favour of :class:`~lumin.nn.callbacks.cyclic_callbacks.CycleLR`.
It is a copy of the old `CycleLR` class used in lumin<=0.6.
It will be removed in V0.8
'''

# XXX remove in V0.8

def __init__(self, lr_range:Tuple[float,float], interp:str='cosine', cycle_mult:int=1, decrease_param:Union[str,bool]='auto', scale:int=1,
model:Optional[OldAbsModel]=None, nb:Optional[int]=None, plot_settings:PlotSettings=PlotSettings()):
if decrease_param == 'auto': decrease_param = True if interp == 'cosine' else False
super().__init__(interp=interp, param_range=lr_range, cycle_mult=cycle_mult,
decrease_param=decrease_param, scale=scale, model=model, nb=nb, plot_settings=plot_settings)
self.param_name = 'Learning Rate'

def on_batch_begin(self, **kargs) -> None:
r'''
Computes the new lr and assignes it to the optimiser
'''

lr = super().on_batch_begin(**kargs)
self.model.set_lr(lr)


class CycleLR(AbsCyclicCallback):
r'''
Callback to cycle learning rate during training according to either:
Expand Down Expand Up @@ -262,31 +148,6 @@ def __init__(self, lr_range:Tuple[float,float], interp:str='cosine', cycle_mult:
def _set_param(self, param:float) -> None: self.model.set_lr(param)


class OldCycleMom(OldAbsCyclicCallback):
r'''
.. Attention:: This class is depreciated in favour of :class:`~lumin.nn.callbacks.cyclic_callbacks.CycleMom`.
It is a copy of the old `CycleMom` class used in lumin<=0.6.
It will be removed in V0.8
'''

# XXX remove in V0.8

def __init__(self, mom_range:Tuple[float,float], interp:str='cosine', cycle_mult:int=1, decrease_param:Union[str,bool]='auto', scale:int=1,
model:Optional[OldAbsModel]=None, nb:Optional[int]=None, plot_settings:PlotSettings=PlotSettings()):
if decrease_param == 'auto': decrease_param = False if interp == 'cosine' else True
super().__init__(interp=interp, param_range=mom_range, cycle_mult=cycle_mult,
decrease_param=decrease_param, scale=scale, model=model, nb=nb, plot_settings=plot_settings)
self.param_name = 'Momentum'

def on_batch_begin(self, **kargs) -> None:
r'''
Computes the new momentum and assignes it to the optimiser
'''

mom = super().on_batch_begin(**kargs)
self.model.set_mom(mom)


class CycleMom(AbsCyclicCallback):
r'''
Callback to cycle momentum (beta 1) during training according to either:
Expand Down Expand Up @@ -319,66 +180,6 @@ def __init__(self, mom_range:Tuple[float,float], interp:str='cosine', cycle_mult
def _set_param(self, param:float) -> None: self.model.set_mom(param)


class OldOneCycle(OldAbsCyclicCallback):
r'''
.. Attention:: This class is depreciated in favour of :class:`~lumin.nn.callbacks.cyclic_callbacks.OneCycle`.
It is a copy of the old `OneCycle` class used in lumin<=0.6.
It will be removed in V0.8
'''

# XXX remove in V0.8

def __init__(self, lengths:Tuple[int,int], lr_range:List[float], mom_range:Tuple[float,float]=(0.85, 0.95), interp:str='cosine',
model:Optional[OldAbsModel]=None, nb:Optional[int]=None, plot_settings:PlotSettings=PlotSettings()):
super().__init__(interp=interp, param_range=None, cycle_mult=1, scale=lengths[0], model=model, nb=nb, plot_settings=plot_settings)
self.lengths,self.lr_range,self.mom_range,self.hist = lengths,lr_range,mom_range,{'lr': [], 'mom': []}
if len(self.lr_range) == 2: self.lr_range.append(0)

def on_batch_begin(self, **kargs) -> None:
r'''
Computes the new lr and momentum and assignes them to the optimiser
'''

self.decrease_param = self.cycle_count % 1 != 0
self.param_range = self.lr_range
lr = self._calc_param()
self.hist['lr'].append(lr)
self.model.set_lr(lr)

self.decrease_param = self.cycle_count % 1 == 0
self.param_range = self.mom_range
mom = self._calc_param()
self.hist['mom'].append(mom)
self.model.set_mom(mom)

def _incr_cycle(self) -> None:
self.cycle_iter += 1
if self.cycle_iter == self.nb:
self.cycle_iter = 0
self.nb = self.lengths[1]*self.nb/self.lengths[0]
self.cycle_count += 0.5
self.cycle_end = self.cycle_count % 1 == 0
self.lr_range[0] = self.lr_range[2]
if self.cycle_count == 1: self.model.stop_train = True

def plot(self):
r'''
Plots the history of the lr and momentum evolution as a function of iterations
'''

with sns.axes_style(self.plot_settings.style), sns.color_palette(self.plot_settings.cat_palette):
fig, axs = plt.subplots(2, 1, figsize=(self.plot_settings.w_mid, self.plot_settings.h_mid))
axs[1].set_xlabel("Iterations", fontsize=self.plot_settings.lbl_sz, color=self.plot_settings.lbl_col)
axs[0].set_ylabel("Learning Rate", fontsize=self.plot_settings.lbl_sz, color=self.plot_settings.lbl_col)
axs[1].set_ylabel("Momentum", fontsize=self.plot_settings.lbl_sz, color=self.plot_settings.lbl_col)
axs[0].plot(range(len(self.hist['lr'])), self.hist['lr'])
axs[1].plot(range(len(self.hist['mom'])), self.hist['mom'])
for ax in axs:
ax.tick_params(axis='x', labelsize=self.plot_settings.tk_sz, labelcolor=self.plot_settings.tk_col)
ax.tick_params(axis='y', labelsize=self.plot_settings.tk_sz, labelcolor=self.plot_settings.tk_col)
plt.show()


class OneCycle(AbsCyclicCallback):
r'''
Callback implementing Smith 1-cycle evolution for lr and momentum (beta_1) https://arxiv.org/abs/1803.09820
Expand Down

0 comments on commit 7f5dbba

Please sign in to comment.