{"cells":[{"metadata":{"_uuid":"c7cc9bd9-b7dd-410f-ad76-06883a648265","_cell_guid":"ce7a03c0-1782-4acf-b588-adcb9803842e","trusted":true},"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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e5c72616-79c6-4a8b-aee2-cf43f9319182","_cell_guid":"da8e54c7-2660-444c-8493-87428f996176","trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9af78f4f-80f0-4592-80dd-3750125f5249","_cell_guid":"3f4b6cf2-a090-4812-8f44-93a6bffe462d","trusted":true},"cell_type":"code","source":"from fastai.text import *\nfrom fastai import *","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"738de188-dced-4f8b-b1c5-3b14d40b8704","_cell_guid":"25f18b5c-e005-4197-bad2-e59d41a04ac8","trusted":true},"cell_type":"code","source":"#path = Path('/kaggle/input/jigsaw-multilingual-toxic-comment-classification/')\n#path.ls()\n!mkdir data\n!pwd\n!cp -a ../input/jigsaw-multilingual-toxic-comment-classification/jigsaw-toxic-comment-train.csv ./data/\n!cp -a ../input/jigsaw-multilingual-toxic-comment-classification/test.csv ./data/\n!cp -a ../input/jigsaw-multilingual-toxic-comment-classification/validation.csv ./data/\n#!cp -a ../input/jigsaw-multilingual-toxic-comment-classification/jigsaw-unintended-bias-train.csv ./data\n#!cp -a ../input/jigsaw-multilingual-toxic-comment-classification/sample_submission.csv ./data\n!ls data\n\npath = Path('/kaggle/working/data/')\npath.ls()\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = pd.read_csv(path/'jigsaw-toxic-comment-train.csv')\n#df_test = pd.read_csv(path/'jigsaw-unintended-bias-train.csv', usecols=[0,1,2,3,4,5,6,7])\n#df_test.columns=['id','comment_text','toxic','severe_toxic','obscene','identity_hate','insult','threat']\n#df = df_train.append(df_test)\ndf = df_train\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9582c02c-6b88-40b4-bb92-46f61ac0c26a","_cell_guid":"eaf74677-0f26-44f6-9e96-eba6fa6ada73","trusted":true},"cell_type":"code","source":"bs = 64\ndata_lm = (TextList.from_df(df, path, cols='comment_text')\n                .split_by_rand_pct(0.1)\n                .label_for_lm()\n                .databunch(bs=bs))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"81d78262-cc88-45dc-a609-d58e2cb9fb6f","_cell_guid":"8d53c460-d042-4941-a800-9b1195852ff7","trusted":true},"cell_type":"code","source":"data_lm.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"50f50f51-a3d9-4664-87e0-e7cf2a1b8af1","_cell_guid":"c9b39ab2-e1eb-4dc6-a85c-4bfb444b85c2","trusted":true},"cell_type":"code","source":"learn = language_model_learner(data_lm, AWD_LSTM, drop_mult=0.3)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"85344358-2dd1-49e9-8176-c6ac05921022","_cell_guid":"ce44f065-b23e-4dbc-8974-4cef736e551d","trusted":true},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot(skip_end=15)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3e88f522-55b6-4eca-b5bc-954e3965ce39","_cell_guid":"19b76fd8-6e78-427c-a653-08c6301f4ea0","trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(1, 1e-2, moms=(0.8,0.7))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"09d3b496-4f07-4a72-9774-93d05aa93d73","_cell_guid":"199342d1-e548-402b-93a3-896f15929026","trusted":true},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(1, 1e-3, moms=(0.8,0.7))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"38b3f552-5a83-446c-a37b-c82db514e4cd","_cell_guid":"3c7a7233-c58f-4ca1-acf4-3c7486fe0cfb","trusted":true},"cell_type":"code","source":"learn.save_encoder('fine_tuned_enc')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"38228f6e-9e06-4399-aeb0-9edbb59c66fc","_cell_guid":"a30de5a7-b3e6-4fda-8798-d62828e82a54","trusted":true},"cell_type":"code","source":"test = pd.read_csv(path/\"test.csv\")\ntest_datalist = TextList.from_df(test, cols='content')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"df48ed57-a9bb-44f7-b202-e62c33c7796b","_cell_guid":"362b498b-8e5b-4491-8175-a134168e3e0b","trusted":true},"cell_type":"code","source":"data_cls = (TextList.from_csv(path, 'jigsaw-toxic-comment-train.csv', cols='comment_text', vocab=data_lm.vocab)\n                .split_by_rand_pct(valid_pct=0.1)\n                .label_from_df(cols=['toxic'])\n                .add_test(test_datalist)\n                .databunch())\ndata_cls.save('data_clas.pkl')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2df0515d-c58b-454c-9b2b-cd3c572a4ba1","_cell_guid":"f5a2c7c4-d2af-4b1c-8195-657d81e57eab","trusted":true},"cell_type":"code","source":"data_clas = load_data(path, 'data_clas.pkl', bs=bs)\ndata_clas.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f567a497-47a6-405a-8d05-5d14c12b8201","_cell_guid":"090a49ae-11f8-499c-95cd-4d7ccf07e00c","trusted":true},"cell_type":"code","source":"learn = text_classifier_learner(data_clas, AWD_LSTM, drop_mult=0.5)\nlearn.load_encoder('fine_tuned_enc')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5d5e2cbb-df97-473d-8a7b-6426a708f174","_cell_guid":"97be6bf2-8b0d-4ceb-9520-9c12c246dbc4","trusted":true},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"49f3438b-a239-4c31-91cf-f3ef3553c76b","_cell_guid":"9774550b-8243-4e9a-9ed2-5081bf50bdd2","trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(1, 2e-2, moms=(0.8,0.7))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5a973f6c-48bf-496f-8cdd-c02643c48db6","_cell_guid":"4f50d8c9-b0d3-42ca-9a9e-c1d8bf95b3ce","trusted":true},"cell_type":"code","source":"learn.save('first')\nlearn.load('first');","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"11af98f1-f01f-4531-9056-c73d07335386","_cell_guid":"e7d29ca7-40d8-48e3-a832-1dddd771f609","trusted":true},"cell_type":"code","source":"learn.freeze_to(-2)\nlearn.fit_one_cycle(1, slice(1e-2/(2.6**4),1e-2), moms=(0.8,0.7))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"30e93929-bd6f-483f-ab9d-8b429fff27ea","_cell_guid":"388290e8-ad8d-48d0-8dea-71d6048c9793","trusted":true},"cell_type":"code","source":"learn.save('second');\nlearn.load('second');","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9c45a696-da76-46bf-bc2b-3e57338fce30","_cell_guid":"e4d5d645-23d0-443b-9f12-ebdc85929d2c","trusted":true},"cell_type":"code","source":"learn.freeze_to(-3)\nlearn.fit_one_cycle(1, slice(5e-3/(2.6**4),5e-3), moms=(0.8,0.7))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e96934c8-dc97-4416-80bd-6f0623940f68","_cell_guid":"41d3f2ba-bc61-4647-83ee-f7282717a983","trusted":true},"cell_type":"code","source":"learn.save('third')\nlearn.load('third');","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"da26384c-90e7-4a8f-a7e3-396ee4daa4fd","_cell_guid":"2837d337-51d0-4d33-b2c4-c83e2b557382","trusted":true},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(1, slice(1e-3/(2.6**4),1e-3), moms=(0.8,0.7))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"68b264ad-622a-4d8c-963d-8cc419a0e5ca","_cell_guid":"8cd19775-0f3a-4d99-99b5-6d122a15810a","trusted":true},"cell_type":"code","source":"preds, target = learn.get_preds(DatasetType.Test, ordered=True)\nlabels = preds.numpy()\nlabels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cf0fe610-7de3-48b5-a4d0-d39293426322","_cell_guid":"a335c40a-42cf-46f2-b309-b3ce13256d10","trusted":true},"cell_type":"code","source":"test_id = test['id']\nlabel_cols = ['something','toxic']\n\nsubmission = pd.DataFrame({'id': test_id})\n#submission=pd.DataFrame(preds.numpy(), columns = label_cols)\nsubmission = pd.concat([submission, pd.DataFrame(preds.numpy(), columns = label_cols)], axis=1)\nheader=[\"id\",\"toxic\"]\nsubmission.to_csv('submission.csv',columns=header, index=False)\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9aeb6b07-e57d-4972-a594-8d226510935f","_cell_guid":"cd2643d1-1efa-48b5-81a9-93c3f6939701","trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}