{ "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/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": [ "[fast.ai](http://www.fast.ai/) has a pre-trained English model available that we can download." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "347a1b95288949099a5727fb6e3ac604", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=221972701), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9afb98d2cf8b498e939f0d3e113f8bac", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=1027972), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "datasets.download_wt103_model()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We'll fine-tune the language model:" ] }, { "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:46\n", "epoch train loss valid loss accuracy\n", "0 4.905827 4.156222 0.246251 (00:23)\n", "1 4.632848 4.087290 0.254222 (00:23)\n", "\n" ] } ], "source": [ "learn = RNNLearner.language_model(data_lm, pretrained_fnames=['lstm_wt103', 'itos_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", "0 0.669598 0.671728 0.560000 (00:15)\n", "1 0.664123 0.618949 0.720000 (00:15)\n", "2 0.653664 0.589520 0.710000 (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 }