{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-20T01:14:10.410803Z","iopub.execute_input":"2022-10-20T01:14:10.411265Z","iopub.status.idle":"2022-10-20T01:14:10.421055Z","shell.execute_reply.started":"2022-10-20T01:14:10.411223Z","shell.execute_reply":"2022-10-20T01:14:10.420138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# rec_pangu 这个工具包是我自己开发的，求大家支持一下！求三连！ 链接在这里：https://github.com/HaSai666/rec_pangu 谢谢各位啦～\n# 这个工具包是主要针对推荐系统中的排序算法和多任务建模算法的使用Pytorch开发的一个工具包，内置了20余种模型，内含不少例子，可以让大家很快的进行相关模型的测试，在11～1月这两个月会陆续更新一些GNN的模型，感兴趣的话也可以来和我一起开发～","metadata":{}},{"cell_type":"code","source":"!pip install rec_pangu","metadata":{"execution":{"iopub.status.busy":"2022-10-20T01:14:10.632466Z","iopub.execute_input":"2022-10-20T01:14:10.632911Z","iopub.status.idle":"2022-10-20T01:14:24.126525Z","shell.execute_reply.started":"2022-10-20T01:14:10.632869Z","shell.execute_reply":"2022-10-20T01:14:24.125362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom rec_pangu.dataset import get_dataloader,get_single_dataloader\nfrom rec_pangu.models.ranking import WDL, DeepFM, NFM, FiBiNet, AFM, AFN, AOANet, AutoInt, CCPM, LR, FM, xDeepFM, DCN\nfrom rec_pangu.trainer import RankTraniner\nimport pandas as pd\nfrom sklearn.model_selection import StratifiedKFold, KFold\nfrom sklearn.metrics import roc_auc_score, log_loss\nfrom tqdm.notebook import tqdm\nimport numpy as np\nimport os","metadata":{"execution":{"iopub.status.busy":"2022-10-20T01:14:24.128919Z","iopub.execute_input":"2022-10-20T01:14:24.129599Z","iopub.status.idle":"2022-10-20T01:14:26.168633Z","shell.execute_reply.started":"2022-10-20T01:14:24.129555Z","shell.execute_reply":"2022-10-20T01:14:26.167687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/deepshare-playground/train_behaviour.csv')\ntest_df = pd.read_csv('/kaggle/input/deepshare-playground/test_behaviour.csv')\nuser_df = pd.read_csv('/kaggle/input/deepshare-playground/user_info.csv')\nitem_df = pd.read_csv('/kaggle/input/deepshare-playground/item_info.csv')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T01:14:26.170006Z","iopub.execute_input":"2022-10-20T01:14:26.170562Z","iopub.status.idle":"2022-10-20T01:14:29.892097Z","shell.execute_reply.started":"2022-10-20T01:14:26.170532Z","shell.execute_reply":"2022-10-20T01:14:29.891127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item_df = item_df.drop_duplicates(subset=['item_id'], keep='last', inplace=False)\nuser_df = user_df.drop_duplicates(subset=['user_id'], keep='last', inplace=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T01:14:29.894567Z","iopub.execute_input":"2022-10-20T01:14:29.894911Z","iopub.status.idle":"2022-10-20T01:14:29.921410Z","shell.execute_reply.started":"2022-10-20T01:14:29.894877Z","shell.execute_reply":"2022-10-20T01:14:29.920553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.concat([train_df,test_df],axis=0).reset_index(drop=True)\ndf = df.merge(user_df,on='user_id',how='left').reset_index(drop=True)\ndf = df.merge(item_df,on='item_id',how='left').reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T01:14:29.922941Z","iopub.execute_input":"2022-10-20T01:14:29.923361Z","iopub.status.idle":"2022-10-20T01:14:33.511271Z","shell.execute_reply.started":"2022-10-20T01:14:29.923325Z","shell.execute_reply":"2022-10-20T01:14:33.510233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['user_fea4'] = df['user_fea4'].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T01:14:33.512652Z","iopub.execute_input":"2022-10-20T01:14:33.513559Z","iopub.status.idle":"2022-10-20T01:14:33.552952Z","shell.execute_reply.started":"2022-10-20T01:14:33.513513Z","shell.execute_reply":"2022-10-20T01:14:33.552054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info(verbose=True,null_counts=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T01:14:33.554461Z","iopub.execute_input":"2022-10-20T01:14:33.554840Z","iopub.status.idle":"2022-10-20T01:14:33.939543Z","shell.execute_reply.started":"2022-10-20T01:14:33.554804Z","shell.execute_reply":"2022-10-20T01:14:33.938588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"schema={\n    \"sparse_cols\": ['user_id','item_id']+[f'user_fea{i}' for i in range(1,4)] + [f'item_fea{i}' for i in range(1,10)],\n    \"dense_cols\": ['user_fea4'],\n    \"label_col\":'label',\n}","metadata":{"execution":{"iopub.status.busy":"2022-10-20T01:14:33.941094Z","iopub.execute_input":"2022-10-20T01:14:33.941678Z","iopub.status.idle":"2022-10-20T01:14:33.947595Z","shell.execute_reply.started":"2022-10-20T01:14:33.941639Z","shell.execute_reply":"2022-10-20T01:14:33.946383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = df[df['label'].notna()].reset_index(drop=True)\ntest_df = df[df['label'].isna()].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T01:14:33.949111Z","iopub.execute_input":"2022-10-20T01:14:33.949473Z","iopub.status.idle":"2022-10-20T01:14:34.688691Z","shell.execute_reply.started":"2022-10-20T01:14:33.949437Z","shell.execute_reply":"2022-10-20T01:14:34.687701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_enc_dict(train_df,test_df,schema):\n    #计算enc_dict\n    enc_dict = dict(zip( list(schema['dense_cols']+schema['sparse_cols']),[dict() for _ in range(len(schema['dense_cols']+schema['sparse_cols']))]))\n    for f in tqdm(schema['sparse_cols']):\n        all_value = list(set( list(train_df[f].unique())+list(test_df[f].unique())) )\n        map_dict = dict(zip(sorted(all_value), range(1, len(all_value)+1)))\n        enc_dict[f] = map_dict\n        enc_dict[f]['vocab_size'] = len(all_value) +1\n\n    for f in tqdm(schema['dense_cols']):\n        enc_dict[f]['min'] = min(train_df[f].min(), test_df[f].min())\n        enc_dict[f]['max'] = max(train_df[f].max(), test_df[f].max())\n\n    return enc_dict","metadata":{"execution":{"iopub.status.busy":"2022-10-20T01:15:23.797509Z","iopub.execute_input":"2022-10-20T01:15:23.797930Z","iopub.status.idle":"2022-10-20T01:15:23.808890Z","shell.execute_reply.started":"2022-10-20T01:15:23.797895Z","shell.execute_reply":"2022-10-20T01:15:23.807880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"enc_dict = get_enc_dict(train_df,test_df,schema)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T01:15:24.306254Z","iopub.execute_input":"2022-10-20T01:15:24.306597Z","iopub.status.idle":"2022-10-20T01:15:25.478260Z","shell.execute_reply.started":"2022-10-20T01:15:24.306566Z","shell.execute_reply":"2022-10-20T01:15:25.477327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#声明使用的device\nif torch.cuda.is_available():\n    device = torch.device('cuda')  \nelse: \n    device = torch.device('cpu')\nbatch_size = 4096\nmodel_ckpt_dir = '/kaggle/working/pangu_model_ckpt/'\nos.makedirs(model_ckpt_dir, mode=0o777, exist_ok=True)\ntrain_df['label'] = train_df['label'].astype('int32')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T01:15:25.865302Z","iopub.execute_input":"2022-10-20T01:15:25.865890Z","iopub.status.idle":"2022-10-20T01:15:25.957181Z","shell.execute_reply.started":"2022-10-20T01:15:25.865860Z","shell.execute_reply":"2022-10-20T01:15:25.956112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params = {\n    'embedding_dim' : 16,\n    'hidden_units' : [256, 128, 64],\n}\nn_fold = 5\noof_pre = np.zeros(len(train_df))\ny_pre = np.zeros(len(test_df))\n\nkf = KFold(n_splits=n_fold)\nfor fold_, (trn_idx, val_idx) in enumerate(kf.split(train_df)):\n    temp_train_df = train_df.iloc[trn_idx].reset_index(drop=True)\n    temp_valid_df = train_df.iloc[val_idx].reset_index(drop=True)\n    \n    train_loader = get_single_dataloader(temp_train_df, schema, enc_dict, batch_size=batch_size, num_workers=2)\n    valid_loader = get_single_dataloader(temp_valid_df, schema, enc_dict, batch_size=batch_size)\n    \n    #声明模型,排序模型目前支持：WDL, DeepFM, NFM, FiBiNet, AFM, AFN, AOANet, AutoInt, CCPM, LR, FM, xDeepFM\n    model = DeepFM(enc_dict=enc_dict,**params)\n    #声明Trainer\n    trainer = RankTraniner(num_task=1)\n    #训练模型\n    trainer.fit(model, train_loader, valid_loader, epoch=3, lr=3e-4, device=device)\n    #保存模型权重和enc_dict\n    save_dict = {'model': model.state_dict(),\n                'enc_dict': enc_dict}\n    torch.save(save_dict, os.path.join(model_ckpt_dir, f'model_{fold_}.pth'))\n    # oof推理\n    oof_pre[val_idx] = trainer.predict_dataframe(model, temp_valid_df, enc_dict, schema, device=device)\n    # 测试集推理\n    y_pre = trainer.predict_dataframe(model, test_df, enc_dict, schema, device=device) \n    \n    break\n    ","metadata":{"execution":{"iopub.status.busy":"2022-10-19T13:12:24.856010Z","iopub.execute_input":"2022-10-19T13:12:24.856374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"local_auc = roc_auc_score(train_df['label'].iloc[val_idx], oof_pre[val_idx])\nprint(f'Local AUC:{local_auc}')","metadata":{"execution":{"iopub.status.busy":"2022-10-19T13:12:22.214020Z","iopub.status.idle":"2022-10-19T13:12:22.214689Z","shell.execute_reply.started":"2022-10-19T13:12:22.214418Z","shell.execute_reply":"2022-10-19T13:12:22.214461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_df = pd.DataFrame()\nres_df['ID'] = test_df['ID']\nres_df['predict'] = y_pre\nres_df.to_csv('/kaggle/working/lgb-baseline.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-19T13:12:22.216595Z","iopub.status.idle":"2022-10-19T13:12:22.217512Z","shell.execute_reply.started":"2022-10-19T13:12:22.217248Z","shell.execute_reply":"2022-10-19T13:12:22.217275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import rec_pangu\nrec_pangu.__version__","metadata":{"execution":{"iopub.status.busy":"2022-10-14T13:35:17.346697Z","iopub.execute_input":"2022-10-14T13:35:17.347915Z","iopub.status.idle":"2022-10-14T13:35:17.353912Z","shell.execute_reply.started":"2022-10-14T13:35:17.347868Z","shell.execute_reply":"2022-10-14T13:35:17.353114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2022-10-19T12:35:17.681844Z","iopub.execute_input":"2022-10-19T12:35:17.682376Z","iopub.status.idle":"2022-10-19T12:35:17.696126Z","shell.execute_reply.started":"2022-10-19T12:35:17.682330Z","shell.execute_reply":"2022-10-19T12:35:17.694211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}