Source code for recbole.model.context_aware_recommender.widedeep
# -*- coding: utf-8 -*-# @Time : 2020/08/30# @Author : Xinyan Fan# @Email : xinyan.fan@ruc.edu.cn# @File : widedeep.pyr"""WideDeep#####################################################Reference: Heng-Tze Cheng et al. "Wide & Deep Learning for Recommender Systems." in RecSys 2016."""importtorch.nnasnnfromtorch.nn.initimportxavier_normal_,constant_fromrecbole.model.abstract_recommenderimportContextRecommenderfromrecbole.model.layersimportMLPLayers
[docs]classWideDeep(ContextRecommender):r"""WideDeep is a context-based recommendation model. It jointly trains wide linear models and deep neural networks to combine the benefits of memorization and generalization for recommender systems. The wide component is a generalized linear model of the form :math:`y = w^Tx + b`. The deep component is a feed-forward neural network. The wide component and deep component are combined using a weighted sum of their output log odds as the prediction, which is then fed to one common logistic loss function for joint training. """def__init__(self,config,dataset):super(WideDeep,self).__init__(config,dataset)# load parameters infoself.mlp_hidden_size=config["mlp_hidden_size"]self.dropout_prob=config["dropout_prob"]# define layers and losssize_list=[self.embedding_size*self.num_feature_field]+self.mlp_hidden_sizeself.mlp_layers=MLPLayers(size_list,self.dropout_prob)self.deep_predict_layer=nn.Linear(self.mlp_hidden_size[-1],1)self.sigmoid=nn.Sigmoid()self.loss=nn.BCEWithLogitsLoss()# parameters initializationself.apply(self._init_weights)def_init_weights(self,module):ifisinstance(module,nn.Embedding):xavier_normal_(module.weight.data)elifisinstance(module,nn.Linear):xavier_normal_(module.weight.data)ifmodule.biasisnotNone:constant_(module.bias.data,0)