-
Notifications
You must be signed in to change notification settings - Fork 28
/
loss.py
52 lines (46 loc) · 2.18 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
48
49
50
51
52
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
class _Loss(nn.Module):
def __init__(self, reduction='sum'):
'''
`reduction` (string, optional)
- Specifies the reduction to apply to the output: `none` | `mean` | `sum`.
`none`: no reduction will be applied,
`mean`: the sum of the output will be divided by the number of elements in the output,
`sum`: the output will be summed.
Note: size_average and reduce are in the process of being deprecated,
and in the meantime, specifying either of those two args will override reduction.
Default: `sum`
'''
super().__init__()
assert(reduction == 'mean' or reduction ==
'sum' or reduction == 'none')
self.reduction = reduction
class BPRLoss(_Loss):
def __init__(self, reduction='sum'):
'''
`reduction` (string, optional)
- Specifies the reduction to apply to the output: `none` | `mean` | `sum`. `none`: no reduction will be applied, `mean`: the sum of the output will be divided by the number of elements in the output, `sum`: the output will be summed. Note: size_average and reduce are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction. Default: `sum`
'''
# ensure reduction in (mean,sum, none)
super().__init__(reduction)
def forward(self, model_output, **kwargs):
'''
`model_output` (tensor) - column 0 must be the scores of positive bundles/items, column 1 must be the negative.
'''
pred, L2_loss = model_output
# BPR loss
loss = -torch.log(torch.sigmoid(pred[:, 0] - pred[:, 1]))
# reduction
if self.reduction == 'mean':
loss = torch.mean(loss)
elif self.reduction == 'sum':
loss = torch.sum(loss)
elif self.reduction == 'none':
pass
else:
raise ValueError("reduction must be 'none' | 'mean' | 'sum'")
loss += L2_loss / kwargs['batch_size'] if 'batch_size' in kwargs else 0
return loss