-
Notifications
You must be signed in to change notification settings - Fork 6
/
LRRecommender.py
33 lines (28 loc) · 979 Bytes
/
LRRecommender.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
'''
@Author: Yu Di
@Date: 2019-08-15 17:40:21
@LastEditors: Yudi
@LastEditTime: 2019-08-15 17:44:50
@Company: Cardinal Operation
@Email: [email protected]
@Description:
'''
import torch
class FeaturesLinear(torch.nn.Module):
def __init__(self, field_dims, output_dim=1):
super().__init__()
self.fc = torch.nn.Embedding(sum(field_dims), output_dim)
self.bias = torch.nn.Parameter(torch.zeros((output_dim,)))
self.offsets = np.array((0, *np.cumsum(field_dims)[:-1]), dtype=np.long)
def forward(self, x):
x = x + x.new_tensor(self.offsets).unsqueeze(0)
return torch.sum(self.fc(x), dim=1) + self.bias
class LR(torch.nn.Module):
def __init__(self, params):
super().__init__()
self.linear = FeaturesLinear(params['field_dims'])
def forward(self, x):
'''
@param x: Long tensor of size ``(batch_size, num_fields)``
'''
return torch.sigmoid(self.linear(x).squeeze(1))