forked from ZiyaoGeng/RecLearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
64 lines (56 loc) · 2.62 KB
/
model.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
"""
Created on July 9, 2020
model: Wide & Deep Learning for Recommender Systems
@author: Ziyao Geng([email protected])
"""
import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense, Embedding, Dropout, Input
from tensorflow.keras.regularizers import l2
from modules import Linear, DNN
class WideDeep(Model):
def __init__(self, feature_columns, hidden_units, activation='relu',
dnn_dropout=0., embed_reg=1e-6, w_reg=1e-6):
"""
Wide&Deep
:param feature_columns: A list. sparse column feature information.
:param hidden_units: A list. Neural network hidden units.
:param activation: A string. Activation function of dnn.
:param dnn_dropout: A scalar. Dropout of dnn.
:param embed_reg: A scalar. The regularizer of embedding.
:param w_reg: A scalar. The regularizer of Linear.
"""
super(WideDeep, self).__init__()
self.sparse_feature_columns = feature_columns
self.embed_layers = {
'embed_' + str(i): Embedding(input_dim=feat['feat_num'],
input_length=1,
output_dim=feat['embed_dim'],
embeddings_initializer='random_uniform',
embeddings_regularizer=l2(embed_reg))
for i, feat in enumerate(self.sparse_feature_columns)
}
self.index_mapping = []
self.feature_length = 0
for feat in self.sparse_feature_columns:
self.index_mapping.append(self.feature_length)
self.feature_length += feat['feat_num']
self.dnn_network = DNN(hidden_units, activation, dnn_dropout)
self.linear = Linear(self.feature_length, w_reg=w_reg)
self.final_dense = Dense(1, activation=None)
def call(self, inputs, **kwargs):
sparse_embed = tf.concat([self.embed_layers['embed_{}'.format(i)](inputs[:, i])
for i in range(inputs.shape[1])], axis=-1)
x = sparse_embed # (batch_size, field * embed_dim)
# Wide
wide_inputs = inputs + tf.convert_to_tensor(self.index_mapping)
wide_out = self.linear(wide_inputs)
# Deep
deep_out = self.dnn_network(x)
deep_out = self.final_dense(deep_out)
# out
outputs = tf.nn.sigmoid(0.5 * wide_out + 0.5 * deep_out)
return outputs
def summary(self, **kwargs):
sparse_inputs = Input(shape=(len(self.sparse_feature_columns),), dtype=tf.int32)
Model(inputs=sparse_inputs, outputs=self.call(sparse_inputs)).summary()