-
Notifications
You must be signed in to change notification settings - Fork 0
/
NegativeSampling.py
38 lines (31 loc) · 1.37 KB
/
NegativeSampling.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
from Strategy import Strategy
class NegativeSampling(Strategy):
def __init__(self, model = None, loss = None, batch_size = 256, regul_rate = 0.0, l3_regul_rate = 0.0):
super(NegativeSampling, self).__init__()
self.model = model
self.loss = loss
self.batch_size = batch_size
self.regul_rate = regul_rate
self.l3_regul_rate = l3_regul_rate
def _get_positive_score(self, score):
positive_score = score[:self.batch_size]
positive_score = positive_score.view(-1, self.batch_size).permute(1, 0)
return positive_score
def _get_negative_score(self, score):
negative_score = score[self.batch_size:]
negative_score = negative_score.view(-1, self.batch_size).permute(1, 0)
return negative_score
def forward(self, data):
score = self.model(data)
p_score = self._get_positive_score(score)
n_score = self._get_negative_score(score)
loss_res = self.loss(p_score, n_score)
if self.regul_rate != 0:
loss_res += self.regul_rate * self.model.regularization(data)
if self.l3_regul_rate != 0:
loss_res += self.l3_regul_rate * self.model.l3_regularization()
return loss_res
def save_checkpoint(self, path):
self.model.save_checkpoint(path)
def startingBatch(self):
return self.model.startingBatch()