{"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":"2023-02-23T06:57:26.300480Z","iopub.execute_input":"2023-02-23T06:57:26.300963Z","iopub.status.idle":"2023-02-23T06:57:26.344052Z","shell.execute_reply.started":"2023-02-23T06:57:26.300866Z","shell.execute_reply":"2023-02-23T06:57:26.342961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install rec_pangu","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:57:28.967876Z","iopub.execute_input":"2023-02-23T06:57:28.968578Z","iopub.status.idle":"2023-02-23T06:57:42.271834Z","shell.execute_reply.started":"2023-02-23T06:57:28.968541Z","shell.execute_reply":"2023-02-23T06:57:42.270534Z"},"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":"2023-02-23T06:57:42.274803Z","iopub.execute_input":"2023-02-23T06:57:42.275237Z","iopub.status.idle":"2023-02-23T06:57:45.211723Z","shell.execute_reply.started":"2023-02-23T06:57:42.275188Z","shell.execute_reply":"2023-02-23T06:57:45.210358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/deepshare-playground-series2/train_data.csv')\ntest_df = pd.read_csv('/kaggle/input/deepshare-playground-series2/test_data.csv')\n\n# label转成int32\ntrain_df['label'] = train_df['label'].astype('int32')","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:57:45.213453Z","iopub.execute_input":"2023-02-23T06:57:45.213881Z","iopub.status.idle":"2023-02-23T06:58:18.002820Z","shell.execute_reply.started":"2023-02-23T06:57:45.213839Z","shell.execute_reply":"2023-02-23T06:58:18.001813Z"},"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,6)] + ['item_fea1']+['user_fea9'],\n    \"dense_cols\": [f'user_fea{i}' for i in range(6,9)] ,\n    \"label_col\":'label',\n}","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:58:18.005557Z","iopub.execute_input":"2023-02-23T06:58:18.005846Z","iopub.status.idle":"2023-02-23T06:58:18.015275Z","shell.execute_reply.started":"2023-02-23T06:58:18.005819Z","shell.execute_reply":"2023-02-23T06:58:18.014369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in schema['sparse_cols']:\n    train_df[col] = train_df[col].fillna('-1').astype('str')\n    test_df[col] = test_df[col].fillna('-1').astype('str')\nfor col in schema['dense_cols']:\n    train_df[col] = train_df[col].fillna(0)\n    test_df[col] = test_df[col].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:58:18.018056Z","iopub.execute_input":"2023-02-23T06:58:18.018818Z","iopub.status.idle":"2023-02-23T06:58:26.219976Z","shell.execute_reply.started":"2023-02-23T06:58:18.018783Z","shell.execute_reply":"2023-02-23T06:58:26.218941Z"},"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":"2023-02-23T06:58:26.221283Z","iopub.execute_input":"2023-02-23T06:58:26.221650Z","iopub.status.idle":"2023-02-23T06:58:26.230887Z","shell.execute_reply.started":"2023-02-23T06:58:26.221616Z","shell.execute_reply":"2023-02-23T06:58:26.229764Z"},"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":"2023-02-23T06:58:26.232607Z","iopub.execute_input":"2023-02-23T06:58:26.233439Z","iopub.status.idle":"2023-02-23T06:58:30.931773Z","shell.execute_reply.started":"2023-02-23T06:58:26.233392Z","shell.execute_reply":"2023-02-23T06:58:30.930635Z"},"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')\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":"2023-02-23T07:00:08.342444Z","iopub.execute_input":"2023-02-23T07:00:08.343529Z","iopub.status.idle":"2023-02-23T07:00:08.361628Z","shell.execute_reply.started":"2023-02-23T07:00:08.343476Z","shell.execute_reply":"2023-02-23T07:00:08.360579Z"},"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 += np.array(trainer.predict_dataframe(model, test_df, enc_dict, schema, device=device)) / n_fold\n    ","metadata":{"execution":{"iopub.status.busy":"2023-02-23T07:27:09.559527Z","iopub.execute_input":"2023-02-23T07:27:09.559904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"local_auc = roc_auc_score(train_df['label'], oof_pre)\nprint(f'Local AUC:{local_auc}')","metadata":{"execution":{"iopub.status.busy":"2023-02-23T07:19:29.260546Z","iopub.execute_input":"2023-02-23T07:19:29.260906Z","iopub.status.idle":"2023-02-23T07:19:29.445953Z","shell.execute_reply.started":"2023-02-23T07:19:29.260870Z","shell.execute_reply":"2023-02-23T07:19:29.444880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_df = pd.DataFrame()\nres_df['ID'] = test_df['ID']\nres_df['label'] = y_pre\nres_df.to_csv('/kaggle/working/deepfm-baseline.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T07:19:29.447540Z","iopub.execute_input":"2023-02-23T07:19:29.447908Z","iopub.status.idle":"2023-02-23T07:19:33.504418Z","shell.execute_reply.started":"2023-02-23T07:19:29.447879Z","shell.execute_reply":"2023-02-23T07:19:33.503268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:56:39.837764Z","iopub.execute_input":"2023-02-23T06:56:39.838208Z","iopub.status.idle":"2023-02-23T06:56:39.846814Z","shell.execute_reply.started":"2023-02-23T06:56:39.838169Z","shell.execute_reply":"2023-02-23T06:56:39.845764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}