I am training a Multiclass classifier model and want to use Bi tempered logistics loss function. Currently, it is not available in fastai library. Can anybody guide me on how I can integrate it in fastai using PyTorch? I am a beginner so I don’t have much idea. It would be great if you guys can help me out.
An easier way:
If you search for “Bi tempered logistic loss PyTorch", you will find that someone has already done the implementation and testing. Then you only need to bring their function into your notebook and pass it when creating the Learner.
I copy-pasted the PyTorch code and called it in learner. However i am having a doubt about what do I pass in activation when I call this in Learner.
learn=cnn_learner(dls,resnet50,
loss_func=bi_tempered_logistic_loss(activation=?,t1=0.2,t2=1,labels=dls.vocab))
The loss function receives activations (the output of the model) and labels. These are provided automatically by the fastai training loop.
The simplest usage is to define your own my_bi_tempered_logistic_loss(activations,labels) whose body passes these two parameters along with fixed parameters t1, t2, etc. to bi_tempered_logistic_loss, and returns its result. The would be called a “partial function”.
Then to cnn_learner, simply pass the name my_bi_tempered_logistic_loss as the loss function.
The situation is confusing because PyTorch loss functions are created for example by a call to nn.MSELoss(…). In your case, my_bi_tempered_logistic_loss is directly the loss function. Just remember, loss_func is itself a function that takes activations and labels, and returns losses.