{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"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",
" label | \n",
" text | \n",
" is_valid | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" negative | \n",
" Un-bleeping-believable! Meg Ryan doesn't even ... | \n",
" False | \n",
"
\n",
" \n",
" 1 | \n",
" positive | \n",
" This is a extremely well-made film. The acting... | \n",
" False | \n",
"
\n",
" \n",
" 2 | \n",
" negative | \n",
" Every once in a long while a movie will come a... | \n",
" False | \n",
"
\n",
" \n",
" 3 | \n",
" positive | \n",
" Name just says it all. I watched this movie wi... | \n",
" False | \n",
"
\n",
" \n",
" 4 | \n",
" negative | \n",
" This movie succeeds at being one of the most u... | \n",
" False | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" label text is_valid\n",
"0 negative Un-bleeping-believable! Meg Ryan doesn't even ... False\n",
"1 positive This is a extremely well-made film. The acting... False\n",
"2 negative Every once in a long while a movie will come a... False\n",
"3 positive Name just says it all. I watched this movie wi... False\n",
"4 negative This movie succeeds at being one of the most u... False"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_csv(path/'texts.csv')\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 just 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": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" epoch | \n",
" train_loss | \n",
" valid_loss | \n",
" accuracy | \n",
" time | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" 4.752841 | \n",
" 3.936385 | \n",
" 0.286949 | \n",
" 00:17 | \n",
"
\n",
" \n",
" 2 | \n",
" 4.450984 | \n",
" 3.839346 | \n",
" 0.292485 | \n",
" 00:17 | \n",
"
\n",
" \n",
" 3 | \n",
" 4.170427 | \n",
" 3.803478 | \n",
" 0.294866 | \n",
" 00:17 | \n",
"
\n",
" \n",
" 4 | \n",
" 3.946596 | \n",
" 3.798583 | \n",
" 0.295342 | \n",
" 00:17 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"learn = language_model_learner(data_lm, AWD_LSTM)\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": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" epoch | \n",
" train_loss | \n",
" valid_loss | \n",
" accuracy | \n",
" time | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" 0.706597 | \n",
" 0.635787 | \n",
" 0.671642 | \n",
" 00:33 | \n",
"
\n",
" \n",
" 2 | \n",
" 0.687497 | \n",
" 0.627676 | \n",
" 0.651741 | \n",
" 00:33 | \n",
"
\n",
" \n",
" 3 | \n",
" 0.668437 | \n",
" 0.603736 | \n",
" 0.681592 | \n",
" 00:30 | \n",
"
\n",
" \n",
" 4 | \n",
" 0.650379 | \n",
" 0.597291 | \n",
" 0.676617 | \n",
" 00:30 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"learn = text_classifier_learner(data_clas, AWD_LSTM)\n",
"learn.load_encoder('enc')\n",
"learn.fit_one_cycle(4, moms=moms)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" epoch | \n",
" train_loss | \n",
" valid_loss | \n",
" accuracy | \n",
" time | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" 0.635453 | \n",
" 0.589016 | \n",
" 0.696517 | \n",
" 00:51 | \n",
"
\n",
" \n",
" 2 | \n",
" 0.644397 | \n",
" 0.604481 | \n",
" 0.676617 | \n",
" 00:55 | \n",
"
\n",
" \n",
" 3 | \n",
" 0.646375 | \n",
" 0.579778 | \n",
" 0.696517 | \n",
" 00:55 | \n",
"
\n",
" \n",
" 4 | \n",
" 0.632535 | \n",
" 0.552055 | \n",
" 0.731343 | \n",
" 00:53 | \n",
"
\n",
" \n",
" 5 | \n",
" 0.638672 | \n",
" 0.544292 | \n",
" 0.736318 | \n",
" 00:51 | \n",
"
\n",
" \n",
" 6 | \n",
" 0.621485 | \n",
" 0.549286 | \n",
" 0.736318 | \n",
" 00:55 | \n",
"
\n",
" \n",
" 7 | \n",
" 0.612489 | \n",
" 0.543637 | \n",
" 0.741294 | \n",
" 00:53 | \n",
"
\n",
" \n",
" 8 | \n",
" 0.611454 | \n",
" 0.542703 | \n",
" 0.736318 | \n",
" 00:54 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"learn.unfreeze()\n",
"learn.fit_one_cycle(8, slice(1e-5,1e-3), moms=moms)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}