{"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\n\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":"2021-05-29T17:07:52.112334Z","iopub.execute_input":"2021-05-29T17:07:52.112787Z","iopub.status.idle":"2021-05-29T17:07:52.117619Z","shell.execute_reply.started":"2021-05-29T17:07:52.112729Z","shell.execute_reply":"2021-05-29T17:07:52.116770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nprint(torch.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T17:07:52.119048Z","iopub.execute_input":"2021-05-29T17:07:52.119654Z","iopub.status.idle":"2021-05-29T17:07:52.135644Z","shell.execute_reply.started":"2021-05-29T17:07:52.119616Z","shell.execute_reply":"2021-05-29T17:07:52.134472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pytorch_lightning==1.3.3\n!pip install transformers==4.6.1","metadata":{"execution":{"iopub.status.busy":"2021-05-29T17:07:52.139268Z","iopub.execute_input":"2021-05-29T17:07:52.139552Z","iopub.status.idle":"2021-05-29T17:08:06.925408Z","shell.execute_reply.started":"2021-05-29T17:07:52.139526Z","shell.execute_reply":"2021-05-29T17:08:06.924428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append(\"..\")\nimport pandas as pd\nimport pytorch_lightning as pl\nimport torch\nfrom torch.utils.data import DataLoader\n\nfrom input.hidden.src.data import load_data\nfrom input.hidden.src.data import SodicDataset\nfrom input.hidden.src.model import BertFinetuner\n\ndef run():\n    pl.seed_everything(42)\n    train_data, valid_data, test_data = load_data(\"../input/hidden/data/train.csv\", \"../input/hidden/data/test.csv\")\n    \n    train_set = SodicDataset(train_data)\n    valid_set = SodicDataset(valid_data)\n\n    train_loader = DataLoader(train_set, batch_size=16, shuffle=True, num_workers=4, collate_fn=train_set.collate_fn)\n    valid_loader = DataLoader(valid_set, batch_size=16, shuffle=False, num_workers=4, collate_fn=valid_set.collate_fn)\n\n    model = BertFinetuner()\n    trainer = pl.Trainer(\n        default_root_dir=\"./\",\n        max_epochs=1,\n        gpus=1,\n        deterministic=True\n    )\n    trainer.fit(model, train_loader, valid_loader)\n\n    test_set = SodicDataset(test_data)\n    test_loader = DataLoader(test_set, batch_size=8, shuffle=False, num_workers=4, collate_fn=test_set.collate_fn)\n    preds = trainer.predict(model, test_loader)\n    preds = torch.cat(preds, dim=0).detach().tolist()\n\n    test_df = pd.read_csv(\"../input/hidden/data/test.csv\")\n    test_df[\"label\"] = preds\n    result_df = test_df[[\"id\", \"label\"]]\n    result_df.to_csv(\"submission.csv\", index=False)\n\n\nif __name__ == \"__main__\":\n    run()\n","metadata":{"execution":{"iopub.status.busy":"2021-05-29T17:14:42.695576Z","iopub.execute_input":"2021-05-29T17:14:42.695928Z","iopub.status.idle":"2021-05-29T17:18:46.143058Z","shell.execute_reply.started":"2021-05-29T17:14:42.695895Z","shell.execute_reply":"2021-05-29T17:18:46.142171Z"},"trusted":true},"execution_count":null,"outputs":[]}]}