{"cells":[{"metadata":{"trusted":true,"_uuid":"33f4a9d4f39f29c4958be31c850f2d0aef00fe1a"},"cell_type":"markdown","source":"https://docs.fast.ai/text.html"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from fastai import *\nfrom fastai.text import * \nfrom fastai.gen_doc.nbdoc import *\nfrom fastai.datasets import * \nfrom fastai.datasets import Config\nfrom pathlib import Path\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"path = Path('../input/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a134c6446bf05835226b3acd2a2823af05c1c09d"},"cell_type":"code","source":"df_test = pd.read_csv(path/'test.csv')\ndf_test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9ee8ee66a3a6f8ca9c1507f703e44682c5588ee0"},"cell_type":"code","source":"df = pd.read_csv(path/'train.csv')#.sample(frac=0.05)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03ddd741cf19dfa20ce00cc424c1c14ab9a25871"},"cell_type":"code","source":"data_lm = (TextList.from_csv(path, 'train.csv', cols='question_text')\n            .random_split_by_pct(.2)\n            .label_for_lm()\n            .add_test(TextList.from_csv(path, 'test.csv', cols='question_text'))\n            .databunch())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed045f8558f081117d4e24e7b10935a1caf3dfca"},"cell_type":"code","source":"data_clas = (TextList.from_csv(path, 'train.csv', cols='question_text', vocab=data_lm.vocab)\n            #.split_from_df(col='target')\n             .random_split_by_pct(.2)\n            .label_from_df(cols='target')\n            #.add_test(TextList.from_csv(path, 'test.csv', cols='question_text'))\n            .databunch(bs=42))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"52cb4bd77fd2a74850ed214e638b57069bdf459a"},"cell_type":"code","source":"data_clas.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"406df48024d3b0014542c213205265986aede644"},"cell_type":"code","source":"MODEL_PATH = \"/tmp/model/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e1a75766869b62b29c66d0bd4ef78eba6203cd04"},"cell_type":"code","source":"learn = text_classifier_learner(data_clas,model_dir=MODEL_PATH)\n#learn.load_encoder('mini_train_encoder')\nlearn.fit_one_cycle(2, slice(1e-3,1e-2))\n#learn.save('mini_train_clas')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"77c83eec1d36f85b3eb88f80d1ae77097a335a3e"},"cell_type":"code","source":"learn.show_results()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"984b944e59e5aa4a17b971fd367e6a04aebe59cc"},"cell_type":"code","source":"# Language model data\n#data_lm = TextLMDataBunch.from_csv(path, 'train.csv', cols='question_text')\n# Classifier model data\n#data_clas = TextClasDataBunch.from_csv(path, 'train.csv', vocab=data_lm.train_ds.vocab, bs=4) #32\n\n#data_lm = TextLMDataBunch.from_df(path, df)\n#data_clas = TextClasDataBunch.from_df(path, df,  vocab=data_lm.train_ds.vocab)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3757c9e6155ccee3ab288e13cddd81ae090702c4"},"cell_type":"code","source":"data_lm.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2bf25bb10211653a2e4b84594a3d7f4a02114c0d"},"cell_type":"code","source":"#??language_model_learner()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"090fee4283f67de45ea1b7cddf7c7e5a81b42181"},"cell_type":"code","source":"learn = language_model_learner(data_lm, drop_mult=0.5, model_dir=MODEL_PATH) #bptt=65, emb_sz=400, pretrained_model=URLs.WT103","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5b547d3d30d3504000ef72dedfa4c3c89490d72a"},"cell_type":"code","source":"acc_02 = partial(accuracy_thresh, thresh=0.2)\nf_score = partial(fbeta, thresh=0.2)\n\nlearn.metrics = metrics=[accuracy,acc_02, f_score]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"954909a2e6a2a01b94bd33997ee91758cec5bd1d"},"cell_type":"code","source":"learn.fit_one_cycle(1, 1e-2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e83006f151b2ed8cfea3a27780725d962a7ca6d6"},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(1, 1e-3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"647d428cf2b1829ef30eda69590b36f129c6b28b"},"cell_type":"code","source":"learn.predict(\"This is a review about\", n_words=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b937f1bf6c94694394477aadefb2e4fdfaaa4b9f"},"cell_type":"code","source":"learn.save_encoder('ft_enc')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bc597a07ff57956f22db96188976c12c2ee10978"},"cell_type":"code","source":"learn = text_classifier_learner(data_clas, drop_mult=0.5)\nlearn.load_encoder('ft_enc')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cbf40d0e89b86e148674d899de9c60942b2c3cce"},"cell_type":"code","source":"data_clas.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fa34af170952a777e92fa60a3af5239c31cf3171"},"cell_type":"code","source":"learn.fit_one_cycle(1, 1e-2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7fc735688aa22df6fc8b977b6ffacae4f14a1a22"},"cell_type":"code","source":"learn.freeze_to(-2)\nlearn.fit_one_cycle(1, slice(5e-3/2., 5e-3))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4ad82b9c6d20f17711c6568ef59a121fc50e4566"},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(1, slice(2e-3/100, 2e-3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a8da586e00b20c4c9e095d16a0bc801831b719de"},"cell_type":"code","source":"learn.predict(\"This was a great movie!\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a391763661f383a9b83854d95c99cc5b1ec11c44"},"cell_type":"code","source":"learn.show_results()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ad840fad519642520a6a80f6691372aa5e617653"},"cell_type":"code","source":"probs, _ = learn.get_preds(DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"36955e13d075c600ca6bd9f569d59df5a1e4f8ba"},"cell_type":"code","source":"preds = np.argmax(probs, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a1bbb0fc61c8e02bf3651c74f26d72226f3398dc"},"cell_type":"code","source":"ids = df_test[\"qid\"].copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"01c79a1ae223128e6806034670286445e838c699"},"cell_type":"code","source":"submission = pd.DataFrame(data={\n    \"qid\": ids,\n    \"prediction\": preds\n})\nsubmission.to_csv(\"submission.csv\", index=False)\nsubmission.head(n=50)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}