{"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":"!pip install recbole","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-04-29T02:16:37.002136Z","iopub.execute_input":"2022-04-29T02:16:37.002882Z","iopub.status.idle":"2022-04-29T02:16:53.866469Z","shell.execute_reply.started":"2022-04-29T02:16:37.002765Z","shell.execute_reply":"2022-04-29T02:16:53.8655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# train model with recbole\nhttps://recbole.io/docs/user_guide/usage/use_modules.html","metadata":{}},{"cell_type":"code","source":"import logging\nfrom logging import getLogger\nfrom recbole.config import Config\nfrom recbole.data import create_dataset, data_preparation\nfrom recbole.model.sequential_recommender import GRU4Rec\nfrom recbole.model.sequential_recommender import LightSANs\nfrom recbole.trainer import Trainer\nfrom recbole.utils import init_seed, init_logger","metadata":{"execution":{"iopub.status.busy":"2022-04-29T02:18:10.842329Z","iopub.execute_input":"2022-04-29T02:18:10.842897Z","iopub.status.idle":"2022-04-29T02:18:10.850287Z","shell.execute_reply.started":"2022-04-29T02:18:10.842857Z","shell.execute_reply":"2022-04-29T02:18:10.849375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parameter_dict = {\n    'data_path': '../input/',\n    'USER_ID_FIELD': 'user_id',\n    'ITEM_ID_FIELD': 'item_id',\n    'TIME_FIELD': 'timestamp',\n    'user_inter_num_interval': \"[40,inf)\",\n    'item_inter_num_interval': \"[30,inf)\",\n    'load_col': {'inter': ['user_id', 'item_id', 'timestamp'],\n                 'item': ['item_id', 'product_code', 'product_type_no', 'product_group_name', 'graphical_appearance_no',\n                      'colour_group_code', 'perceived_colour_value_id', 'perceived_colour_master_id',\n                      'department_no', 'index_code', 'index_group_no', 'section_no', 'garment_group_no',#],\n                        *[f'{i}' for i in range(30)]]\n             },\n    'selected_features': ['product_code', 'product_type_no', 'product_group_name', 'graphical_appearance_no',\n                          'colour_group_code', 'perceived_colour_value_id', 'perceived_colour_master_id',\n                          'department_no', 'index_code', 'index_group_no', 'section_no', 'garment_group_no',#],\n                        *[f'{i}' for i in range(30)]],\n    'neg_sampling': None,\n    'eval_args': {\n        'split': {'RS': [9, 0, 1]},\n        'group_by': 'user',\n        'order': 'TO',\n        'mode': 'full'},\n    'learning_rate': 3e-3,\n    'metrics': 'MAP',\n    'topk': 12,\n    'valid_metric': 'MAP@12',\n    'reproducibility': False,\n    'epochs': 50,\n    'train_batch_size': 3000,\n    'embedding_size': 64,\n    'hidden_size': 64,#128,\n    #'num_layers': 1,\n    'dropout_prob': 0.5 #0.3#,\n    #'k_interests': 100\n}\n\nconfig = Config(model='LightSANs', dataset='hm-data-recbole-small', config_dict=parameter_dict)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-04-29T02:22:25.507546Z","iopub.execute_input":"2022-04-29T02:22:25.508147Z","iopub.status.idle":"2022-04-29T02:22:25.537142Z","shell.execute_reply.started":"2022-04-29T02:22:25.508105Z","shell.execute_reply":"2022-04-29T02:22:25.53649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# init random seed\ninit_seed(config['seed'], config['reproducibility'])\n\n# logger initialization\ninit_logger(config)\nlogger = getLogger()\n# Create handlers\nc_handler = logging.StreamHandler()\nc_handler.setLevel(logging.INFO)\nlogger.addHandler(c_handler)\n\n# write config info into log\nlogger.info(config)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-04-29T02:18:28.248984Z","iopub.execute_input":"2022-04-29T02:18:28.249673Z","iopub.status.idle":"2022-04-29T02:18:28.44356Z","shell.execute_reply.started":"2022-04-29T02:18:28.249636Z","shell.execute_reply":"2022-04-29T02:18:28.442977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = create_dataset(config)\nlogger.info(dataset)","metadata":{"execution":{"iopub.status.busy":"2022-04-29T02:18:53.82674Z","iopub.execute_input":"2022-04-29T02:18:53.827015Z","iopub.status.idle":"2022-04-29T02:20:16.231519Z","shell.execute_reply.started":"2022-04-29T02:18:53.826986Z","shell.execute_reply":"2022-04-29T02:20:16.230857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dataset splitting\ntrain_data, valid_data, test_data = data_preparation(config, dataset)","metadata":{"execution":{"iopub.status.busy":"2022-04-29T02:20:28.960108Z","iopub.execute_input":"2022-04-29T02:20:28.960569Z","iopub.status.idle":"2022-04-29T02:20:46.748041Z","shell.execute_reply.started":"2022-04-29T02:20:28.960521Z","shell.execute_reply":"2022-04-29T02:20:46.747408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(config['device'])\n# model loading and initialization\nmodel = LightSANs(config, train_data.dataset).to(config['device'])\nlogger.info(model)\n\n# trainer loading and initialization\ntrainer = Trainer(config, model)\n\n# model training\nbest_valid_score, best_valid_result = trainer.fit(train_data, valid_data=test_data)","metadata":{"execution":{"iopub.status.busy":"2022-04-29T02:22:29.191567Z","iopub.execute_input":"2022-04-29T02:22:29.192058Z","iopub.status.idle":"2022-04-29T03:04:36.368428Z","shell.execute_reply.started":"2022-04-29T02:22:29.192017Z","shell.execute_reply":"2022-04-29T03:04:36.367775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# get preds\nhttps://recbole.io/docs/user_guide/usage/case_study.html","metadata":{}},{"cell_type":"code","source":"from recbole.utils.case_study import full_sort_topk\nexternal_user_ids = dataset.id2token(\n    dataset.uid_field, list(range(dataset.user_num)))[1:]#fist element in array is 'PAD'(default of Recbole) ->remove it ","metadata":{"execution":{"iopub.status.busy":"2022-04-29T03:05:12.201538Z","iopub.execute_input":"2022-04-29T03:05:12.201797Z","iopub.status.idle":"2022-04-29T03:05:12.214079Z","shell.execute_reply.started":"2022-04-29T03:05:12.201769Z","shell.execute_reply":"2022-04-29T03:05:12.213267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"topk_items = []\nfor internal_user_id in list(range(dataset.user_num))[1:]:\n    _, topk_iid_list = full_sort_topk([internal_user_id], model, test_data, k=24, device=config['device'])\n    last_topk_iid_list = topk_iid_list[-1]\n    external_item_list = dataset.id2token(dataset.iid_field, last_topk_iid_list.cpu()).tolist()\n    topk_items.append(external_item_list)\nprint(len(topk_items))","metadata":{"execution":{"iopub.status.busy":"2022-04-29T03:18:27.230832Z","iopub.execute_input":"2022-04-29T03:18:27.231086Z","iopub.status.idle":"2022-04-29T03:19:52.825684Z","shell.execute_reply.started":"2022-04-29T03:18:27.231058Z","shell.execute_reply":"2022-04-29T03:19:52.824904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nexternal_item_str = [' '.join(x) for x in topk_items]\nresult = pd.DataFrame(external_user_ids, columns=['customer_id'])\nresult['prediction'] = external_item_str\nresult.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T03:20:08.121216Z","iopub.execute_input":"2022-04-29T03:20:08.121487Z","iopub.status.idle":"2022-04-29T03:20:08.159079Z","shell.execute_reply.started":"2022-04-29T03:20:08.121454Z","shell.execute_reply":"2022-04-29T03:20:08.158323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.shape\nresult.to_parquet('lightsan_sub_t24.parquet.gzip')","metadata":{"execution":{"iopub.status.busy":"2022-04-29T03:20:17.78792Z","iopub.execute_input":"2022-04-29T03:20:17.788702Z","iopub.status.idle":"2022-04-29T03:20:17.836654Z","shell.execute_reply.started":"2022-04-29T03:20:17.788651Z","shell.execute_reply":"2022-04-29T03:20:17.835881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_parquet('lightsan_sub_t24.parquet.gzip')\ntest.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-29T03:20:23.780028Z","iopub.execute_input":"2022-04-29T03:20:23.780278Z","iopub.status.idle":"2022-04-29T03:20:23.828623Z","shell.execute_reply.started":"2022-04-29T03:20:23.78025Z","shell.execute_reply":"2022-04-29T03:20:23.827868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# combine preds with default preds","metadata":{}},{"cell_type":"code","source":"submit_df = pd.read_csv('../input/0237-ensemble-submission-handm/0238_ensemble.csv')\nsubmit_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-29T03:09:49.70363Z","iopub.execute_input":"2022-04-29T03:09:49.70416Z","iopub.status.idle":"2022-04-29T03:09:55.709957Z","shell.execute_reply.started":"2022-04-29T03:09:49.70412Z","shell.execute_reply":"2022-04-29T03:09:55.709261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T03:10:00.983165Z","iopub.execute_input":"2022-04-29T03:10:00.983419Z","iopub.status.idle":"2022-04-29T03:10:00.991969Z","shell.execute_reply.started":"2022-04-29T03:10:00.98339Z","shell.execute_reply":"2022-04-29T03:10:00.991204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df = pd.merge(submit_df, result, on='customer_id', how='outer')\nsubmit_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T03:10:03.25513Z","iopub.execute_input":"2022-04-29T03:10:03.256611Z","iopub.status.idle":"2022-04-29T03:10:04.259066Z","shell.execute_reply.started":"2022-04-29T03:10:03.256565Z","shell.execute_reply":"2022-04-29T03:10:04.258402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df = submit_df.fillna(-1)\nsubmit_df['prediction'] = submit_df.apply(\n    lambda x: x['prediction_y'] if x['prediction_y'] != -1 else x['prediction_x'], axis=1)\nsubmit_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T03:10:43.080384Z","iopub.execute_input":"2022-04-29T03:10:43.080918Z","iopub.status.idle":"2022-04-29T03:11:05.939184Z","shell.execute_reply.started":"2022-04-29T03:10:43.080877Z","shell.execute_reply":"2022-04-29T03:11:05.938495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df = submit_df.drop(columns=['prediction_y', 'prediction_x'])\nsubmit_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T03:11:15.946001Z","iopub.execute_input":"2022-04-29T03:11:15.946252Z","iopub.status.idle":"2022-04-29T03:11:16.252038Z","shell.execute_reply.started":"2022-04-29T03:11:15.946225Z","shell.execute_reply":"2022-04-29T03:11:16.251133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df.to_csv('submission.csv.gzip', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-29T03:11:29.606552Z","iopub.execute_input":"2022-04-29T03:11:29.607047Z","iopub.status.idle":"2022-04-29T03:11:40.75251Z","shell.execute_reply.started":"2022-04-29T03:11:29.607013Z","shell.execute_reply":"2022-04-29T03:11:40.751763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('test')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}