{"cells":[{"metadata":{"trusted":true,"_uuid":"33f4a9d4f39f29c4958be31c850f2d0aef00fe1a"},"cell_type":"markdown","source":"https://docs.fast.ai/text.html"},{"metadata":{"trusted":true,"_uuid":"fcf77c495f4a6fed6b62e6bfd869f7bd6951d3ec"},"cell_type":"code","source":"!pip3 install fastai==1.0.42","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from 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 = untar_data(URLs.IMDB_SAMPLE)\npath","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9ee8ee66a3a6f8ca9c1507f703e44682c5588ee0"},"cell_type":"code","source":"df = pd.read_csv(path/'texts.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"984b944e59e5aa4a17b971fd367e6a04aebe59cc"},"cell_type":"code","source":"# Language model data\ndata_lm = TextLMDataBunch.from_csv(path, 'texts.csv')\n# Classifier model data\ndata_clas = TextClasDataBunch.from_csv(path, 'texts.csv', vocab=data_lm.train_ds.vocab, bs=32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"090fee4283f67de45ea1b7cddf7c7e5a81b42181"},"cell_type":"code","source":"learn = language_model_learner(data_lm, pretrained_model=URLs.WT103, drop_mult=0.5)\nlearn.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(5, 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(5, 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":"","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}