-
Notifications
You must be signed in to change notification settings - Fork 648
/
Copy pathnet.py
347 lines (293 loc) · 13.4 KB
/
net.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import paddle
import paddle.nn as nn
import paddle.nn.functional as fun
import math
class MLPLayer(nn.Layer):
def __init__(self, input_shape, units_list=None, activation=None,
**kwargs):
super(MLPLayer, self).__init__(**kwargs)
if units_list is None:
units_list = [128, 128, 64]
units_list = [input_shape] + units_list
self.units_list = units_list
self.mlp = []
self.activation = activation
for i, unit in enumerate(units_list[:-1]):
if i != len(units_list) - 1:
dense = paddle.nn.Linear(
in_features=unit,
out_features=units_list[i + 1],
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.TruncatedNormal(
std=1.0 / math.sqrt(unit))))
self.mlp.append(dense)
self.add_sublayer('dense_%d' % i, dense)
relu = paddle.nn.ReLU()
self.mlp.append(relu)
self.add_sublayer('relu_%d' % i, relu)
norm = paddle.nn.BatchNorm1D(units_list[i + 1])
self.mlp.append(norm)
self.add_sublayer('norm_%d' % i, norm)
else:
dense = paddle.nn.Linear(
in_features=unit,
out_features=units_list[i + 1],
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.TruncatedNormal(
std=1.0 / math.sqrt(unit))))
self.mlp.append(dense)
self.add_sublayer('dense_%d' % i, dense)
if self.activation is not None:
relu = paddle.nn.ReLU()
self.mlp.append(relu)
self.add_sublayer('relu_%d' % i, relu)
def forward(self, inputs):
outputs = inputs
for n_layer in self.mlp:
outputs = n_layer(outputs)
return outputs
class FENLayer(nn.Layer):
def __init__(self, sparse_field_num, sparse_feature_num,
sparse_feature_dim, dense_feature_dim, fen_layers_size,
dense_layers_size):
super(FENLayer, self).__init__()
self.sparse_field_num = sparse_field_num
self.sparse_feature_num = sparse_feature_num
self.sparse_feature_dim = sparse_feature_dim
self.dense_feature_dim = dense_feature_dim
self.fen_layers_size = fen_layers_size
self.dense_layers_size = dense_layers_size
self.fen_mlp = MLPLayer(
input_shape=(sparse_field_num + 1) * sparse_feature_dim,
units_list=fen_layers_size,
activation="relu")
use_sparse = True
if paddle.is_compiled_with_custom_device('npu'):
use_sparse = False
self.sparse_embedding = paddle.nn.Embedding(
num_embeddings=self.sparse_feature_num,
embedding_dim=self.sparse_feature_dim,
sparse=use_sparse,
weight_attr=paddle.ParamAttr(
name="SparseFeatFactors",
initializer=paddle.nn.initializer.Uniform()))
self.sparse_weight = paddle.nn.Embedding(
num_embeddings=self.sparse_feature_num,
embedding_dim=1,
sparse=use_sparse,
weight_attr=paddle.ParamAttr(
name="SparseFeatFactors2",
initializer=paddle.nn.initializer.Uniform()))
self.dense_linear = paddle.nn.Linear(
in_features=self.dense_feature_dim, out_features=1)
self.dense_mlp = MLPLayer(
input_shape=self.dense_feature_dim,
units_list=self.dense_layers_size,
activation="relu")
self.dnn_mlp = MLPLayer(
input_shape=self.sparse_feature_dim,
units_list=[1],
activation="relu")
def forward(self, sparse_inputs, dense_inputs):
# ------------------ first order ------------------------------------
# (batch_size, sparse_field_num)
sparse_inputs_concat = paddle.concat(sparse_inputs, axis=1)
# (batch_size, sparse_field_num, 1)
sparse_emb_one = self.sparse_weight(sparse_inputs_concat)
# (batch_size, sparse_field_num)
sparse_emb_one = paddle.squeeze(sparse_emb_one, axis=-1)
# (batch_size, 1)
dense_emb_one = self.dense_linear(dense_inputs)
# (batch_size, (sparse_field_num + 1))
feat_emb_one = paddle.concat([dense_emb_one, sparse_emb_one], axis=1)
# -------------------- fen layer ------------------------------------
# (batch_size, embedding_size)
dense_embedding = self.dense_mlp(dense_inputs)
dnn_logits = self.dnn_mlp(dense_embedding)
dense_embedding = paddle.unsqueeze(dense_embedding, axis=1)
# (batch_size, sparse_field_num, embedding_size)
sparse_embedding = self.sparse_embedding(sparse_inputs_concat)
# (batch_size, (sparse_field_num + 1), embedding_size)
feat_embeddings = paddle.concat(
[dense_embedding, sparse_embedding], axis=1)
batch_size, sparse_field_num_1, embedding_size = feat_embeddings.shape
# (batch_size, (sparse_field_num + 1))
m_x = self.fen_mlp(
paddle.reshape(
feat_embeddings,
shape=(batch_size, sparse_field_num_1 * embedding_size)))
return dnn_logits, feat_emb_one, feat_embeddings, m_x
class FMLayer(nn.Layer):
def __init__(self):
super(FMLayer, self).__init__()
self.bias = paddle.create_parameter(
is_bias=True, shape=[1], dtype='float32')
def forward(self, dnn_logits, first_order, combined_features):
"""
first_order: FM first order (batch_size, 1)
combined_features: FM sparse features (batch_size, sparse_field_num + 1, embedding_size)
"""
# sum square part
# (batch_size, embedding_size)
summed_features_emb = paddle.sum(combined_features, axis=1)
summed_features_emb_square = paddle.square(summed_features_emb)
# square sum part
squared_features_emb = paddle.square(combined_features)
# (batch_size, embedding_size)
squared_sum_features_emb = paddle.sum(squared_features_emb, axis=1)
# (batch_size, 1)
logits = first_order + 0.5 * paddle.sum(
summed_features_emb_square - squared_sum_features_emb,
axis=1,
keepdim=True) + self.bias + dnn_logits
return fun.sigmoid(logits)
class MultiHeadAttentionLayer(nn.Layer):
def __init__(self, att_factor_dim, att_head_num, sparse_feature_dim,
sparse_field_num):
super(MultiHeadAttentionLayer, self).__init__()
self.att_factor_dim = att_factor_dim
self.att_head_num = att_head_num
self.sparse_feature_dim = sparse_feature_dim
self.sparse_field_num = sparse_field_num
self.W_Query = paddle.create_parameter(
default_initializer=nn.initializer.TruncatedNormal(),
shape=[
self.sparse_feature_dim,
self.att_factor_dim * self.att_head_num
],
dtype='float32')
self.W_Key = paddle.create_parameter(
default_initializer=nn.initializer.TruncatedNormal(),
shape=[
self.sparse_feature_dim,
self.att_factor_dim * self.att_head_num
],
dtype='float32')
self.W_Value = paddle.create_parameter(
default_initializer=nn.initializer.TruncatedNormal(),
shape=[
self.sparse_feature_dim,
self.att_factor_dim * self.att_head_num
],
dtype='float32')
self.W_Res = paddle.create_parameter(
default_initializer=nn.initializer.TruncatedNormal(),
shape=[
self.sparse_feature_dim,
self.att_factor_dim * self.att_head_num
],
dtype='float32')
self.dnn_layer = MLPLayer(
input_shape=(self.sparse_field_num + 1) * self.att_factor_dim *
self.att_head_num,
units_list=[self.sparse_field_num + 1],
activation="relu")
def forward(self, combined_features):
"""
combined_features: (batch_size, (sparse_field_num + 1), embedding_size)
W_Query: (embedding_size, factor_dim * att_head_num)
(b, f, e) * (e, d*h) -> (b, f, d*h)
"""
# (b, f, d*h)
querys = paddle.matmul(combined_features, self.W_Query)
keys = paddle.matmul(combined_features, self.W_Key)
values = paddle.matmul(combined_features, self.W_Value)
b, f, d_h = querys.shape
# (h, b, f, d) <- (b, f, d)
querys = paddle.stack(paddle.split(querys, self.att_head_num, axis=2))
keys = paddle.stack(paddle.split(keys, self.att_head_num, axis=2))
values = paddle.stack(paddle.split(values, self.att_head_num, axis=2))
# (h, b, f, f)
inner_product = paddle.matmul(querys, keys, transpose_y=True)
inner_product /= self.att_factor_dim**0.5
normalized_att_scores = fun.softmax(inner_product)
# (h, b, f, d)
result = paddle.matmul(normalized_att_scores, values)
result = paddle.concat(
paddle.split(
result, self.att_head_num, axis=0), axis=-1)
# (b, f, h * d)
result = paddle.squeeze(result, axis=0)
result += paddle.matmul(combined_features, self.W_Res)
# (b, f * h * d)
result = paddle.reshape(result, shape=(b, f * d_h))
m_vec = self.dnn_layer(result)
return m_vec
class IFM(nn.Layer):
def __init__(self, sparse_field_num, sparse_feature_num,
sparse_feature_dim, dense_feature_dim, fen_layers_size,
dense_layers_size):
super(IFM, self).__init__()
self.sparse_field_num = sparse_field_num
self.sparse_feature_num = sparse_feature_num
self.sparse_feature_dim = sparse_feature_dim
self.dense_feature_dim = dense_feature_dim
self.fen_layers_size = fen_layers_size
self.dense_layers_size = dense_layers_size
self.fen_layer = FENLayer(
sparse_field_num=self.sparse_field_num,
sparse_feature_num=self.sparse_feature_num,
sparse_feature_dim=self.sparse_feature_dim,
dense_feature_dim=self.dense_feature_dim,
fen_layers_size=self.fen_layers_size,
dense_layers_size=self.dense_layers_size)
self.fm_layer = FMLayer()
def forward(self, sparse_inputs, dense_inputs):
dnn_logits, feat_emb_one, feat_embeddings, m_x = self.fen_layer(
sparse_inputs, dense_inputs)
m_x = fun.softmax(m_x)
# (batch_size, (sparse_field_num + 1))
feat_emb_one = feat_emb_one * m_x
# (batch_size, (sparse_field_num + 1), embedding_size)
feat_embeddings = feat_embeddings * paddle.unsqueeze(m_x, axis=-1)
# (batch_size, 1)
first_order = paddle.sum(feat_emb_one, axis=1, keepdim=True)
return self.fm_layer(dnn_logits, first_order, feat_embeddings)
class DIFM(nn.Layer):
def __init__(self, sparse_field_num, sparse_feature_num,
sparse_feature_dim, dense_feature_dim, fen_layers_size,
dense_layers_size, att_factor_dim, att_head_num):
super(DIFM, self).__init__()
self.sparse_field_num = sparse_field_num
self.sparse_feature_num = sparse_feature_num
self.sparse_feature_dim = sparse_feature_dim
self.dense_feature_dim = dense_feature_dim
self.fen_layers_size = fen_layers_size
self.dense_layers_size = dense_layers_size
self.att_factor_dim = att_factor_dim
self.att_head_num = att_head_num
self.fen_layer = FENLayer(
sparse_field_num=self.sparse_field_num,
sparse_feature_num=self.sparse_feature_num,
sparse_feature_dim=self.sparse_feature_dim,
dense_feature_dim=self.dense_feature_dim,
fen_layers_size=self.fen_layers_size,
dense_layers_size=self.dense_layers_size)
self.fm_layer = FMLayer()
self.mha_layer = MultiHeadAttentionLayer(
att_factor_dim=self.att_factor_dim,
att_head_num=self.att_head_num,
sparse_feature_dim=self.sparse_feature_dim,
sparse_field_num=self.sparse_field_num)
def forward(self, sparse_inputs, dense_inputs):
dnn_logits, feat_emb_one, feat_embeddings, m_bit = self.fen_layer(
sparse_inputs, dense_inputs)
m_vec = self.mha_layer(feat_embeddings)
m = fun.softmax(m_vec + m_bit)
feat_emb_one = feat_emb_one * m
feat_embeddings = feat_embeddings * paddle.unsqueeze(m, axis=-1)
first_order = paddle.sum(feat_emb_one, axis=1, keepdim=True)
return self.fm_layer(dnn_logits, first_order, feat_embeddings)