MCPcopy
hub / github.com/shenweichen/DeepCTR-Torch / DeepFM

Class DeepFM

deepctr_torch/models/deepfm.py:16–86  ·  view source on GitHub ↗

Instantiates the DeepFM Network architecture. :param linear_feature_columns: An iterable containing all the features used by linear part of the model. :param dnn_feature_columns: An iterable containing all the features used by deep part of the model. :param use_fm: bool,use FM part or n

Source from the content-addressed store, hash-verified

14
15
16class DeepFM(BaseModel):
17 """Instantiates the DeepFM Network architecture.
18
19 :param linear_feature_columns: An iterable containing all the features used by linear part of the model.
20 :param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
21 :param use_fm: bool,use FM part or not
22 :param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of DNN
23 :param l2_reg_linear: float. L2 regularizer strength applied to linear part
24 :param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
25 :param l2_reg_dnn: float. L2 regularizer strength applied to DNN
26 :param init_std: float,to use as the initialize std of embedding vector
27 :param seed: integer ,to use as random seed.
28 :param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
29 :param dnn_activation: Activation function to use in DNN
30 :param dnn_use_bn: bool. Whether use BatchNormalization before activation or not in DNN
31 :param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
32 :param device: str, ``"cpu"`` or ``"cuda:0"``
33 :param gpus: list of int or torch.device for multiple gpus. If None, run on `device`. `gpus[0]` should be the same gpu with `device`.
34 :return: A PyTorch model instance.
35
36 """
37
38 def __init__(self,
39 linear_feature_columns, dnn_feature_columns, use_fm=True,
40 dnn_hidden_units=(256, 128),
41 l2_reg_linear=0.00001, l2_reg_embedding=0.00001, l2_reg_dnn=0, init_std=0.0001, seed=1024,
42 dnn_dropout=0,
43 dnn_activation='relu', dnn_use_bn=False, task='binary', device='cpu', gpus=None):
44
45 super(DeepFM, self).__init__(linear_feature_columns, dnn_feature_columns, l2_reg_linear=l2_reg_linear,
46 l2_reg_embedding=l2_reg_embedding, init_std=init_std, seed=seed, task=task,
47 device=device, gpus=gpus)
48
49 self.use_fm = use_fm
50 self.use_dnn = len(dnn_feature_columns) > 0 and len(
51 dnn_hidden_units) > 0
52 if use_fm:
53 self.fm = FM()
54
55 if self.use_dnn:
56 self.dnn = DNN(self.compute_input_dim(dnn_feature_columns), dnn_hidden_units,
57 activation=dnn_activation, l2_reg=l2_reg_dnn, dropout_rate=dnn_dropout, use_bn=dnn_use_bn,
58 init_std=init_std, device=device)
59 self.dnn_linear = nn.Linear(
60 dnn_hidden_units[-1], 1, bias=False).to(device)
61
62 self.add_regularization_weight(
63 filter(lambda x: 'weight' in x[0] and 'bn' not in x[0], self.dnn.named_parameters()), l2=l2_reg_dnn)
64 self.add_regularization_weight(self.dnn_linear.weight, l2=l2_reg_dnn)
65 self.to(device)
66
67 def forward(self, X):
68
69 sparse_embedding_list, dense_value_list = self.input_from_feature_columns(X, self.dnn_feature_columns,
70 self.embedding_dict)
71 logit = self.linear_model(X)
72
73 if self.use_fm and len(sparse_embedding_list) > 0:

Calls

no outgoing calls

Tested by 2

test_DeepFMFunction · 0.72