Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

whether using "self.opt" or "self.run.opt" #541

Open
hnczwj2008 opened this issue Jul 2, 2020 · 1 comment
Open

whether using "self.opt" or "self.run.opt" #541

hnczwj2008 opened this issue Jul 2, 2020 · 1 comment

Comments

@hnczwj2008
Copy link

hnczwj2008 commented Jul 2, 2020

Hi,
I'm reading the course-v3 ***.ipynb , https://github.com/fastai/course-v3/blob/master/nbs/dl2/09_optimizers.ipynb , and I was wondering whether should I use "self.opt" or "self.run.opt" in the callbackhanders. such as: "class Recorder(Callback):" ,"class ParamScheduler(Callback):" and so on.
here is the code I am geting confused:

class Recorder(Callback):
    def begin_fit(self): self.lrs,self.losses = [],[]

    def after_batch(self):
        if not self.in_train: return #or using: if not self.run.in_train: return
        self.lrs.append(self.opt.hypers[-1]['lr'])  #or using:  self.lrs.append(self.run.opt.hypers[-1]['lr']) 
        self.losses.append(self.loss.detach().cpu()) #or using:  self.losses.append(self.run.loss.detach().cpu()) 

class ParamScheduler(Callback):
    _order=1
    def __init__(self, pname, sched_funcs):
        self.pname,self.sched_funcs = pname,listify(sched_funcs)

    def begin_batch(self): 
        if not self.in_train: return
        fs = self.sched_funcs
        if len(fs)==1: fs = fs*len(self.opt.param_groups) #or using:  if len(fs)==1: fs = fs*len(self.run.opt.param_groups)
        pos = self.n_epochs/self.epochs #or using: pos = self.run.n_epochs/self.run.epochs
        for f,h in zip(fs,self.opt.hypers): h[self.pname] = f(pos) #or using:   for f,h in zip(fs,self.run.opt.hypers): h[self.pname] = f(pos)

Because , I find the attributes: "opt , loss,n_epochs, epochs.... " are only defined in the Learner(), they are passed to every CallbackHander by the Callback() class through the code:

class Callback():
    _order=0 
    def set_runner(self, run): self.run=run 
@prats0599
Copy link

Both self.opt and self.run.opt refer to the same object(i.e. the optimizer in the Learner class which gets passed to Runner).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants