Instantiates the DCN-Mix model. :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 cross_num: positive integet,cross layer num
| 18 | |
| 19 | |
| 20 | class DCNMix(BaseModel): |
| 21 | """Instantiates the DCN-Mix model. |
| 22 | |
| 23 | :param linear_feature_columns: An iterable containing all the features used by linear part of the model. |
| 24 | :param dnn_feature_columns: An iterable containing all the features used by deep part of the model. |
| 25 | :param cross_num: positive integet,cross layer number |
| 26 | :param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of DNN |
| 27 | :param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector |
| 28 | :param l2_reg_cross: float. L2 regularizer strength applied to cross net |
| 29 | :param l2_reg_dnn: float. L2 regularizer strength applied to DNN |
| 30 | :param init_std: float,to use as the initialize std of embedding vector |
| 31 | :param seed: integer ,to use as random seed. |
| 32 | :param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate. |
| 33 | :param dnn_use_bn: bool. Whether use BatchNormalization before activation or not DNN |
| 34 | :param dnn_activation: Activation function to use in DNN |
| 35 | :param low_rank: Positive integer, dimensionality of low-rank sapce. |
| 36 | :param num_experts: Positive integer, number of experts. |
| 37 | :param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss |
| 38 | :param device: str, ``"cpu"`` or ``"cuda:0"`` |
| 39 | :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`. |
| 40 | :return: A PyTorch model instance. |
| 41 | |
| 42 | """ |
| 43 | |
| 44 | def __init__(self, linear_feature_columns, |
| 45 | dnn_feature_columns, cross_num=2, |
| 46 | dnn_hidden_units=(128, 128), l2_reg_linear=0.00001, |
| 47 | l2_reg_embedding=0.00001, l2_reg_cross=0.00001, l2_reg_dnn=0, init_std=0.0001, seed=1024, |
| 48 | dnn_dropout=0, low_rank=32, num_experts=4, |
| 49 | dnn_activation='relu', dnn_use_bn=False, task='binary', device='cpu', gpus=None): |
| 50 | |
| 51 | super(DCNMix, self).__init__(linear_feature_columns=linear_feature_columns, |
| 52 | dnn_feature_columns=dnn_feature_columns, l2_reg_embedding=l2_reg_embedding, |
| 53 | init_std=init_std, seed=seed, task=task, device=device, gpus=gpus) |
| 54 | self.dnn_hidden_units = dnn_hidden_units |
| 55 | self.cross_num = cross_num |
| 56 | self.dnn = DNN(self.compute_input_dim(dnn_feature_columns), dnn_hidden_units, |
| 57 | activation=dnn_activation, use_bn=dnn_use_bn, l2_reg=l2_reg_dnn, dropout_rate=dnn_dropout, |
| 58 | init_std=init_std, device=device) |
| 59 | if len(self.dnn_hidden_units) > 0 and self.cross_num > 0: |
| 60 | dnn_linear_in_feature = self.compute_input_dim(dnn_feature_columns) + dnn_hidden_units[-1] |
| 61 | elif len(self.dnn_hidden_units) > 0: |
| 62 | dnn_linear_in_feature = dnn_hidden_units[-1] |
| 63 | elif self.cross_num > 0: |
| 64 | dnn_linear_in_feature = self.compute_input_dim(dnn_feature_columns) |
| 65 | |
| 66 | self.dnn_linear = nn.Linear(dnn_linear_in_feature, 1, bias=False).to( |
| 67 | device) |
| 68 | self.crossnet = CrossNetMix(in_features=self.compute_input_dim(dnn_feature_columns), |
| 69 | low_rank=low_rank, num_experts=num_experts, |
| 70 | layer_num=cross_num, device=device) |
| 71 | self.add_regularization_weight( |
| 72 | filter(lambda x: 'weight' in x[0] and 'bn' not in x[0], self.dnn.named_parameters()), l2=l2_reg_dnn) |
| 73 | self.add_regularization_weight(self.dnn_linear.weight, l2=l2_reg_linear) |
| 74 | regularization_modules = [self.crossnet.U_list, self.crossnet.V_list, self.crossnet.C_list] |
| 75 | for module in regularization_modules: |
| 76 | self.add_regularization_weight(module, l2=l2_reg_cross) |
| 77 |