torch-rechub 0.0.3__py3-none-any.whl → 0.0.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- torch_rechub/__init__.py +14 -0
- torch_rechub/basic/activation.py +54 -54
- torch_rechub/basic/callback.py +33 -33
- torch_rechub/basic/features.py +87 -94
- torch_rechub/basic/initializers.py +92 -92
- torch_rechub/basic/layers.py +994 -720
- torch_rechub/basic/loss_func.py +223 -34
- torch_rechub/basic/metaoptimizer.py +76 -72
- torch_rechub/basic/metric.py +251 -250
- torch_rechub/models/generative/__init__.py +6 -0
- torch_rechub/models/generative/hllm.py +249 -0
- torch_rechub/models/generative/hstu.py +189 -0
- torch_rechub/models/matching/__init__.py +13 -11
- torch_rechub/models/matching/comirec.py +193 -188
- torch_rechub/models/matching/dssm.py +72 -66
- torch_rechub/models/matching/dssm_facebook.py +77 -79
- torch_rechub/models/matching/dssm_senet.py +28 -16
- torch_rechub/models/matching/gru4rec.py +85 -87
- torch_rechub/models/matching/mind.py +103 -101
- torch_rechub/models/matching/narm.py +82 -76
- torch_rechub/models/matching/sasrec.py +143 -140
- torch_rechub/models/matching/sine.py +148 -151
- torch_rechub/models/matching/stamp.py +81 -83
- torch_rechub/models/matching/youtube_dnn.py +75 -71
- torch_rechub/models/matching/youtube_sbc.py +98 -98
- torch_rechub/models/multi_task/__init__.py +7 -5
- torch_rechub/models/multi_task/aitm.py +83 -84
- torch_rechub/models/multi_task/esmm.py +56 -55
- torch_rechub/models/multi_task/mmoe.py +58 -58
- torch_rechub/models/multi_task/ple.py +116 -130
- torch_rechub/models/multi_task/shared_bottom.py +45 -45
- torch_rechub/models/ranking/__init__.py +14 -11
- torch_rechub/models/ranking/afm.py +65 -63
- torch_rechub/models/ranking/autoint.py +102 -0
- torch_rechub/models/ranking/bst.py +61 -63
- torch_rechub/models/ranking/dcn.py +38 -38
- torch_rechub/models/ranking/dcn_v2.py +59 -69
- torch_rechub/models/ranking/deepffm.py +131 -123
- torch_rechub/models/ranking/deepfm.py +43 -42
- torch_rechub/models/ranking/dien.py +191 -191
- torch_rechub/models/ranking/din.py +93 -91
- torch_rechub/models/ranking/edcn.py +101 -117
- torch_rechub/models/ranking/fibinet.py +42 -50
- torch_rechub/models/ranking/widedeep.py +41 -41
- torch_rechub/trainers/__init__.py +4 -3
- torch_rechub/trainers/ctr_trainer.py +288 -128
- torch_rechub/trainers/match_trainer.py +336 -170
- torch_rechub/trainers/matching.md +3 -0
- torch_rechub/trainers/mtl_trainer.py +356 -207
- torch_rechub/trainers/seq_trainer.py +427 -0
- torch_rechub/utils/data.py +492 -360
- torch_rechub/utils/hstu_utils.py +198 -0
- torch_rechub/utils/match.py +457 -274
- torch_rechub/utils/model_utils.py +233 -0
- torch_rechub/utils/mtl.py +136 -126
- torch_rechub/utils/onnx_export.py +220 -0
- torch_rechub/utils/visualization.py +271 -0
- torch_rechub-0.0.5.dist-info/METADATA +402 -0
- torch_rechub-0.0.5.dist-info/RECORD +64 -0
- {torch_rechub-0.0.3.dist-info → torch_rechub-0.0.5.dist-info}/WHEEL +1 -2
- {torch_rechub-0.0.3.dist-info → torch_rechub-0.0.5.dist-info/licenses}/LICENSE +21 -21
- torch_rechub-0.0.3.dist-info/METADATA +0 -177
- torch_rechub-0.0.3.dist-info/RECORD +0 -55
- torch_rechub-0.0.3.dist-info/top_level.txt +0 -1
|
@@ -1,91 +1,93 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Date: create on 23/04/2022, update on 30/04/2022
|
|
3
|
-
References:
|
|
4
|
-
paper: (KDD'2018) Deep Interest Network for Click-Through Rate Prediction
|
|
5
|
-
url: https://arxiv.org/abs/1706.06978
|
|
6
|
-
code: https://github.com/huawei-noah/benchmark/blob/main/FuxiCTR/fuxictr/pytorch/models/DIN.py
|
|
7
|
-
Authors: Mincai Lai, laimincai@shanghaitech.edu.cn
|
|
8
|
-
"""
|
|
9
|
-
|
|
10
|
-
import torch
|
|
11
|
-
import torch.nn as nn
|
|
12
|
-
|
|
13
|
-
from ...basic.layers import
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
class DIN(nn.Module):
|
|
17
|
-
"""Deep Interest Network
|
|
18
|
-
Args:
|
|
19
|
-
features (list): the list of `Feature Class`. training by MLP. It means the user profile features and context features in origin paper, exclude history and target features.
|
|
20
|
-
history_features (list): the list of `Feature Class`,training by ActivationUnit. It means the user behaviour sequence features, eg.item id sequence, shop id sequence.
|
|
21
|
-
target_features (list): the list of `Feature Class`, training by ActivationUnit. It means the target feature which will execute target-attention with history feature.
|
|
22
|
-
mlp_params (dict): the params of the last MLP module, keys include:`{"dims":list, "activation":str, "dropout":float, "output_layer":bool`}
|
|
23
|
-
attention_mlp_params (dict): the params of the ActivationUnit module, keys include:`{"dims":list, "activation":str, "dropout":float, "use_softmax":bool`}
|
|
24
|
-
"""
|
|
25
|
-
|
|
26
|
-
def __init__(self, features, history_features, target_features, mlp_params, attention_mlp_params):
|
|
27
|
-
super().__init__()
|
|
28
|
-
self.features = features
|
|
29
|
-
self.history_features = history_features
|
|
30
|
-
self.target_features = target_features
|
|
31
|
-
self.num_history_features = len(history_features)
|
|
32
|
-
self.all_dims = sum([fea.embed_dim for fea in features + history_features + target_features])
|
|
33
|
-
|
|
34
|
-
self.embedding = EmbeddingLayer(features + history_features + target_features)
|
|
35
|
-
self.attention_layers = nn.ModuleList(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
embed_x_features = self.embedding(x, self.features)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
att_weight =
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
1
|
+
"""
|
|
2
|
+
Date: create on 23/04/2022, update on 30/04/2022
|
|
3
|
+
References:
|
|
4
|
+
paper: (KDD'2018) Deep Interest Network for Click-Through Rate Prediction
|
|
5
|
+
url: https://arxiv.org/abs/1706.06978
|
|
6
|
+
code: https://github.com/huawei-noah/benchmark/blob/main/FuxiCTR/fuxictr/pytorch/models/DIN.py
|
|
7
|
+
Authors: Mincai Lai, laimincai@shanghaitech.edu.cn
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import torch
|
|
11
|
+
import torch.nn as nn
|
|
12
|
+
|
|
13
|
+
from ...basic.layers import MLP, EmbeddingLayer
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class DIN(nn.Module):
|
|
17
|
+
"""Deep Interest Network
|
|
18
|
+
Args:
|
|
19
|
+
features (list): the list of `Feature Class`. training by MLP. It means the user profile features and context features in origin paper, exclude history and target features.
|
|
20
|
+
history_features (list): the list of `Feature Class`,training by ActivationUnit. It means the user behaviour sequence features, eg.item id sequence, shop id sequence.
|
|
21
|
+
target_features (list): the list of `Feature Class`, training by ActivationUnit. It means the target feature which will execute target-attention with history feature.
|
|
22
|
+
mlp_params (dict): the params of the last MLP module, keys include:`{"dims":list, "activation":str, "dropout":float, "output_layer":bool`}
|
|
23
|
+
attention_mlp_params (dict): the params of the ActivationUnit module, keys include:`{"dims":list, "activation":str, "dropout":float, "use_softmax":bool`}
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(self, features, history_features, target_features, mlp_params, attention_mlp_params):
|
|
27
|
+
super().__init__()
|
|
28
|
+
self.features = features
|
|
29
|
+
self.history_features = history_features
|
|
30
|
+
self.target_features = target_features
|
|
31
|
+
self.num_history_features = len(history_features)
|
|
32
|
+
self.all_dims = sum([fea.embed_dim for fea in features + history_features + target_features])
|
|
33
|
+
|
|
34
|
+
self.embedding = EmbeddingLayer(features + history_features + target_features)
|
|
35
|
+
self.attention_layers = nn.ModuleList([ActivationUnit(fea.embed_dim, **attention_mlp_params) for fea in self.history_features])
|
|
36
|
+
self.mlp = MLP(self.all_dims, activation="dice", **mlp_params)
|
|
37
|
+
|
|
38
|
+
def forward(self, x):
|
|
39
|
+
# (batch_size, num_features, emb_dim)
|
|
40
|
+
embed_x_features = self.embedding(x, self.features)
|
|
41
|
+
# (batch_size, num_history_features, seq_length, emb_dim)
|
|
42
|
+
embed_x_history = self.embedding(x, self.history_features)
|
|
43
|
+
# (batch_size, num_target_features, emb_dim)
|
|
44
|
+
embed_x_target = self.embedding(x, self.target_features)
|
|
45
|
+
attention_pooling = []
|
|
46
|
+
for i in range(self.num_history_features):
|
|
47
|
+
attention_seq = self.attention_layers[i](embed_x_history[:, i, :, :], embed_x_target[:, i, :])
|
|
48
|
+
attention_pooling.append(attention_seq.unsqueeze(1)) # (batch_size, 1, emb_dim)
|
|
49
|
+
# (batch_size, num_history_features, emb_dim)
|
|
50
|
+
attention_pooling = torch.cat(attention_pooling, dim=1)
|
|
51
|
+
|
|
52
|
+
mlp_in = torch.cat([attention_pooling.flatten(start_dim=1), embed_x_target.flatten(start_dim=1), embed_x_features.flatten(start_dim=1)], dim=1) # (batch_size, N)
|
|
53
|
+
|
|
54
|
+
y = self.mlp(mlp_in)
|
|
55
|
+
return torch.sigmoid(y.squeeze(1))
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class ActivationUnit(nn.Module):
|
|
59
|
+
"""Activation Unit Layer mentioned in DIN paper, it is a Target Attention method.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
embed_dim (int): the length of embedding vector.
|
|
63
|
+
history (tensor):
|
|
64
|
+
Shape:
|
|
65
|
+
- Input: `(batch_size, seq_length, emb_dim)`
|
|
66
|
+
- Output: `(batch_size, emb_dim)`
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
def __init__(self, emb_dim, dims=None, activation="dice", use_softmax=False):
|
|
70
|
+
super(ActivationUnit, self).__init__()
|
|
71
|
+
if dims is None:
|
|
72
|
+
dims = [36]
|
|
73
|
+
self.emb_dim = emb_dim
|
|
74
|
+
self.use_softmax = use_softmax
|
|
75
|
+
self.attention = MLP(4 * self.emb_dim, dims=dims, activation=activation)
|
|
76
|
+
|
|
77
|
+
def forward(self, history, target):
|
|
78
|
+
seq_length = history.size(1)
|
|
79
|
+
# batch_size,seq_length,emb_dim
|
|
80
|
+
target = target.unsqueeze(1).expand(-1, seq_length, -1)
|
|
81
|
+
att_input = torch.cat([target, history, target - history, target * history], dim=-1) # batch_size,seq_length,4*emb_dim
|
|
82
|
+
# (batch_size*seq_length,4*emb_dim)
|
|
83
|
+
att_weight = self.attention(att_input.view(-1, 4 * self.emb_dim))
|
|
84
|
+
# (batch_size*seq_length, 1) -> (batch_size,seq_length)
|
|
85
|
+
att_weight = att_weight.view(-1, seq_length)
|
|
86
|
+
if self.use_softmax:
|
|
87
|
+
att_weight = att_weight.softmax(dim=-1)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
# (batch_size, seq_length, 1) * (batch_size, seq_length, emb_dim)
|
|
91
|
+
# (batch_size,emb_dim)
|
|
92
|
+
output = (att_weight.unsqueeze(-1) * history).sum(dim=1)
|
|
93
|
+
return output
|
|
@@ -1,117 +1,101 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Date: create on 09/13/2022
|
|
3
|
-
References:
|
|
4
|
-
paper: (KDD'21) EDCN: Enhancing Explicit and Implicit Feature Interactions via Information Sharing for Parallel Deep CTR Models
|
|
5
|
-
url: https://dlp-kdd.github.io/assets/pdf/DLP-KDD_2021_paper_12.pdf
|
|
6
|
-
Authors: lailai, lailai_zxy@tju.edu.cn
|
|
7
|
-
"""
|
|
8
|
-
|
|
9
|
-
import torch
|
|
10
|
-
from torch import nn
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
super().__init__()
|
|
29
|
-
self.features = features
|
|
30
|
-
self.n_cross_layers = n_cross_layers
|
|
31
|
-
self.num_fields = len(features)
|
|
32
|
-
self.dims = sum([fea.embed_dim for fea in features])
|
|
33
|
-
self.fea_dims = [fea.embed_dim for fea in features]
|
|
34
|
-
self.embedding = EmbeddingLayer(features)
|
|
35
|
-
self.cross_layers = nn.ModuleList([CrossLayer(self.dims) for _ in range(n_cross_layers)])
|
|
36
|
-
self.bridge_modules = nn.ModuleList([BridgeModule(self.dims, bridge_type) for _ in range(n_cross_layers)])
|
|
37
|
-
self.regulation_modules = nn.ModuleList([RegulationModule(self.num_fields,
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
self.
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
self.g2 = nn.Parameter(torch.ones(num_fields))
|
|
103
|
-
|
|
104
|
-
def forward(self, x):
|
|
105
|
-
if self.use_regulation:
|
|
106
|
-
g1 = torch.cat([(self.g1[i]/ self.tau).softmax(dim=-1).unsqueeze(-1).repeat(1, self.dims[i]) for i in range(self.num_fields)], dim=-1)
|
|
107
|
-
g2 = torch.cat([(self.g2[i] / self.tau).softmax(dim=-1).unsqueeze(-1).repeat(1, self.dims[i]) for i in range(self.num_fields)], dim=-1)
|
|
108
|
-
|
|
109
|
-
out1, out2 = g1*x, g2*x
|
|
110
|
-
else:
|
|
111
|
-
out1, out2 = x, x
|
|
112
|
-
return out1, out2
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
1
|
+
"""
|
|
2
|
+
Date: create on 09/13/2022
|
|
3
|
+
References:
|
|
4
|
+
paper: (KDD'21) EDCN: Enhancing Explicit and Implicit Feature Interactions via Information Sharing for Parallel Deep CTR Models
|
|
5
|
+
url: https://dlp-kdd.github.io/assets/pdf/DLP-KDD_2021_paper_12.pdf
|
|
6
|
+
Authors: lailai, lailai_zxy@tju.edu.cn
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import torch
|
|
10
|
+
from torch import nn
|
|
11
|
+
|
|
12
|
+
from ...basic.layers import LR, MLP, CrossLayer, EmbeddingLayer
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class EDCN(torch.nn.Module):
|
|
16
|
+
"""Deep & Cross Network with a mixture of low-rank architecture
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
features (list[Feature Class]): training by the whole module.
|
|
20
|
+
n_cross_layers (int) : the number of layers of feature intersection layers
|
|
21
|
+
mlp_params (dict): the params of the last MLP module, keys include:`{"dims":list, "activation":str, "dropout":float, "output_layer":bool`}
|
|
22
|
+
bridge_type (str): the type interaction function, in ["hadamard_product", "pointwise_addition", "concatenation", "attention_pooling"]
|
|
23
|
+
use_regulation_module (bool): True, whether to use regulation module
|
|
24
|
+
temperature (int): the temperature coefficient to control distribution
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def __init__(self, features, n_cross_layers, mlp_params, bridge_type="hadamard_product", use_regulation_module=True, temperature=1):
|
|
28
|
+
super().__init__()
|
|
29
|
+
self.features = features
|
|
30
|
+
self.n_cross_layers = n_cross_layers
|
|
31
|
+
self.num_fields = len(features)
|
|
32
|
+
self.dims = sum([fea.embed_dim for fea in features])
|
|
33
|
+
self.fea_dims = [fea.embed_dim for fea in features]
|
|
34
|
+
self.embedding = EmbeddingLayer(features)
|
|
35
|
+
self.cross_layers = nn.ModuleList([CrossLayer(self.dims) for _ in range(n_cross_layers)])
|
|
36
|
+
self.bridge_modules = nn.ModuleList([BridgeModule(self.dims, bridge_type) for _ in range(n_cross_layers)])
|
|
37
|
+
self.regulation_modules = nn.ModuleList([RegulationModule(self.num_fields, self.fea_dims, tau=temperature, use_regulation=use_regulation_module) for _ in range(n_cross_layers)])
|
|
38
|
+
mlp_params["dims"] = [self.dims, self.dims]
|
|
39
|
+
self.mlps = nn.ModuleList([MLP(self.dims, output_layer=False, **mlp_params) for _ in range(n_cross_layers)])
|
|
40
|
+
self.linear = LR(self.dims * 3)
|
|
41
|
+
|
|
42
|
+
def forward(self, x):
|
|
43
|
+
embed_x = self.embedding(x, self.features, squeeze_dim=True)
|
|
44
|
+
cross_i, deep_i = self.regulation_modules[0](embed_x)
|
|
45
|
+
cross_0 = cross_i
|
|
46
|
+
for i in range(self.n_cross_layers):
|
|
47
|
+
if i > 0:
|
|
48
|
+
cross_i, deep_i = self.regulation_modules[i](bridge_i)
|
|
49
|
+
cross_i = cross_i + self.cross_layers[i](cross_0, cross_i)
|
|
50
|
+
deep_i = self.mlps[i](deep_i)
|
|
51
|
+
bridge_i = self.bridge_modules[i](cross_i, deep_i)
|
|
52
|
+
x_stack = torch.cat([cross_i, deep_i, bridge_i], dim=1)
|
|
53
|
+
y = self.linear(x_stack)
|
|
54
|
+
return torch.sigmoid(y.squeeze(1))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class BridgeModule(torch.nn.Module):
|
|
58
|
+
|
|
59
|
+
def __init__(self, input_dim, bridge_type):
|
|
60
|
+
super(BridgeModule, self).__init__()
|
|
61
|
+
assert bridge_type in ["hadamard_product", "pointwise_addition", "concatenation", "attention_pooling"], 'bridge_type= is not supported'.format(bridge_type)
|
|
62
|
+
self.bridge_type = bridge_type
|
|
63
|
+
if bridge_type == "concatenation":
|
|
64
|
+
self.concat_pooling = nn.Sequential(nn.Linear(input_dim * 2, input_dim), nn.ReLU())
|
|
65
|
+
elif bridge_type == "attention_pooling":
|
|
66
|
+
self.attention_x = nn.Sequential(nn.Linear(input_dim, input_dim), nn.ReLU(), nn.Linear(input_dim, input_dim, bias=False), nn.Softmax(dim=-1))
|
|
67
|
+
self.attention_h = nn.Sequential(nn.Linear(input_dim, input_dim), nn.ReLU(), nn.Linear(input_dim, input_dim, bias=False), nn.Softmax(dim=-1))
|
|
68
|
+
|
|
69
|
+
def forward(self, x, h):
|
|
70
|
+
if self.bridge_type == "hadamard_product":
|
|
71
|
+
out = x * h
|
|
72
|
+
elif self.bridge_type == "pointwise_addition":
|
|
73
|
+
out = x + h
|
|
74
|
+
elif self.bridge_type == "concatenation":
|
|
75
|
+
out = self.concat_pooling(torch.cat([x, h], dim=-1))
|
|
76
|
+
elif self.bridge_type == "attention_pooling":
|
|
77
|
+
out = self.attention_x(x) * x + self.attention_h(h) * h
|
|
78
|
+
return out
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class RegulationModule(torch.nn.Module):
|
|
82
|
+
|
|
83
|
+
def __init__(self, num_fields, dims, tau, use_regulation=True):
|
|
84
|
+
super(RegulationModule, self).__init__()
|
|
85
|
+
self.use_regulation = use_regulation
|
|
86
|
+
if self.use_regulation:
|
|
87
|
+
self.num_fields = num_fields
|
|
88
|
+
self.dims = dims
|
|
89
|
+
self.tau = tau
|
|
90
|
+
self.g1 = nn.Parameter(torch.ones(num_fields))
|
|
91
|
+
self.g2 = nn.Parameter(torch.ones(num_fields))
|
|
92
|
+
|
|
93
|
+
def forward(self, x):
|
|
94
|
+
if self.use_regulation:
|
|
95
|
+
g1 = torch.cat([(self.g1[i] / self.tau).softmax(dim=-1).unsqueeze(-1).repeat(1, self.dims[i]) for i in range(self.num_fields)], dim=-1)
|
|
96
|
+
g2 = torch.cat([(self.g2[i] / self.tau).softmax(dim=-1).unsqueeze(-1).repeat(1, self.dims[i]) for i in range(self.num_fields)], dim=-1)
|
|
97
|
+
|
|
98
|
+
out1, out2 = g1 * x, g2 * x
|
|
99
|
+
else:
|
|
100
|
+
out1, out2 = x, x
|
|
101
|
+
return out1, out2
|
|
@@ -1,50 +1,42 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Date: create on 10/19/2022
|
|
3
|
-
References:
|
|
4
|
-
paper: (RecSys '19) FiBiNET: combining feature importance and bilinear feature interaction for click-through rate prediction
|
|
5
|
-
url: https://dl.acm.org/doi/abs/10.1145/3298689.3347043
|
|
6
|
-
Authors: lailai, lailai_zxy@tju.edu.cn
|
|
7
|
-
"""
|
|
8
|
-
import torch
|
|
9
|
-
from torch import nn
|
|
10
|
-
|
|
11
|
-
from ...basic.features import SparseFeature
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
self.
|
|
27
|
-
self.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
1
|
+
"""
|
|
2
|
+
Date: create on 10/19/2022
|
|
3
|
+
References:
|
|
4
|
+
paper: (RecSys '19) FiBiNET: combining feature importance and bilinear feature interaction for click-through rate prediction
|
|
5
|
+
url: https://dl.acm.org/doi/abs/10.1145/3298689.3347043
|
|
6
|
+
Authors: lailai, lailai_zxy@tju.edu.cn
|
|
7
|
+
"""
|
|
8
|
+
import torch
|
|
9
|
+
from torch import nn
|
|
10
|
+
|
|
11
|
+
from ...basic.features import SparseFeature
|
|
12
|
+
from ...basic.layers import MLP, BiLinearInteractionLayer, EmbeddingLayer, SENETLayer
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class FiBiNet(torch.nn.Module):
|
|
16
|
+
"""
|
|
17
|
+
Args:
|
|
18
|
+
features (list[Feature Class]): training by the whole module.
|
|
19
|
+
reduction_ratio (int) : Hidden layer reduction factor of SENET layer
|
|
20
|
+
mlp_params (dict): the params of the last MLP module, keys include:`{"dims":list, "activation":str, "dropout":float, "output_layer":bool`}
|
|
21
|
+
bilinear_type (str): the type bilinear interaction function, in ["field_all", "field_each", "field_interaction"], field_all means that all features share a W, field_each means that a feature field corresponds to a W_i, field_interaction means that a feature field intersection corresponds to a W_ij
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(self, features, mlp_params, reduction_ratio=3, bilinear_type="field_interaction", **kwargs):
|
|
25
|
+
super(FiBiNet, self).__init__()
|
|
26
|
+
self.features = features
|
|
27
|
+
self.embedding = EmbeddingLayer(features)
|
|
28
|
+
embedding_dim = max([fea.embed_dim for fea in features])
|
|
29
|
+
num_fields = len([fea.embed_dim for fea in features if isinstance(fea, SparseFeature) and fea.shared_with is None])
|
|
30
|
+
self.senet_layer = SENETLayer(num_fields, reduction_ratio)
|
|
31
|
+
self.bilinear_interaction = BiLinearInteractionLayer(embedding_dim, num_fields, bilinear_type)
|
|
32
|
+
self.dims = num_fields * (num_fields - 1) * embedding_dim
|
|
33
|
+
self.mlp = MLP(self.dims, **mlp_params)
|
|
34
|
+
|
|
35
|
+
def forward(self, x):
|
|
36
|
+
embed_x = self.embedding(x, self.features)
|
|
37
|
+
embed_senet = self.senet_layer(embed_x)
|
|
38
|
+
embed_bi1 = self.bilinear_interaction(embed_x)
|
|
39
|
+
embed_bi2 = self.bilinear_interaction(embed_senet)
|
|
40
|
+
shallow_part = torch.flatten(torch.cat([embed_bi1, embed_bi2], dim=1), start_dim=1)
|
|
41
|
+
mlp_out = self.mlp(shallow_part)
|
|
42
|
+
return torch.sigmoid(mlp_out.squeeze(1))
|
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Date: create on 22/04/2022
|
|
3
|
-
References:
|
|
4
|
-
paper: (DLRS'2016) Wide & Deep Learning for Recommender Systems
|
|
5
|
-
url: https://arxiv.org/abs/1606.07792
|
|
6
|
-
Authors: Mincai Lai, laimincai@shanghaitech.edu.cn
|
|
7
|
-
"""
|
|
8
|
-
|
|
9
|
-
import torch
|
|
10
|
-
|
|
11
|
-
from ...basic.layers import LR, MLP, EmbeddingLayer
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class WideDeep(torch.nn.Module):
|
|
15
|
-
"""Wide & Deep Learning model.
|
|
16
|
-
|
|
17
|
-
Args:
|
|
18
|
-
wide_features (list): the list of `Feature Class`, training by the wide part module.
|
|
19
|
-
deep_features (list): the list of `Feature Class`, training by the deep part module.
|
|
20
|
-
mlp_params (dict): the params of the last MLP module, keys include:`{"dims":list, "activation":str, "dropout":float, "output_layer":bool`}
|
|
21
|
-
"""
|
|
22
|
-
|
|
23
|
-
def __init__(self, wide_features, deep_features, mlp_params):
|
|
24
|
-
super(WideDeep, self).__init__()
|
|
25
|
-
self.wide_features = wide_features
|
|
26
|
-
self.deep_features = deep_features
|
|
27
|
-
self.wide_dims = sum([fea.embed_dim for fea in wide_features])
|
|
28
|
-
self.deep_dims = sum([fea.embed_dim for fea in deep_features])
|
|
29
|
-
self.linear = LR(self.wide_dims)
|
|
30
|
-
self.embedding = EmbeddingLayer(wide_features + deep_features)
|
|
31
|
-
self.mlp = MLP(self.deep_dims, **mlp_params)
|
|
32
|
-
|
|
33
|
-
def forward(self, x):
|
|
34
|
-
input_wide = self.embedding(x, self.wide_features, squeeze_dim=True) #[batch_size, wide_dims]
|
|
35
|
-
input_deep = self.embedding(x, self.deep_features, squeeze_dim=True) #[batch_size, deep_dims]
|
|
36
|
-
|
|
37
|
-
y_wide = self.linear(input_wide) #[batch_size, 1]
|
|
38
|
-
y_deep = self.mlp(input_deep) #[batch_size, 1]
|
|
39
|
-
y = y_wide + y_deep
|
|
40
|
-
y = torch.sigmoid(y.squeeze(1))
|
|
41
|
-
return y
|
|
1
|
+
"""
|
|
2
|
+
Date: create on 22/04/2022
|
|
3
|
+
References:
|
|
4
|
+
paper: (DLRS'2016) Wide & Deep Learning for Recommender Systems
|
|
5
|
+
url: https://arxiv.org/abs/1606.07792
|
|
6
|
+
Authors: Mincai Lai, laimincai@shanghaitech.edu.cn
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import torch
|
|
10
|
+
|
|
11
|
+
from ...basic.layers import LR, MLP, EmbeddingLayer
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class WideDeep(torch.nn.Module):
|
|
15
|
+
"""Wide & Deep Learning model.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
wide_features (list): the list of `Feature Class`, training by the wide part module.
|
|
19
|
+
deep_features (list): the list of `Feature Class`, training by the deep part module.
|
|
20
|
+
mlp_params (dict): the params of the last MLP module, keys include:`{"dims":list, "activation":str, "dropout":float, "output_layer":bool`}
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(self, wide_features, deep_features, mlp_params):
|
|
24
|
+
super(WideDeep, self).__init__()
|
|
25
|
+
self.wide_features = wide_features
|
|
26
|
+
self.deep_features = deep_features
|
|
27
|
+
self.wide_dims = sum([fea.embed_dim for fea in wide_features])
|
|
28
|
+
self.deep_dims = sum([fea.embed_dim for fea in deep_features])
|
|
29
|
+
self.linear = LR(self.wide_dims)
|
|
30
|
+
self.embedding = EmbeddingLayer(wide_features + deep_features)
|
|
31
|
+
self.mlp = MLP(self.deep_dims, **mlp_params)
|
|
32
|
+
|
|
33
|
+
def forward(self, x):
|
|
34
|
+
input_wide = self.embedding(x, self.wide_features, squeeze_dim=True) # [batch_size, wide_dims]
|
|
35
|
+
input_deep = self.embedding(x, self.deep_features, squeeze_dim=True) # [batch_size, deep_dims]
|
|
36
|
+
|
|
37
|
+
y_wide = self.linear(input_wide) # [batch_size, 1]
|
|
38
|
+
y_deep = self.mlp(input_deep) # [batch_size, 1]
|
|
39
|
+
y = y_wide + y_deep
|
|
40
|
+
y = torch.sigmoid(y.squeeze(1))
|
|
41
|
+
return y
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
from .ctr_trainer import CTRTrainer
|
|
2
|
-
from .match_trainer import MatchTrainer
|
|
3
|
-
from .mtl_trainer import MTLTrainer
|
|
1
|
+
from .ctr_trainer import CTRTrainer
|
|
2
|
+
from .match_trainer import MatchTrainer
|
|
3
|
+
from .mtl_trainer import MTLTrainer
|
|
4
|
+
from .seq_trainer import SeqTrainer
|