-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptim.py
More file actions
100 lines (80 loc) · 3.02 KB
/
Copy pathoptim.py
File metadata and controls
100 lines (80 loc) · 3.02 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import torch
import torch.nn as nn
import numpy as np
from torch.autograd import Variable
import torch.nn.functional as F
def set_seed(seed):
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
class A_softmax(nn.Module):
def __init__(self, gamma=0):
super(A_softmax, self).__init__()
self.gamma = gamma
self.it = 0
self.LambdaMin = 5.0
self.LambdaMax = 1500.0
self.lamb = 1500.0
def forward(self, input, target):
self.it += 1
cos_theta, phi_theta = input
target = target.view(-1, 1)
index = cos_theta.data * 0.0
index.scatter_(1, target.data.view(-1, 1), 1)
index = index.byte().type(torch.bool)
index = Variable(index)
self.lamb = max(self.LambdaMin, self.LambdaMax / (1 + 0.1 * self.it))
output = cos_theta * 1.0 # size=(B,Classnum)
output[index] -= cos_theta[index] * (1.0 + 0) / (1 + self.lamb)
output[index] += phi_theta[index] * (1.0 + 0) / (1 + self.lamb)
logpt = F.log_softmax(output, dim=1)
logpt = logpt.gather(1, target)
logpt = logpt.view(-1)
pt = Variable(logpt.data.exp())
loss = -1 * (1 - pt) ** self.gamma * logpt
loss = loss.mean()
return loss
class ScheduledOptim(object):
""" A simple wrapper class for learning rate scheduling """
def __init__(self, optimizer, n_warmup_steps):
self.optimizer = optimizer
self.d_model = 64
self.n_warmup_steps = n_warmup_steps
self.n_current_steps = 0
self.delta = 1
def step(self):
"Step by the inner optimizer"
self.optimizer.step()
def zero_grad(self):
"Zero out the gradients by the inner optimizer"
self.optimizer.zero_grad()
def increase_delta(self):
self.delta *= 2
def update_learning_rate(self):
"Learning rate scheduling per step"
self.n_current_steps += self.delta
new_lr = np.power(self.d_model, -0.5) * np.min([
np.power(self.n_current_steps, -0.5),
np.power(self.n_warmup_steps, -1.5) * self.n_current_steps])
for param_group in self.optimizer.param_groups:
param_group['lr'] = new_lr
return new_lr
def state_dict(self):
ret = {
'd_model': self.d_model,
'n_warmup_steps': self.n_warmup_steps,
'n_current_steps': self.n_current_steps,
'delta': self.delta,
}
ret['optimizer'] = self.optimizer.state_dict()
return ret
def load_state_dict(self, state_dict):
self.d_model = state_dict['d_model']
self.n_warmup_steps = state_dict['n_warmup_steps']
self.n_current_steps = state_dict['n_current_steps']
self.delta = state_dict['delta']
self.optimizer.load_state_dict(state_dict['optimizer'])