prototypical-networks icon indicating copy to clipboard operation
prototypical-networks copied to clipboard

About calculation of loss function

Open Littleor opened this issue 2 years ago • 1 comments

Hi, I have some questions about this loss function because there are some differences between the paper and the code. First of all, the paper: Screen Shot 2021-11-27 at 19 26 52 The loss is the sum of the average logsoftmax between the query point and other prototypes and the average distance between the query point and the corresponding prototypes.

But in the code:

       log_p_y = F.log_softmax(-dists, dim=1).view(n_class, n_query, -1)
       loss_val = -log_p_y.gather(2, target_inds).squeeze().view(-1).mean()

The loss in code is only the sum of LogSoftmax between the query point and the corresponding prototypes.

I'm confused. Is it my understanding of the code or my understanding of the paper?

Littleor avatar Nov 27 '21 11:11 Littleor

I had the same confusion when replicated the paper's results. You could basically run cross-entropy and results will be the same. In this formula, he uses log_softmax on distances, then take only the right ones with gather function and compute mean on that. So it is basically the same.

I did similarly in my own implementation: https://github.com/fabian57fabian/prototypical-networks-few-shot-learning

fabian57fabian avatar Sep 29 '23 12:09 fabian57fabian