-
Notifications
You must be signed in to change notification settings - Fork 3
/
optimizers.py
46 lines (39 loc) · 1.55 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
import tqdm
import torch
from torch import nn
from torch import optim
from models import KBCModel
from regularizers import Regularizer
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
class KBCOptimizer(object):
def __init__(
self, model: KBCModel, regularizer: Regularizer, optimizer: optim.Optimizer, batch_size: int = 256,
verbose: bool = True
):
self.model = model
self.regularizer = 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 = self.model.forward(input_batch)
truth = input_batch[:, 2]
l_fit = loss(predictions, truth)
l_reg = self.regularizer.forward(factors)
l = l_fit + l_reg
self.optimizer.zero_grad()
l.backward()
self.optimizer.step()
b_begin += self.batch_size
bar.update(input_batch.shape[0])
bar.set_postfix(loss=f'{l.item():.0f}')