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

Class AFN

deepctr_torch/models/afn.py:17–74  ·  view source on GitHub ↗

Instantiates the Adaptive Factorization Network architecture. In DeepCTR-Torch, we only provide the non-ensembled version of AFN for the consistency of model interfaces. For the ensembled version of AFN+, please refer to https://github.com/WeiyuCheng/DeepCTR-Torch (Pytorch Version) or https

Source from the content-addressed store, hash-verified

15
16
17class AFN(BaseModel):
18 """Instantiates the Adaptive Factorization Network architecture.
19
20 In DeepCTR-Torch, we only provide the non-ensembled version of AFN for the consistency of model interfaces. For the ensembled version of AFN+, please refer to https://github.com/WeiyuCheng/DeepCTR-Torch (Pytorch Version) or https://github.com/WeiyuCheng/AFN-AAAI-20 (Tensorflow Version).
21
22 :param linear_feature_columns: An iterable containing all the features used by linear part of the model.
23 :param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
24 :param ltl_hidden_size: integer, the number of logarithmic neurons in AFN
25 :param afn_dnn_hidden_units: list, list of positive integer or empty list, the layer number and units in each layer of DNN layers in AFN
26 :param l2_reg_linear: float. L2 regularizer strength applied to linear part
27 :param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
28 :param l2_reg_dnn: float. L2 regularizer strength applied to DNN
29 :param init_std: float,to use as the initialize std of embedding vector
30 :param seed: integer ,to use as random seed.
31 :param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
32 :param dnn_activation: Activation function to use in DNN
33 :param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
34 :param device: str, ``"cpu"`` or ``"cuda:0"``
35 :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`.
36 :return: A PyTorch model instance.
37
38 """
39
40 def __init__(self,
41 linear_feature_columns, dnn_feature_columns,
42 ltl_hidden_size=256, afn_dnn_hidden_units=(256, 128),
43 l2_reg_linear=0.00001, l2_reg_embedding=0.00001, l2_reg_dnn=0,
44 init_std=0.0001, seed=1024, dnn_dropout=0, dnn_activation='relu',
45 task='binary', device='cpu', gpus=None):
46
47 super(AFN, self).__init__(linear_feature_columns, dnn_feature_columns, l2_reg_linear=l2_reg_linear,
48 l2_reg_embedding=l2_reg_embedding, init_std=init_std, seed=seed, task=task,
49 device=device, gpus=gpus)
50
51 self.ltl = LogTransformLayer(len(self.embedding_dict), self.embedding_size, ltl_hidden_size)
52 self.afn_dnn = DNN(self.embedding_size * ltl_hidden_size, afn_dnn_hidden_units,
53 activation=dnn_activation, l2_reg=l2_reg_dnn, dropout_rate=dnn_dropout, use_bn=True,
54 init_std=init_std, device=device)
55 self.afn_dnn_linear = nn.Linear(afn_dnn_hidden_units[-1], 1)
56 self.to(device)
57
58 def forward(self, X):
59
60 sparse_embedding_list, _ = self.input_from_feature_columns(X, self.dnn_feature_columns,
61 self.embedding_dict)
62 logit = self.linear_model(X)
63 if len(sparse_embedding_list) == 0:
64 raise ValueError('Sparse embeddings not provided. AFN only accepts sparse embeddings as input.')
65
66 afn_input = torch.cat(sparse_embedding_list, dim=1)
67 ltl_result = self.ltl(afn_input)
68 afn_logit = self.afn_dnn(ltl_result)
69 afn_logit = self.afn_dnn_linear(afn_logit)
70
71 logit += afn_logit
72 y_pred = self.out(logit)
73
74 return y_pred

Callers 1

test_AFNFunction · 0.90

Calls

no outgoing calls

Tested by 1

test_AFNFunction · 0.72