-
Notifications
You must be signed in to change notification settings - Fork 3
/
optimizers.py
64 lines (53 loc) · 2.3 KB
/
optimizers.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
62
import tqdm
import torch
from torch import nn
from torch import optim
from models import TKBCModel
from regularizers import Regularizer
from datasets import TemporalDataset
class TKBCOptimizer(object):
def __init__(
self, model: TKBCModel,
emb_regularizer: Regularizer, temporal_regularizer: Regularizer,
optimizer: optim.Optimizer, batch_size: int = 256,
verbose: bool = True
):
self.model = model
self.emb_regularizer = emb_regularizer
self.temporal_regularizer = temporal_regularizer
self.optimizer = optimizer
self.batch_size = batch_size
self.verbose = verbose
def epoch(self, examples: torch.LongTensor):
actual_examples = examples[torch.randperm(examples.shape[0]), :]
loss = nn.CrossEntropyLoss(reduction='mean')
with tqdm.tqdm(total=examples.shape[0], unit='ex', disable=not self.verbose) as bar:
bar.set_description(f'train loss')
b_begin = 0
while b_begin < examples.shape[0]:
input_batch = actual_examples[
b_begin:b_begin + self.batch_size
].cuda()
predictions, factors, time = self.model.forward(input_batch)
truth = input_batch[:, 2]
l_fit = loss(predictions, truth)
l_reg = self.emb_regularizer.forward(factors)
l_time = torch.zeros_like(l_reg)
if time is not None:
l_time = self.temporal_regularizer.forward(time)
l = l_fit + l_reg + l_time
self.optimizer.zero_grad()
l.backward()
for param in self.model.parameters():
if param.grad is not None:
if torch.isnan(param.grad).any():
param.grad = torch.nan_to_num(param.grad)
self.optimizer.step()
b_begin += self.batch_size
bar.update(input_batch.shape[0])
print('loss={},reg={},cont={}'.format(l_fit.item(),l_reg.item(),l_time.item()))
bar.set_postfix(
loss=f'{l_fit.item():.0f}',
reg=f'{l_reg.item():.0f}',
cont=f'{l_time.item():.0f}'
)