Package: rec-pangu
Rec PanGu1.开源定位使用pytorch对经典的rank/多任务模型进行实现，并且对外提供统一调用的API接口，极大的降低了使用Rank/多任务模型的时间成本该项目使用了pytorch来实现我们的各种模型，以便于初学推荐系统的人可以更好的理解算法的核心思想由于已经有了很多类似的优秀的开源，我们这里对那些十分通用的模块参考了已有的开源，十分感谢这些开源贡献者的贡献2.安装#最新版gitclonehttps://github.com/HaSai666/rec_pangu.gitcdrec_pangu
pipinstall-e.--verbose#稳定版pipinstallrec_pangu--upgrade3.Rank模型模型论文年份WDLWide & Deep Learning for Recommender Systems2016DeepFMDeepFM: A Factorization-Machine based Neural Network for CTR Prediction2017NFMNeural Factorization Machines for Sparse Predictive Analytics2017FiBiNetFiBiNET: Combining Feature Importance and Bilinear Feature Interaction for Click-Through Rate2019AFMAttentional Factorization Machines2017AutoIntAutoInt: Automatic Feature Interaction Learning via Self-Attentive Neural Networks2018CCPMA Convolutional Click Prediction Model2015LR/2019FM/2019xDeepFMxDeepFM: Combining Explicit and Implicit Feature Interactions for Recommender Systems2018DCNDeep & Cross Network for Ad Click Predictions2019MaskNetMaskNet: Introducing Feature-Wise Multiplication to CTR Ranking Models by Instance-Guided Mask20214.多任务模型模型论文年份MMOEModeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts2018ShareBottomModeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts2018ESSMEntire Space Multi-Task Model: An Effective Approach for Estimating Post-Click Conversion Rate2018OMOEModeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts2018MLMMOE//AITMModeling the Sequential Dependence among Audience Multi-step Conversions with Multi-task Learning in Targeted Display Advertising20195.序列召回模型目前支持如下类型的序列召回模型:经典序列召回模型基于图的序列召回模型多兴趣序列召回模型基于LLM的序列召回模型模型类型论文年份YotubeDNN经典序列召回Deep Neural Networks for YouTube Recommendations2016Gru4Rec经典序列召回Session-based recommendations with recurrent neural networks2015Narm经典序列召回Neural Attentive Session-based Recommendation2017NextItNet经典序列召回A Simple Convolutional Generative Network for Next Item2019ContraRec序列对比学习Sequential Recommendation with Multiple Contrast SignalsComirecSA多兴趣召回Controllable multi-interest framework for recommendation2020ComirecDR多兴趣召回Controllable multi-interest framework for recommendation2020Mind多兴趣召回Multi-Interest Network with Dynamic Routing for Recommendation at Tmall2019Re4多兴趣召回Re4: Learning to Re-contrast, Re-attend, Re-construct for Multi-interest Recommendation2022CMI多兴趣召回mproving Micro-video Recommendation via Contrastive Multiple Interests2022SRGNN图序列召回Session-based Recommendation with Graph Neural Networks2019GC-SAN图序列召回SGraph Contextualized Self-Attention Network for Session-based Recommendation2019NISER图序列召回NISER: Normalized Item and Session Representations to Handle Popularity Bias2019GCE-GNN(ToDo)图序列召回Global Context Enhanced Graph Neural Networks for Session-based Recommendation2020Recformer(ToDo)LLM序列召回Text Is All You Need: Learning Language Representations for Sequential Recommendation20236.图协同过滤模型模型类型论文年份NGCF(ToDo)图协同过滤Neural Graph Collaborative Filtering2019LightGCN(ToDo)图协同过滤LightGCN: Simplifying and Powering Graph Convolution Network for Recommendation2019NCL(ToDo)图对比学习Improving Graph Collaborative Filtering with Neighborhood-enriched Contrastive Learning2022SimGCL(ToDo)图对比学习Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for Recommendation2022SGL(ToDo)图对比学习Self-supervised Graph Learning for Recommendation20217.Demo我们的Rank和多任务模型所对外暴露的接口十分相似,同时我们这里也支持使用wandb来实时监测模型训练指标,我们下面会分别给出Rank,多任务模型,wandb的demo7.1 排序任务Demo# 声明数据schemaimporttorchfromrec_pangu.datasetimportget_dataloaderfromrec_pangu.models.rankingimportWDL,DeepFM,NFM,FiBiNet,AFM,AFN,AOANet,AutoInt,CCPM,LR,FM,xDeepFMfromrec_pangu.trainerimportRankTrainerimportpandasaspdif__name__=='__main__':df=pd.read_csv('sample_data/ranking_sample_data.csv')print(df.head())# 声明数据schemaschema={"sparse_cols":['user_id','item_id','item_type','dayofweek','is_workday','city','county','town','village','lbs_city','lbs_district','hardware_platform','hardware_ischarging','os_type','network_type','position'],"dense_cols":['item_expo_1d','item_expo_7d','item_expo_14d','item_expo_30d','item_clk_1d','item_clk_7d','item_clk_14d','item_clk_30d','use_duration'],"label_col":'click',}# 准备数据,这里只选择了100条数据,所以没有切分数据集train_df=dfvalid_df=dftest_df=df# 声明使用的devicedevice=torch.device('cpu')# 获取dataloadertrain_loader,valid_loader,test_loader,enc_dict=get_dataloader(train_df,valid_df,test_df,schema)# 声明模型,排序模型目前支持：WDL, DeepFM, NFM, FiBiNet, AFM, AFN, AOANet, AutoInt, CCPM, LR, FM, xDeepFMmodel=xDeepFM(enc_dict=enc_dict)# 声明Trainertrainer=RankTrainer(num_task=1)# 训练模型trainer.fit(model,train_loader,valid_loader,epoch=5,lr=1e-3,device=device)# 保存模型权重trainer.save_model(model,'./model_ckpt')# 模型验证test_metric=trainer.evaluate_model(model,test_loader,device=device)print('Test metric:{}'.format(test_metric))7.2 多任务模型Demoimporttorchfromrec_pangu.datasetimportget_dataloaderfromrec_pangu.models.multi_taskimportAITM,ShareBottom,ESSM,MMOE,OMOE,MLMMOEfromrec_pangu.trainerimportRankTrainerimportpandasaspdif__name__=='__main__':df=pd.read_csv('sample_data/multi_task_sample_data.csv')print(df.head())#声明数据schemaschema={"sparse_cols":['user_id','item_id','item_type','dayofweek','is_workday','city','county','town','village','lbs_city','lbs_district','hardware_platform','hardware_ischarging','os_type','network_type','position'],"dense_cols":['item_expo_1d','item_expo_7d','item_expo_14d','item_expo_30d','item_clk_1d','item_clk_7d','item_clk_14d','item_clk_30d','use_duration'],"label_col":['click','scroll'],}#准备数据,这里只选择了100条数据,所以没有切分数据集train_df=dfvalid_df=dftest_df=df#声明使用的devicedevice=torch.device('cpu')#获取dataloadertrain_loader,valid_loader,test_loader,enc_dict=get_dataloader(train_df,valid_df,test_df,schema)#声明模型,多任务模型目前支持：AITM,ShareBottom,ESSM,MMOE,OMOE,MLMMOEmodel=AITM(enc_dict=enc_dict)#声明Trainertrainer=RankTrainer(num_task=2)#训练模型trainer.fit(model,train_loader,valid_loader,epoch=5,lr=1e-3,device=device)#保存模型权重trainer.save_model(model,'./model_ckpt')#模型验证test_metric=trainer.evaluate_model(model,test_loader,device=device)print('Test metric:{}'.format(test_metric))7.3 序列召回Demoimporttorchfromrec_pangu.datasetimportget_dataloaderfromrec_pangu.models.sequenceimportComirecSA,ComirecDR,MIND,CMI,Re4,NARM,YotubeDNN,SRGNNfromrec_pangu.trainerimportSequenceTrainerfromrec_pangu.utilsimportset_deviceimportpandasaspdif__name__=='__main__':#声明数据schemaschema={'user_col':'user_id','item_col':'item_id','cate_cols':['genre'],'max_length':20,'time_col':'timestamp','task_type':'sequence'}# 模型配置config={'embedding_dim':64,'lr':0.001,'K':1,'device':-1,}config['device']=set_device(config['device'])config.update(schema)#样例数据train_df=pd.read_csv('./sample_data/sample_train.csv')valid_df=pd.read_csv('./sample_data/sample_valid.csv')test_df=pd.read_csv('./sample_data/sample_test.csv')#声明使用的devicedevice=torch.device('cpu')#获取dataloadertrain_loader,valid_loader,test_loader,enc_dict=get_dataloader(train_df,valid_df,test_df,schema,batch_size=50)#声明模型,序列召回模型模型目前支持： ComirecSA,ComirecDR,MIND,CMI,Re4,NARM,YotubeDNN,SRGNNmodel=ComirecSA(enc_dict=enc_dict,config=config)#声明Trainertrainer=SequenceTrainer(model_ckpt_dir='./model_ckpt')#训练模型trainer.fit(model,train_loader,valid_loader,epoch=500,lr=1e-3,device=device,log_rounds=10,use_earlystoping=True,max_patience=5,monitor_metric='recall@20',)#保存模型权重和enc_dicttrainer.save_all(model,enc_dict,'./model_ckpt')#模型验证test_metric=trainer.evaluate_model(model,test_loader,device=device)7.4 图协调过滤DemoTODO