model = SomeModel()
loss = distance_loss()
scores = model(torch.rand(1,3,32,32))
optimizer = torch.optim.SGD(model.parameters(), lr = 0.001 )
optimizer.zero_grad()
loss.backward()
optimizer.step()

But this returns error: âdistance_lossâ object has no attribute âbackwardâ. It is my understanding that backward method is automatically implemented in nn.module. What goes wrong here? Also do I somehow need to append the learnable parameters of distance_loss() to model.parameters() for optimizer to know?

I think you have two things called loss here that are confusing.
The loss in your code is actually the loss module you defined. You need to call it with the input to get the loss value from your batch. Then you will be able to call .backward() on that.

Thank you!! Is there a way that I can integrate the gradient of the loss into the optimizer so that it will perform gradient descent on the learnable parameters of my cusom defined loss function together with the parameters from model?