-
Notifications
You must be signed in to change notification settings - Fork 32
/
loss.py
61 lines (50 loc) · 2.09 KB
/
loss.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import torch
import torch.nn as nn
from torch.autograd.function import Function
import torch.nn.functional as F
from torch.autograd import Variable
class OCSoftmax(nn.Module):
def __init__(self, feat_dim=2, r_real=0.9, r_fake=0.5, alpha=20.0):
super(OCSoftmax, self).__init__()
self.feat_dim = feat_dim
self.r_real = r_real
self.r_fake = r_fake
self.alpha = alpha
self.center = nn.Parameter(torch.randn(1, self.feat_dim))
nn.init.kaiming_uniform_(self.center, 0.25)
self.softplus = nn.Softplus()
def forward(self, x, labels):
"""
Args:
x: feature matrix with shape (batch_size, feat_dim).
labels: ground truth labels with shape (batch_size).
"""
w = F.normalize(self.center, p=2, dim=1)
x = F.normalize(x, p=2, dim=1)
scores = x @ w.transpose(0,1)
output_scores = scores.clone()
scores[labels == 0] = self.r_real - scores[labels == 0]
scores[labels == 1] = scores[labels == 1] - self.r_fake
loss = self.softplus(self.alpha * scores).mean()
return loss, output_scores.squeeze(1)
class AMSoftmax(nn.Module):
def __init__(self, num_classes, enc_dim, s=20, m=0.9):
super(AMSoftmax, self).__init__()
self.enc_dim = enc_dim
self.num_classes = num_classes
self.s = s
self.m = m
self.centers = nn.Parameter(torch.randn(num_classes, enc_dim))
def forward(self, feat, label):
batch_size = feat.shape[0]
norms = torch.norm(feat, p=2, dim=-1, keepdim=True)
nfeat = torch.div(feat, norms)
norms_c = torch.norm(self.centers, p=2, dim=-1, keepdim=True)
ncenters = torch.div(self.centers, norms_c)
logits = torch.matmul(nfeat, torch.transpose(ncenters, 0, 1))
y_onehot = torch.FloatTensor(batch_size, self.num_classes)
y_onehot.zero_()
y_onehot = Variable(y_onehot).cuda()
y_onehot.scatter_(1, torch.unsqueeze(label, dim=-1), self.m)
margin_logits = self.s * (logits - y_onehot)
return logits, margin_logits