Callback usage

I’m having a bit of trouble using callbacks/ implementing them so was hoping someone here could help. Basically trying to add L1loss, which looks like this:

class L1Loss(LearnerCallback):
    def __init__(self, learn, lr=1e-2):
        self.l1_lr = lr
    def on_backward_end(self, **kwargss):
        optimizer = self.learn.opt.opt
        for group in optimizer.param_groups():
            for param in group['params']:
                sign = / (torch.abs( + 1e-9)
       = - self.l1_lr * sign

However when I try to fit the function I get the error TypeError: on_train_begin() missing 1 required positional argument: 'self'.

My learning bit looks like follows:

learner = Learner(db, 

A minimal working example with toy data can be found here in this colab notebook.

Am I supposed to use the @dataclass decorator? Any thoughts would be highly appreciated. :slight_smile:

You need the line super().__init__(learn) otherwise your callback isn’t properly initialized.

I’m afraid that didn’t help either. Still getting the same error. Sorry. Changed it to super().__init__(learn) regardless.

You also need to instantiate your callback: L1Loss(), not L1Loss.

1 Like

Yep, that did the trick.

learner = Learner(db, model, loss_func=F.mse_loss, wd=0)
cb = L1Loss(learner), callbacks=[cb])

Can I confirm that cb’s were supposed to be added after initiating the learner as shown above. i.e. not how I was trying initially: learner = Learner(other_params, callbacks=[L1Loss()]).

1 Like