{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from fastai import * # Quick access to most common functionality\n", "from fastai.text import * # Quick access to NLP functionality" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Text example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An example of creating a language model and then transfering to a classifier." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "PosixPath('/home/ubuntu/.fastai/data/imdb_sample')" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "path = untar_data(URLs.IMDB_SAMPLE)\n", "path" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open and view the independent and dependent variables:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
01
00Un-bleeping-believable! Meg Ryan doesn't even ...
11This is a extremely well-made film. The acting...
20Every once in a long while a movie will come a...
31Name just says it all. I watched this movie wi...
40This movie succeeds at being one of the most u...
\n", "
" ], "text/plain": [ " 0 1\n", "0 0 Un-bleeping-believable! Meg Ryan doesn't even ...\n", "1 1 This is a extremely well-made film. The acting...\n", "2 0 Every once in a long while a movie will come a...\n", "3 1 Name just says it all. I watched this movie wi...\n", "4 0 This movie succeeds at being one of the most u..." ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(path/'train.csv', header=None)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('negative', 'positive')" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classes = read_classes(path/'classes.txt')\n", "classes[0], classes[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a `DataBunch` for each of the language model and the classifier:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data_lm = TextLMDataBunch.from_csv(path)\n", "data_clas = TextClasDataBunch.from_csv(path, vocab=data_lm.train_ds.vocab)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We'll fine-tune the language model. [fast.ai](http://www.fast.ai/) has a pre-trained English model available that we can download, we jsut have to specify it like this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HBox(children=(IntProgress(value=0, max=2), HTML(value='0.00% [0/2 00:00<00:00]'))), HTML(value…" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Total time: 00:44\n", "epoch train loss valid loss accuracy\n", "1 4.921718 4.155864 0.245985 (00:22)\n", "2 4.640784 4.090212 0.252899 (00:22)\n", "\n" ] } ], "source": [ "learn = RNNLearner.language_model(data_lm, pretrained_model=URLs.WT103)\n", "learn.unfreeze()\n", "learn.fit(2, slice(1e-4,1e-2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save our language model's encoder:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.save_encoder('enc')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fine tune it to create a classifier:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HBox(children=(IntProgress(value=0, max=3), HTML(value='0.00% [0/3 00:00<00:00]'))), HTML(value…" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Total time: 00:46\n", "epoch train loss valid loss accuracy\n", "1 0.686322 0.669499 0.635000 (00:15)\n", "2 0.651778 0.617119 0.715000 (00:15)\n", "3 0.654797 0.593843 0.695000 (00:15)\n", "\n" ] } ], "source": [ "learn = RNNLearner.classifier(data_clas)\n", "learn.load_encoder('enc')\n", "learn.fit(3, 1e-3)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }