-
Notifications
You must be signed in to change notification settings - Fork 14
/
losses.py
executable file
·34 lines (24 loc) · 999 Bytes
/
losses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import torch.nn as nn
import torch
class HuberLoss(nn.Module):
def __init__(self, delta=.01):
super(HuberLoss, self).__init__()
self.delta = delta
def __call__(self, input, target):
mask = torch.zeros_like(input)
mann = torch.abs(input - target)
eucl = .5 * (mann**2)
mask[...] = mann < self.delta
# loss = eucl * mask + self.delta * (mann - .5 * self.delta) * (1 - mask)
loss = eucl * mask / self.delta + (mann - .5 * self.delta) * (1 - mask)
return torch.sum(loss, dim=-1, keepdim=False).mean()
class L1Loss(nn.Module):
def __init__(self):
super(L1Loss, self).__init__()
def __call__(self, input, target):
return torch.sum(torch.abs(input - target), dim=-1, keepdim=False).mean()
class L2Loss(nn.Module):
def __init__(self):
super(L2Loss, self).__init__()
def __call__(self, input, target):
return torch.sum((input - target)**2, dim=-1, keepdim=False).mean()