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.
Files changed (64) hide show
  1. torch_rechub/__init__.py +14 -0
  2. torch_rechub/basic/activation.py +54 -54
  3. torch_rechub/basic/callback.py +33 -33
  4. torch_rechub/basic/features.py +87 -94
  5. torch_rechub/basic/initializers.py +92 -92
  6. torch_rechub/basic/layers.py +994 -720
  7. torch_rechub/basic/loss_func.py +223 -34
  8. torch_rechub/basic/metaoptimizer.py +76 -72
  9. torch_rechub/basic/metric.py +251 -250
  10. torch_rechub/models/generative/__init__.py +6 -0
  11. torch_rechub/models/generative/hllm.py +249 -0
  12. torch_rechub/models/generative/hstu.py +189 -0
  13. torch_rechub/models/matching/__init__.py +13 -11
  14. torch_rechub/models/matching/comirec.py +193 -188
  15. torch_rechub/models/matching/dssm.py +72 -66
  16. torch_rechub/models/matching/dssm_facebook.py +77 -79
  17. torch_rechub/models/matching/dssm_senet.py +28 -16
  18. torch_rechub/models/matching/gru4rec.py +85 -87
  19. torch_rechub/models/matching/mind.py +103 -101
  20. torch_rechub/models/matching/narm.py +82 -76
  21. torch_rechub/models/matching/sasrec.py +143 -140
  22. torch_rechub/models/matching/sine.py +148 -151
  23. torch_rechub/models/matching/stamp.py +81 -83
  24. torch_rechub/models/matching/youtube_dnn.py +75 -71
  25. torch_rechub/models/matching/youtube_sbc.py +98 -98
  26. torch_rechub/models/multi_task/__init__.py +7 -5
  27. torch_rechub/models/multi_task/aitm.py +83 -84
  28. torch_rechub/models/multi_task/esmm.py +56 -55
  29. torch_rechub/models/multi_task/mmoe.py +58 -58
  30. torch_rechub/models/multi_task/ple.py +116 -130
  31. torch_rechub/models/multi_task/shared_bottom.py +45 -45
  32. torch_rechub/models/ranking/__init__.py +14 -11
  33. torch_rechub/models/ranking/afm.py +65 -63
  34. torch_rechub/models/ranking/autoint.py +102 -0
  35. torch_rechub/models/ranking/bst.py +61 -63
  36. torch_rechub/models/ranking/dcn.py +38 -38
  37. torch_rechub/models/ranking/dcn_v2.py +59 -69
  38. torch_rechub/models/ranking/deepffm.py +131 -123
  39. torch_rechub/models/ranking/deepfm.py +43 -42
  40. torch_rechub/models/ranking/dien.py +191 -191
  41. torch_rechub/models/ranking/din.py +93 -91
  42. torch_rechub/models/ranking/edcn.py +101 -117
  43. torch_rechub/models/ranking/fibinet.py +42 -50
  44. torch_rechub/models/ranking/widedeep.py +41 -41
  45. torch_rechub/trainers/__init__.py +4 -3
  46. torch_rechub/trainers/ctr_trainer.py +288 -128
  47. torch_rechub/trainers/match_trainer.py +336 -170
  48. torch_rechub/trainers/matching.md +3 -0
  49. torch_rechub/trainers/mtl_trainer.py +356 -207
  50. torch_rechub/trainers/seq_trainer.py +427 -0
  51. torch_rechub/utils/data.py +492 -360
  52. torch_rechub/utils/hstu_utils.py +198 -0
  53. torch_rechub/utils/match.py +457 -274
  54. torch_rechub/utils/model_utils.py +233 -0
  55. torch_rechub/utils/mtl.py +136 -126
  56. torch_rechub/utils/onnx_export.py +220 -0
  57. torch_rechub/utils/visualization.py +271 -0
  58. torch_rechub-0.0.5.dist-info/METADATA +402 -0
  59. torch_rechub-0.0.5.dist-info/RECORD +64 -0
  60. {torch_rechub-0.0.3.dist-info → torch_rechub-0.0.5.dist-info}/WHEEL +1 -2
  61. {torch_rechub-0.0.3.dist-info → torch_rechub-0.0.5.dist-info/licenses}/LICENSE +21 -21
  62. torch_rechub-0.0.3.dist-info/METADATA +0 -177
  63. torch_rechub-0.0.3.dist-info/RECORD +0 -55
  64. 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 EmbeddingLayer, MLP
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
- [ActivationUnit(fea.embed_dim, **attention_mlp_params) for fea in self.history_features])
37
- self.mlp = MLP(self.all_dims, activation="dice", **mlp_params)
38
-
39
- def forward(self, x):
40
- embed_x_features = self.embedding(x, self.features) #(batch_size, num_features, emb_dim)
41
- embed_x_history = self.embedding(
42
- x, self.history_features) #(batch_size, num_history_features, seq_length, emb_dim)
43
- embed_x_target = self.embedding(x, self.target_features) #(batch_size, num_target_features, emb_dim)
44
- attention_pooling = []
45
- for i in range(self.num_history_features):
46
- attention_seq = self.attention_layers[i](embed_x_history[:, i, :, :], embed_x_target[:, i, :])
47
- attention_pooling.append(attention_seq.unsqueeze(1)) #(batch_size, 1, emb_dim)
48
- attention_pooling = torch.cat(attention_pooling, dim=1) #(batch_size, num_history_features, emb_dim)
49
-
50
- mlp_in = torch.cat([
51
- attention_pooling.flatten(start_dim=1),
52
- embed_x_target.flatten(start_dim=1),
53
- embed_x_features.flatten(start_dim=1)
54
- ],
55
- dim=1) #(batch_size, N)
56
-
57
- y = self.mlp(mlp_in)
58
- return torch.sigmoid(y.squeeze(1))
59
-
60
-
61
- class ActivationUnit(nn.Module):
62
- """Activation Unit Layer mentioned in DIN paper, it is a Target Attention method.
63
-
64
- Args:
65
- embed_dim (int): the length of embedding vector.
66
- history (tensor):
67
- Shape:
68
- - Input: `(batch_size, seq_length, emb_dim)`
69
- - Output: `(batch_size, emb_dim)`
70
- """
71
-
72
- def __init__(self, emb_dim, dims=None, activation="dice", use_softmax=False):
73
- super(ActivationUnit, self).__init__()
74
- if dims is None:
75
- dims = [36]
76
- self.emb_dim = emb_dim
77
- self.use_softmax = use_softmax
78
- self.attention = MLP(4 * self.emb_dim, dims=dims, activation=activation)
79
-
80
- def forward(self, history, target):
81
- seq_length = history.size(1)
82
- target = target.unsqueeze(1).expand(-1, seq_length, -1) #batch_size,seq_length,emb_dim
83
- att_input = torch.cat([target, history, target - history, target * history],
84
- dim=-1) # batch_size,seq_length,4*emb_dim
85
- att_weight = self.attention(att_input.view(-1, 4 * self.emb_dim)) # #(batch_size*seq_length,4*emb_dim)
86
- att_weight = att_weight.view(-1, seq_length) #(batch_size*seq_length, 1) -> (batch_size,seq_length)
87
- if self.use_softmax:
88
- att_weight = att_weight.softmax(dim=-1)
89
- # (batch_size, seq_length, 1) * (batch_size, seq_length, emb_dim)
90
- output = (att_weight.unsqueeze(-1) * history).sum(dim=1) #(batch_size,emb_dim)
91
- return output
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
- from ...basic.layers import LR, MLP, CrossLayer, EmbeddingLayer
12
-
13
-
14
- class EDCN(torch.nn.Module):
15
- """Deep & Cross Network with a mixture of low-rank architecture
16
-
17
- Args:
18
- features (list[Feature Class]): training by the whole module.
19
- n_cross_layers (int) : the number of layers of feature intersection layers
20
- mlp_params (dict): the params of the last MLP module, keys include:`{"dims":list, "activation":str, "dropout":float, "output_layer":bool`}
21
- bridge_type (str): the type interaction function, in ["hadamard_product", "pointwise_addition", "concatenation", "attention_pooling"]
22
- use_regulation_module (bool): True, whether to use regulation module
23
- temperature (int): the temperature coefficient to control distribution
24
- """
25
-
26
- def __init__(self, features, n_cross_layers, mlp_params, bridge_type="hadamard_product", use_regulation_module=True,
27
- 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,
38
- self.fea_dims,
39
- tau=temperature,
40
- use_regulation=use_regulation_module) for _ in range(n_cross_layers)])
41
- mlp_params["dims"] = [self.dims, self.dims]
42
- self.mlps = nn.ModuleList([MLP(self.dims, output_layer=False, **mlp_params) for _ in range(n_cross_layers)])
43
- self.linear = LR(self.dims * 3)
44
-
45
- def forward(self, x):
46
- embed_x = self.embedding(x, self.features, squeeze_dim=True)
47
- cross_i, deep_i = self.regulation_modules[0](embed_x)
48
- cross_0 = cross_i
49
- for i in range(self.n_cross_layers):
50
- if i>0:
51
- cross_i, deep_i = self.regulation_modules[i](bridge_i)
52
- cross_i = cross_i + self.cross_layers[i](cross_0, cross_i)
53
- deep_i = self.mlps[i](deep_i)
54
- bridge_i = self.bridge_modules[i](cross_i, deep_i)
55
- x_stack = torch.cat([cross_i, deep_i, bridge_i], dim=1)
56
- y = self.linear(x_stack)
57
- return torch.sigmoid(y.squeeze(1))
58
-
59
-
60
- class BridgeModule(torch.nn.Module):
61
- def __init__(self, input_dim, bridge_type):
62
- super(BridgeModule, self).__init__()
63
- assert bridge_type in ["hadamard_product", "pointwise_addition", "concatenation",
64
- "attention_pooling"], 'bridge_type= is not supported'.format(bridge_type)
65
- self.bridge_type = bridge_type
66
- if bridge_type=="concatenation":
67
- self.concat_pooling = nn.Sequential(nn.Linear(input_dim * 2, input_dim),
68
- nn.ReLU())
69
- elif bridge_type=="attention_pooling":
70
- self.attention_x = nn.Sequential(nn.Linear(input_dim, input_dim),
71
- nn.ReLU(),
72
- nn.Linear(input_dim, input_dim,bias=False),
73
- nn.Softmax(dim=-1))
74
- self.attention_h = nn.Sequential(nn.Linear(input_dim, input_dim),
75
- nn.ReLU(),
76
- nn.Linear(input_dim, input_dim,bias=False),
77
- nn.Softmax(dim=-1))
78
- def forward(self, x, h):
79
- if self.bridge_type == "hadamard_product":
80
- out = x * h
81
- elif self.bridge_type == "pointwise_addition":
82
- out = x + h
83
- elif self.bridge_type == "concatenation":
84
- out = self.concat_pooling(torch.cat([x, h], dim=-1))
85
- elif self.bridge_type == "attention_pooling":
86
- out = self.attention_x(x) * x + self.attention_h(h) * h
87
- return out
88
-
89
-
90
- class RegulationModule(torch.nn.Module):
91
- def __init__(self, num_fields,
92
- dims,
93
- tau,
94
- use_regulation=True):
95
- super(RegulationModule, self).__init__()
96
- self.use_regulation = use_regulation
97
- if self.use_regulation:
98
- self.num_fields = num_fields
99
- self.dims = dims
100
- self.tau = tau
101
- self.g1 = nn.Parameter(torch.ones(num_fields))
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
- from ...basic.layers import MLP, EmbeddingLayer, SENETLayer, BiLinearInteractionLayer
11
- from ...basic.features import SparseFeature
12
- class FiBiNet(torch.nn.Module):
13
- """
14
- Args:
15
- features (list[Feature Class]): training by the whole module.
16
- reduction_ratio (int) : Hidden layer reduction factor of SENET layer
17
- mlp_params (dict): the params of the last MLP module, keys include:`{"dims":list, "activation":str, "dropout":float, "output_layer":bool`}
18
- 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
19
- """
20
- def __init__(self, features, mlp_params, reduction_ratio=3, bilinear_type="field_interaction", **kwargs):
21
- super(FiBiNet, self).__init__()
22
- self.features = features
23
- self.embedding = EmbeddingLayer(features)
24
- embedding_dim = max([fea.embed_dim for fea in features])
25
- num_fields = len([fea.embed_dim for fea in features if isinstance(fea, SparseFeature) and fea.shared_with == None])
26
- self.senet_layer = SENETLayer(num_fields, reduction_ratio)
27
- self.bilinear_interaction = BiLinearInteractionLayer(embedding_dim, num_fields, bilinear_type)
28
- self.dims = num_fields * (num_fields - 1) * embedding_dim
29
- self.mlp = MLP(self.dims, **mlp_params)
30
-
31
- def forward(self, x):
32
- embed_x = self.embedding(x, self.features)
33
- embed_senet = self.senet_layer(embed_x)
34
- embed_bi1 = self.bilinear_interaction(embed_x)
35
- embed_bi2 = self.bilinear_interaction(embed_senet)
36
- shallow_part = torch.flatten(torch.cat([embed_bi1, embed_bi2], dim=1), start_dim=1)
37
- mlp_out = self.mlp(shallow_part)
38
- return torch.sigmoid(mlp_out.squeeze(1))
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