-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarginLoss.py
34 lines (28 loc) · 1.15 KB
/
MarginLoss.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
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
from Loss import Loss
class MarginLoss(Loss):
def __init__(self, adv_temperature=None, margin=6.0):
super(MarginLoss, self).__init__()
self.margin = nn.Parameter(torch.Tensor([margin]))
self.margin.requires_grad = False
if adv_temperature != None:
self.adv_temperature = nn.Parameter(torch.Tensor([adv_temperature]))
self.adv_temperature.requires_grad = False
self.adv_flag = True
else:
self.adv_flag = False
def get_weights(self, n_score):
return F.softmax(-n_score * self.adv_temperature, dim=-1).detach()
def forward(self, p_score, n_score):
if self.adv_flag:
return (self.get_weights(n_score) * torch.max(p_score - n_score, -self.margin)).sum(
dim=-1).mean() + self.margin
else:
return (torch.max(p_score - n_score, -self.margin)).mean() + self.margin
def predict(self, p_score, n_score):
score = self.forward(p_score, n_score)
return score.cpu().data.numpy()