DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Updated on

HuberLoss() in PyTorch

Buy Me a Coffee

*Memos:

HuberLoss() can get the 0D or more D tensor of the zero or more values(float) computed by Huber Loss from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • The 1st argument for initialization is reduction(Optional-Default:'mean'-Type:str). *'none', 'mean' or 'sum' can be selected.
  • The 2nd argument for initialization is delta(Optional-Default:1.0-Type:float). *It must be 0<delta.
  • The 1st argument is input(Required-Type:tensor of float).
  • The 2nd argument is target(Required-Type:tensor of float).
  • input and target should be the same size otherwise there is a warning.
  • The empty 1D or more D input and target tensors with reduction='mean' return nan.
  • The empty 1D or more D input and target tensors with reduction='sum' return 0.. Image description
import torch
from torch import nn

tensor1 = torch.tensor([ 0.4, -0.8, -0.6,  0.3,  0.0, -0.5])
tensor2 = torch.tensor([-0.2,  0.9,  0.4,  0.1,  0.8, -0.5])
                      # 0.5(x-y)^2 because |x-y| < delta
                      # 0.5*(0.4-(-0.2))^2 = 0.18
                      # ↓↓↓↓               ↓↓↓↓   ↓↓↓↓   ↓↓↓
                      # 0.18 + 1.2 + 0.5 + 0.02 + 0.32 + 0.0 = 2.22
                      #        ↑↑↑   ↑↑↑ 
                      # delta(|x-y|-0.5*delta) because |x-y| >= delta
                      # 1.0*((|-0.8-0.9|)-0.5*1.0) = 1.2
                      # 
                      # 2.22 / 6 = 0.37
huberloss = nn.HuberLoss()
huberloss(input=tensor1, target=tensor2)
# tensor(0.3700)

huberloss
# HuberLoss()

huberloss.reduction
# 'mean'

huberloss.delta
# 1.0

huberloss = nn.HuberLoss(reduction='mean', delta=1.0)
huberloss(input=tensor1, target=tensor2)
# tensor(0.3700)

huberloss = nn.HuberLoss(reduction='sum', delta=1.0)
huberloss(input=tensor1, target=tensor2)
# tensor(2.2200)

huberloss = nn.HuberLoss(reduction='none', delta=1.0)
huberloss(input=tensor1, target=tensor2)
# tensor([0.1800, 1.2000, 0.5000, 0.0200, 0.3200, 0.0000])

huberloss = nn.HuberLoss(delta=0.5)
huberloss(input=tensor1, target=tensor2)
# tensor(0.2617)

huberloss = nn.HuberLoss(delta=1.5)
huberloss(input=tensor1, target=tensor2)
# tensor(0.4075)

huberloss = nn.HuberLoss(delta=2.0)
huberloss(input=tensor1, target=tensor2)
# tensor(0.4108)

huberloss = nn.HuberLoss(delta=3.0)
huberloss(input=tensor1, target=tensor2)
# tensor(0.4108)

tensor1 = torch.tensor([[0.4, -0.8, -0.6], [0.3, 0.0, -0.5]])
tensor2 = torch.tensor([[-0.2, 0.9, 0.4], [0.1, 0.8, -0.5]])

huberloss = nn.HuberLoss()
huberloss(input=tensor1, target=tensor2)
# tensor(0.3700)

tensor1 = torch.tensor([[[0.4], [-0.8], [-0.6]], [[0.3], [0.0], [-0.5]]])
tensor2 = torch.tensor([[[-0.2], [0.9], [0.4]], [[0.1], [0.8], [-0.5]]])

huberloss = nn.HuberLoss()
huberloss(input=tensor1, target=tensor2)
# tensor(0.3700)

tensor1 = torch.tensor([])
tensor2 = torch.tensor([])

huberloss = nn.HuberLoss(reduction='mean')
huberloss(input=tensor1, target=tensor2)
# tensor(nan)

huberloss = nn.HuberLoss(reduction='sum')
huberloss(input=tensor1, target=tensor2)
# tensor(0.)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)