6. Loss function
6.1. L1
Computes the mean absolute error.
[1]:
import torch
import torch.nn as nn
criterion = nn.L1Loss()
outputs = torch.tensor([[0.9, 0.8, 0.7]], requires_grad=True)
labels = torch.tensor([[1.0, 0.9, 0.8]], dtype=torch.float)
loss = criterion(outputs, labels)
print('outputs: ', outputs)
print('labels: ', labels)
print('loss: ', loss)
outputs: tensor([[0.9000, 0.8000, 0.7000]], requires_grad=True)
labels: tensor([[1.0000, 0.9000, 0.8000]])
loss: tensor(0.1000, grad_fn=<L1LossBackward>)
6.2. Smooth L1
[2]:
import torch
import torch.nn as nn
criterion = nn.SmoothL1Loss()
outputs = torch.tensor([[0.9, 0.8, 0.7]], requires_grad=True)
labels = torch.tensor([[1.0, 0.9, 0.8]], dtype=torch.float)
loss = criterion(outputs, labels)
print('outputs: ', outputs)
print('labels: ', labels)
print('loss: ', loss)
outputs: tensor([[0.9000, 0.8000, 0.7000]], requires_grad=True)
labels: tensor([[1.0000, 0.9000, 0.8000]])
loss: tensor(0.0050, grad_fn=<SmoothL1LossBackward>)
6.3. Mean-Squared Error
[3]:
criterion = nn.MSELoss()
outputs = torch.tensor([[0.9, 0.8, 0.7]], requires_grad=True)
labels = torch.tensor([[1.0, 0.9, 0.8]], dtype=torch.float)
loss = criterion(outputs, labels)
print('outputs: ', outputs)
print('labels: ', labels)
print('loss: ', loss)
outputs: tensor([[0.9000, 0.8000, 0.7000]], requires_grad=True)
labels: tensor([[1.0000, 0.9000, 0.8000]])
loss: tensor(0.0100, grad_fn=<MseLossBackward>)
6.4. Cross-Entropy
[4]:
import math
criterion = nn.CrossEntropyLoss()
probs = [math.log(p) for p in [0.9, 0.8, 0.7]]
outputs = torch.tensor([probs], requires_grad=True)
labels = torch.tensor([0], dtype=torch.long)
loss = criterion(outputs, labels)
print('outputs: ', outputs)
print('labels: ', labels)
print('loss: ', loss)
outputs: tensor([[-0.1054, -0.2231, -0.3567]], requires_grad=True)
labels: tensor([0])
loss: tensor(0.9808, grad_fn=<NllLossBackward>)
6.5. Negative Log-Likelihood
[5]:
criterion = nn.NLLLoss()
probs = [math.log(p) for p in [0.9, 0.8, 0.7]]
outputs = torch.tensor([probs], requires_grad=True)
labels = torch.tensor([0], dtype=torch.long)
loss = criterion(outputs, labels)
print('outputs: ', outputs)
print('labels: ', labels)
print('loss: ', loss)
outputs: tensor([[-0.1054, -0.2231, -0.3567]], requires_grad=True)
labels: tensor([0])
loss: tensor(0.1054, grad_fn=<NllLossBackward>)
6.6. Poisson Negative Log-Likelihood
[6]:
criterion = nn.PoissonNLLLoss()
probs = [math.log(p) for p in [0.9, 0.8, 0.7]]
outputs = torch.tensor([probs], requires_grad=True)
labels = torch.tensor([0], dtype=torch.long)
loss = criterion(outputs, labels)
print('outputs: ', outputs)
print('labels: ', labels)
print('loss: ', loss)
outputs: tensor([[-0.1054, -0.2231, -0.3567]], requires_grad=True)
labels: tensor([0])
loss: tensor(0.8000, grad_fn=<MeanBackward0>)
6.7. Kullback-Leibler Divergence
[7]:
criterion = nn.KLDivLoss(reduction='batchmean')
probs = [math.log(p) for p in [0.9, 0.8, 0.7]]
outputs = torch.tensor([probs], requires_grad=True)
labels = torch.tensor([0], dtype=torch.float)
loss = criterion(outputs, labels)
print('outputs: ', outputs)
print('labels: ', labels)
print('loss: ', loss)
outputs: tensor([[-0.1054, -0.2231, -0.3567]], requires_grad=True)
labels: tensor([0.])
loss: tensor(0., grad_fn=<DivBackward0>)
6.8. Binary Cross-Entropy
[8]:
sigmoid = nn.Sigmoid()
criterion = nn.BCELoss()
probs = [math.log(p) for p in [0.9, 0.8, 0.7]]
outputs = torch.tensor([probs], requires_grad=True)
labels = torch.tensor([[0.9, 0.8, 0.7]], dtype=torch.float)
loss = criterion(sigmoid(outputs), labels)
print('outputs: ', outputs)
print('labels: ', labels)
print('loss: ', loss)
outputs: tensor([[-0.1054, -0.2231, -0.3567]], requires_grad=True)
labels: tensor([[0.9000, 0.8000, 0.7000]])
loss: tensor(0.7611, grad_fn=<BinaryCrossEntropyBackward>)
6.9. Binary Cross-Entropy with Logits
[9]:
criterion = nn.BCEWithLogitsLoss()
probs = [math.log(p) for p in [0.9, 0.8, 0.7]]
outputs = torch.tensor([probs], requires_grad=True)
labels = torch.tensor([[0.9, 0.8, 0.7]], dtype=torch.float)
loss = criterion(outputs, labels)
print('outputs: ', outputs)
print('labels: ', labels)
print('loss: ', loss)
outputs: tensor([[-0.1054, -0.2231, -0.3567]], requires_grad=True)
labels: tensor([[0.9000, 0.8000, 0.7000]])
loss: tensor(0.7611, grad_fn=<BinaryCrossEntropyWithLogitsBackward>)
6.10. Margin Ranking
[10]:
criterion = nn.MarginRankingLoss()
x1 = torch.tensor([[0.5, 0.5, 0.5]])
x2 = torch.tensor([[0.4, 0.6, 0.3]])
y = torch.tensor([[1]])
criterion(x1, x2, y)
[10]:
tensor(0.0333)
[11]:
criterion = nn.MarginRankingLoss()
x1 = torch.tensor([[0.5, 0.5, 0.5]])
x2 = torch.tensor([[0.4, 0.6, 0.3]])
y = torch.tensor([[-1]])
criterion(x1, x2, y)
[11]:
tensor(0.1000)
6.11. Soft Margin
[12]:
criterion = nn.SoftMarginLoss()
x1 = torch.tensor([[0.5, 0.5, 0.5]])
x2 = torch.tensor([[0.4, 0.6, 0.3]])
criterion(x1, x2)
[12]:
tensor(0.5912)