{ "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/examples/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", " \n", " \n", " \n", " \n", " \n", " \n", "
012
0labeltextis_valid
1negativeUn-bleeping-believable! Meg Ryan doesn't even ...False
2positiveThis is a extremely well-made film. The acting...False
3negativeEvery once in a long while a movie will come a...False
4positiveName just says it all. I watched this movie wi...False
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 label text is_valid\n", "1 negative Un-bleeping-believable! Meg Ryan doesn't even ... False\n", "2 positive This is a extremely well-made film. The acting... False\n", "3 negative Every once in a long while a movie will come a... False\n", "4 positive Name just says it all. I watched this movie wi... False" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(path/'texts.csv', header=None)\n", "df.head()" ] }, { "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, 'texts.csv')\n", "data_clas = TextClasDataBunch.from_csv(path, 'texts.csv', vocab=data_lm.train_ds.vocab, bs=42)" ] }, { "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": [], "source": [ "moms = (0.8,0.7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total time: 01:36\n", "epoch train_loss valid_loss accuracy\n", "1 4.977893 4.187432 0.249392 (00:24)\n", "2 4.619538 4.074967 0.253530 (00:23)\n", "3 4.328201 4.020354 0.261265 (00:24)\n", "4 4.082543 4.019140 0.259830 (00:24)\n", "\n" ] } ], "source": [ "learn = language_model_learner(data_lm, pretrained_model=URLs.WT103_1)\n", "learn.unfreeze()\n", "learn.fit_one_cycle(4, slice(1e-2), moms=moms)" ] }, { "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": [ { "name": "stdout", "output_type": "stream", "text": [ "Total time: 01:38\n", "epoch train_loss valid_loss accuracy\n", "1 0.657817 0.621307 0.736318 (00:23)\n", "2 0.614628 0.503447 0.810945 (00:25)\n", "3 0.579545 0.493127 0.766169 (00:23)\n", "4 0.588436 0.482557 0.791045 (00:25)\n", "\n" ] } ], "source": [ "learn = text_classifier_learner(data_clas)\n", "learn.load_encoder('enc')\n", "learn.freeze()\n", "learn.fit_one_cycle(4, moms=moms)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total time: 07:28\n", "epoch train_loss valid_loss accuracy\n", "1 0.551981 0.475971 0.800995 (00:58)\n", "2 0.574055 0.495363 0.756219 (00:59)\n", "3 0.551674 0.459942 0.796020 (00:55)\n", "4 0.532213 0.444801 0.791045 (00:50)\n", "5 0.519754 0.427174 0.810945 (00:56)\n", "6 0.509370 0.424626 0.830846 (00:50)\n", "7 0.502223 0.415141 0.845771 (00:56)\n", "8 0.518687 0.411042 0.835821 (01:00)\n", "\n" ] } ], "source": [ "learn.unfreeze()\n", "learn.fit_one_cycle(8, slice(1e-5,1e-3), moms=moms)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }