Callback usage

(Sachin) #1

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):
        super().__init__(learn)
        self.l1_lr = lr
    
    def on_backward_end(self, **kwargss):
        breakpoint()
        optimizer = self.learn.opt.opt
        for group in optimizer.param_groups():
            for param in group['params']:
                sign = param.data / (torch.abs(param.data) + 1e-9)
                param.data = param.data - 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, 
                  model, 
                  loss_func=F.mse_loss, 
                  wd=0, 
                  callbacks=[L1Loss])
learner.fit(20)

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:

0 Likes

#2

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

0 Likes

(Sachin) #3

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

0 Likes

#4

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

1 Like

(Sachin) #5

Yep, that did the trick.

learner = Learner(db, model, loss_func=F.mse_loss, wd=0)
cb = L1Loss(learner)
learner.fit(100, 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()]).

0 Likes