forked from LayneH/SAT-selective-cls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloss.py
47 lines (36 loc) · 1.9 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
import torch
from torch.nn import functional as F
def deep_gambler_loss(outputs, targets, reward):
outputs = F.softmax(outputs, dim=1)
outputs, reservation = outputs[:,:-1], outputs[:,-1]
# gain = torch.gather(outputs, dim=1, index=targets.unsqueeze(1)).squeeze()
gain = outputs[torch.arange(targets.shape[0]), targets]
doubling_rate = (gain.add(reservation.div(reward))).log()
return -doubling_rate.mean()
class SelfAdativeTraining():
def __init__(self, num_examples=50000, num_classes=10, mom=0.9):
self.prob_history = torch.zeros(num_examples, num_classes)
self.updated = torch.zeros(num_examples, dtype=torch.int)
self.mom = mom
self.num_classes = num_classes
def _update_prob(self, prob, index, y):
onehot = torch.zeros_like(prob)
onehot[torch.arange(y.shape[0]), y] = 1
prob_history = self.prob_history[index].clone().to(prob.device)
# if not inited, use onehot label to initialize runnning vector
cond = (self.updated[index] == 1).to(prob.device).unsqueeze(-1).expand_as(prob)
prob_mom = torch.where(cond, prob_history, onehot)
# momentum update
prob_mom = self.mom * prob_mom + (1 - self.mom) * prob
self.updated[index] = 1
self.prob_history[index] = prob_mom.to(self.prob_history.device)
return prob_mom
def __call__(self, logits, y, index):
prob = F.softmax(logits.detach()[:, :self.num_classes], dim=1)
prob = self._update_prob(prob, index, y)
soft_label = torch.zeros_like(logits)
soft_label[torch.arange(y.shape[0]), y] = prob[torch.arange(y.shape[0]), y]
soft_label[:, -1] = 1 - prob[torch.arange(y.shape[0]), y]
soft_label = F.normalize(soft_label, dim=1, p=1)
loss = torch.sum(-F.log_softmax(logits, dim=1) * soft_label, dim=1)
return torch.mean(loss)