Classification
- Cross-Entropy
- NLL
- Hinge
- Huber
- Kullback-Leibler
Regression
- MAE (L1)
- MSE (L2)
Metric Learning
- Dice
- Contrastive
- N-pair
- Triplet (Margin-based Loss)
Measuring "Distance" between the answer we expected and the true answer.
Cross-entropy loss, or log loss, measures the performance of a classification model whose output is a probability value between 0 and 1.
Cross-entropy loss increases as the predicted probability diverges from the actual label. A perfect model would have a log loss of 0.
Formula: (K class, y is one-hot vector, log is natural log)
The last output layer of neural network should be
sigmoid
(Another definition of Cross-Entropy)
The last output layer of neural network should be
softmax
NLL Loss vs. Cross Entropy Loss
import torch
import torch.nn.functional as F
thetensor = torch.randn(3, 3)
target = torch.tensor([0, 2, 1])
# NLL Loss
sm = F.softmax(thetensor, dim=1)
log = torch.log(sm)
nllloss = F.nll_loss(log, target)
# CE Loss
celoss = F.cross_entropy(thetensor, target)
print(nllloss, 'should be equal to', celoss)
Dice Loss in Action
- Wiki - Triplet Loss
- C4W4L04 Triplet loss - YouTube
- Lossless Triplet loss - Towards Data Science
- Siamese Network & Triplet Loss - Towards Data Science
- Distance measurement:
$d(a, b) \sim$ $\operatorname{sum}(|A-B|)$ $(\operatorname{sum}((A-B)^2))^{1/2}$ $(\operatorname{sum}((A-B)^3))^{1/3}$ $\operatorname{Jaccard}(A, B)$ $\operatorname{Cosine}(A, B)$
Margin-based Loss
$A$ : document$B$ : positive sample document$C$ : negative sample document- prediction
$B$ or$C$
$$ L = \max{0, M - \cos(r_A, r_B) + \cos(r_A, r_C)} $$ Let similarity between
$A$ and positive sample has "$M$" larger than negetive sample
--