Coverage for hopwise/model/context_aware_recommender/widedeep.py: 100%
35 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-09-30 13:25 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-09-30 13:25 +0000
1# @Time : 2020/08/30
2# @Author : Xinyan Fan
3# @Email : xinyan.fan@ruc.edu.cn
4# @File : widedeep.py
6r"""WideDeep
7#####################################################
8Reference:
9 Heng-Tze Cheng et al. "Wide & Deep Learning for Recommender Systems." in RecSys 2016.
10"""
12from torch import nn
13from torch.nn.init import constant_, xavier_normal_
15from hopwise.model.abstract_recommender import ContextRecommender
16from hopwise.model.layers import MLPLayers
19class WideDeep(ContextRecommender):
20 r"""WideDeep is a context-based recommendation model.
21 It jointly trains wide linear models and deep neural networks to combine the benefits
22 of memorization and generalization for recommender systems. The wide component is a generalized linear model
23 of the form :math:`y = w^Tx + b`. The deep component is a feed-forward neural network. The wide component
24 and deep component are combined using a weighted sum of their output log odds as the prediction,
25 which is then fed to one common logistic loss function for joint training.
26 """
28 def __init__(self, config, dataset):
29 super().__init__(config, dataset)
31 # load parameters info
32 self.mlp_hidden_size = config["mlp_hidden_size"]
33 self.dropout_prob = config["dropout_prob"]
35 # define layers and loss
36 size_list = [self.embedding_size * self.num_feature_field] + self.mlp_hidden_size
37 self.mlp_layers = MLPLayers(size_list, self.dropout_prob)
38 self.deep_predict_layer = nn.Linear(self.mlp_hidden_size[-1], 1)
39 self.sigmoid = nn.Sigmoid()
40 self.loss = nn.BCEWithLogitsLoss()
42 # parameters initialization
43 self.apply(self._init_weights)
45 def _init_weights(self, module):
46 if isinstance(module, nn.Embedding):
47 xavier_normal_(module.weight.data)
48 elif isinstance(module, nn.Linear):
49 xavier_normal_(module.weight.data)
50 if module.bias is not None:
51 constant_(module.bias.data, 0)
53 def forward(self, interaction):
54 widedeep_all_embeddings = self.concat_embed_input_fields(interaction) # [batch_size, num_field, embed_dim]
55 batch_size = widedeep_all_embeddings.shape[0]
56 fm_output = self.first_order_linear(interaction)
58 deep_output = self.deep_predict_layer(self.mlp_layers(widedeep_all_embeddings.view(batch_size, -1)))
59 output = fm_output + deep_output
60 return output.squeeze(-1)
62 def calculate_loss(self, interaction):
63 label = interaction[self.LABEL]
64 output = self.forward(interaction)
65 return self.loss(output, label)
67 def predict(self, interaction):
68 return self.sigmoid(self.forward(interaction))