Custom TrackerCallback __init__() argument error

I’m trying to follow along the documentation (https://docs.fast.ai/callbacks.tracker.html#EarlyStoppingCallback) with coding a basic early stop callback.

My callback looks as follows:

class losscheckcallback(TrackerCallback):
    def on_epoch_end():
        tlprev, vlprev = 1
        self.monitor = 'train_loss'
        tl = self.get_monitor_value()
        self.monitor = 'valid_loss'
        vl = self.get_monitor_value()
        tllock = False
        vllock = False
        if tlprev < tl:
            tllock = True
        if tllock == True and vlprev < vl:
            return {"stop_training":True}
        return

I suspect the issue is that I’m changing the monitor partway through, but I’m unsure of a better way to do it. (I’ve never worked with callbacks before)

The code that generates an error is:

learn = tabular_learner(data, layers=[200,100], metrics=mean_squared_error,callback_fns=[losscheckcallback])
learn.lr_find()
learn.recorder.plot()

and I get:

TypeError                                 Traceback (most recent call last)
<ipython-input-8-c7a9c29f9dd1> in <module>
----> 1 learn.lr_find()
      2 learn.recorder.plot()

~/.local/lib/python3.6/site-packages/fastai/train.py in lr_find(learn, start_lr, end_lr, num_it, stop_div, wd)
     30     cb = LRFinder(learn, start_lr, end_lr, num_it, stop_div)
     31     epochs = int(np.ceil(num_it/len(learn.data.train_dl)))
---> 32     learn.fit(epochs, start_lr, callbacks=[cb], wd=wd)
     33 
     34 def to_fp16(learn:Learner, loss_scale:float=None, max_noskip:int=1000, dynamic:bool=True, clip:float=None,

~/.local/lib/python3.6/site-packages/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks)
    196         if not getattr(self, 'opt', False): self.create_opt(lr, wd)
    197         else: self.opt.lr,self.opt.wd = lr,wd
--> 198         callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)
    199         if defaults.extra_callbacks is not None: callbacks += defaults.extra_callbacks
    200         fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)

~/.local/lib/python3.6/site-packages/fastai/basic_train.py in <listcomp>(.0)
    196         if not getattr(self, 'opt', False): self.create_opt(lr, wd)
    197         else: self.opt.lr,self.opt.wd = lr,wd
--> 198         callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)
    199         if defaults.extra_callbacks is not None: callbacks += defaults.extra_callbacks
    200         fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)

TypeError: __init__() takes 1 positional argument but 2 were given

Do I need to change an __init__() somewhere?

Solved it myself. I’ll post a writeup here in a day or two.

Did you need to feed self into your def?

on_epoch_end(self)
1 Like

Yep. That was essentially it. There were some other things that ended up with me rewriting most of the rest of my code, but in terms of that it was essentially having self.