Created
July 24, 2020 18:22
-
-
Save hiromis/1115a1135a2d32ae1f954608c9e416e5 to your computer and use it in GitHub Desktop.
fastai/fastbook/10_nlp-Copy1.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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, tok_func=SpacyTokenizer),\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 of its popularity . It was the first film that Henry Moore wrote about . The film received a high profile in the United States , and received a mixed reception from critics .\ni liked this movie because it was a \" fun , fun , and an even fun film \" . When the film was released in 1996 , it was screened at the Cannes Film Festival . The film was\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "fastai/fastbook/10_nlp-Copy1.ipynb", | |
"public": true | |
} | |
}, | |
"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" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment