Instantiates the Product-based Neural Network architecture. :param dnn_feature_columns: An iterable containing all the features used by deep part of the model. :param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of deep net :par
| 15 | |
| 16 | |
| 17 | class PNN(BaseModel): |
| 18 | """Instantiates the Product-based Neural Network architecture. |
| 19 | |
| 20 | :param dnn_feature_columns: An iterable containing all the features used by deep part of the model. |
| 21 | :param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of deep net |
| 22 | :param l2_reg_embedding: float . L2 regularizer strength applied to embedding vector |
| 23 | :param l2_reg_dnn: float. L2 regularizer strength applied to DNN |
| 24 | :param init_std: float,to use as the initialize std of embedding vector |
| 25 | :param seed: integer ,to use as random seed. |
| 26 | :param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate. |
| 27 | :param dnn_activation: Activation function to use in DNN |
| 28 | :param use_inner: bool,whether use inner-product or not. |
| 29 | :param use_outter: bool,whether use outter-product or not. |
| 30 | :param kernel_type: str,kernel_type used in outter-product,can be ``'mat'`` , ``'vec'`` or ``'num'`` |
| 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, dnn_feature_columns, dnn_hidden_units=(128, 128), l2_reg_embedding=1e-5, l2_reg_dnn=0, |
| 39 | init_std=0.0001, seed=1024, dnn_dropout=0, dnn_activation='relu', use_inner=True, use_outter=False, |
| 40 | kernel_type='mat', task='binary', device='cpu', gpus=None): |
| 41 | |
| 42 | super(PNN, self).__init__([], dnn_feature_columns, l2_reg_linear=0, l2_reg_embedding=l2_reg_embedding, |
| 43 | init_std=init_std, seed=seed, task=task, device=device, gpus=gpus) |
| 44 | |
| 45 | if kernel_type not in ['mat', 'vec', 'num']: |
| 46 | raise ValueError("kernel_type must be mat,vec or num") |
| 47 | |
| 48 | self.use_inner = use_inner |
| 49 | self.use_outter = use_outter |
| 50 | self.kernel_type = kernel_type |
| 51 | self.task = task |
| 52 | |
| 53 | product_out_dim = 0 |
| 54 | num_inputs = self.compute_input_dim(dnn_feature_columns, include_dense=False, feature_group=True) |
| 55 | num_pairs = int(num_inputs * (num_inputs - 1) / 2) |
| 56 | |
| 57 | if self.use_inner: |
| 58 | product_out_dim += num_pairs |
| 59 | self.innerproduct = InnerProductLayer(device=device) |
| 60 | |
| 61 | if self.use_outter: |
| 62 | product_out_dim += num_pairs |
| 63 | self.outterproduct = OutterProductLayer( |
| 64 | num_inputs, self.embedding_size, kernel_type=kernel_type, device=device) |
| 65 | |
| 66 | self.dnn = DNN(product_out_dim + self.compute_input_dim(dnn_feature_columns), dnn_hidden_units, |
| 67 | activation=dnn_activation, l2_reg=l2_reg_dnn, dropout_rate=dnn_dropout, use_bn=False, |
| 68 | init_std=init_std, device=device) |
| 69 | |
| 70 | self.dnn_linear = nn.Linear( |
| 71 | dnn_hidden_units[-1], 1, bias=False).to(device) |
| 72 | self.add_regularization_weight( |
| 73 | filter(lambda x: 'weight' in x[0] and 'bn' not in x[0], self.dnn.named_parameters()), l2=l2_reg_dnn) |
| 74 | self.add_regularization_weight(self.dnn_linear.weight, l2=l2_reg_dnn) |