Skip to content

Instantly share code, notes, and snippets.

@hiromis
Created Jun 26, 2020
Embed
What would you like to do?
fastai/fastbook/10_nlp.ipynb
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from fastai2.text.all import *\npath = untar_data(URLs.IMDB)",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "get_imdb = partial(get_text_files, folders=['train', 'test', 'unsup'])\n\ndls_lm = DataBlock(\n blocks=TextBlock.from_folder(path, is_lm=True),\n get_items=get_imdb, splitter=RandomSplitter(0.1)\n).dataloaders(path, path=path, bs=128, seq_len=80)",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "learn = language_model_learner(\n dls_lm, AWD_LSTM, drop_mult=0.3, \n metrics=[accuracy, Perplexity()]).to_fp16()",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "TEXT = \"I liked this movie because\"\nN_WORDS = 40\nN_SENTENCES = 2\npreds = [learn.predict(TEXT, N_WORDS, temperature=0.75) \n for _ in range(N_SENTENCES)]",
"execution_count": 4,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "<IPython.core.display.HTML object>",
"text/html": ""
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": "<IPython.core.display.HTML object>",
"text/html": ""
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(\"\\n\".join(preds))",
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": "i liked this movie because it represented a perfect guy , as it was a great deal of time to make a movie . The film was heavily influenced by Robert Altman 's novel The Search for a Family\ni liked this movie because it was reminiscent of a movie . Since then , Lewis has been involved in a number of films , including Let 's Get Crazy , The Amazing Race , The\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"jupytext": {
"split_at_heading": true
},
"kernelspec": {
"name": "fastai",
"display_name": "fastai",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.7.5",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "fastai/fastbook/10_nlp.ipynb",
"public": false
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment