{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## NLP model creation and training" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [], "source": [ "from fastai.gen_doc.nbdoc import *\n", "from fastai.text import * \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The main thing here is [`RNNLearner`](/text.learner.html#RNNLearner). There are also some utility functions to help create and update text models." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Quickly get a learner" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "
language_model_learner
[source]language_model_learner
(**`data`**:[`DataBunch`](/basic_data.html#DataBunch), **`arch`**, **`config`**:`dict`=***`None`***, **`drop_mult`**:`float`=***`1.0`***, **`pretrained`**:`bool`=***`True`***, **`pretrained_fnames`**:`OptStrTuple`=***`None`***, **\\*\\*`learn_kwargs`**) → `LanguageLearner`\n",
"\n",
"Create a [`Learner`](/basic_train.html#Learner) with a language model from `data` and `arch`. "
],
"text/plain": [
"{lower_case_class_name}_lm_config
if you want to change the default parameter. At this stage, only the AWD LSTM support `pretrained=True` but we hope to add more pretrained models soon. `drop_mult` is applied to all the dropouts weights of the `config`, `learn_kwargs` are passed to the [`Learner`](/basic_train.html#Learner) initialization."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide_input": true
},
"outputs": [
{
"data": {
"text/markdown": [
"text_classifier_learner
[source]text_classifier_learner
(**`data`**:[`DataBunch`](/basic_data.html#DataBunch), **`arch`**:`Callable`, **`bptt`**:`int`=***`70`***, **`max_len`**:`int`=***`1400`***, **`config`**:`dict`=***`None`***, **`pretrained`**:`bool`=***`True`***, **`drop_mult`**:`float`=***`1.0`***, **`lin_ftrs`**:`Collection`\\[`int`\\]=***`None`***, **`ps`**:`Collection`\\[`float`\\]=***`None`***, **\\*\\*`learn_kwargs`**) → `TextClassifierLearner`\n",
"\n",
"Create a [`Learner`](/basic_train.html#Learner) with a text classifier from `data` and `arch`. "
],
"text/plain": [
"class
RNNLearner
[source]RNNLearner
(**`data`**:[`DataBunch`](/basic_data.html#DataBunch), **`model`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), **`split_func`**:`OptSplitFunc`=***`None`***, **`clip`**:`float`=***`None`***, **`alpha`**:`float`=***`2.0`***, **`beta`**:`float`=***`1.0`***, **`metrics`**=***`None`***, **\\*\\*`learn_kwargs`**) :: [`Learner`](/basic_train.html#Learner)\n",
"\n",
"Basic class for a [`Learner`](/basic_train.html#Learner) in NLP. "
],
"text/plain": [
"data
and a `model` with a text data using a certain `bptt`. The `split_func` is used to properly split the model in different groups for gradual unfreezing and differential learning rates. Gradient clipping of `clip` is optionally applied. `alpha` and `beta` are all passed to create an instance of [`RNNTrainer`](/callbacks.rnn.html#RNNTrainer). Can be used for a language model or an RNN classifier. It also handles the conversion of weights from a pretrained model as well as saving or loading the encoder."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide_input": true
},
"outputs": [
{
"data": {
"text/markdown": [
"get_preds
[source]get_preds
(**`ds_type`**:[`DatasetType`](/basic_data.html#DatasetType)=***`load_encoder
[source]load_encoder
(**`name`**:`str`)\n",
"\n",
"Load the encoder `name` from the model directory. "
],
"text/plain": [
"save_encoder
[source]save_encoder
(**`name`**:`str`)\n",
"\n",
"Save the encoder to `name` inside the model directory. "
],
"text/plain": [
"load_pretrained
[source]load_pretrained
(**`wgts_fname`**:`str`, **`itos_fname`**:`str`, **`strict`**:`bool`=***`True`***)\n",
"\n",
"Load a pretrained model and adapts it to the data vocabulary. "
],
"text/plain": [
"data
. The two files should be in the models directory of the `learner.path`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Utility functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide_input": true
},
"outputs": [
{
"data": {
"text/markdown": [
"convert_weights
[source]convert_weights
(**`wgts`**:`Weights`, **`stoi_wgts`**:`Dict`\\[`str`, `int`\\], **`itos_new`**:`StrList`) → `Weights`\n",
"\n",
"Convert the model `wgts` to go with a new vocabulary. "
],
"text/plain": [
"class
LanguageLearner
[source]LanguageLearner
(**`data`**:[`DataBunch`](/basic_data.html#DataBunch), **`model`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), **`split_func`**:`OptSplitFunc`=***`None`***, **`clip`**:`float`=***`None`***, **`alpha`**:`float`=***`2.0`***, **`beta`**:`float`=***`1.0`***, **`metrics`**=***`None`***, **\\*\\*`learn_kwargs`**) :: [`RNNLearner`](/text.learner.html#RNNLearner)\n",
"\n",
"Subclass of RNNLearner for predictions. "
],
"text/plain": [
"predict
[source]predict
(**`text`**:`str`, **`n_words`**:`int`=***`1`***, **`no_unk`**:`bool`=***`True`***, **`temperature`**:`float`=***`1.0`***, **`min_p`**:`float`=***`None`***, **`sep`**:`str`=***`' '`***, **`decoder`**=***`'decode_spec_tokens'`***)\n",
"\n",
"Return the `n_words` that come after `text`. "
],
"text/plain": [
"beam_search
[source]beam_search
(**`text`**:`str`, **`n_words`**:`int`, **`no_unk`**:`bool`=***`True`***, **`top_k`**:`int`=***`10`***, **`beam_sz`**:`int`=***`1000`***, **`temperature`**:`float`=***`1.0`***, **`sep`**:`str`=***`' '`***, **`decoder`**=***`'decode_spec_tokens'`***)\n",
"\n",
"Return the `n_words` that come after `text` using beam search. "
],
"text/plain": [
"get_language_model
[source]get_language_model
(**`arch`**:`Callable`, **`vocab_sz`**:`int`, **`config`**:`dict`=***`None`***, **`drop_mult`**:`float`=***`1.0`***)\n",
"\n",
"Create a language model from `arch` and its `config`, maybe `pretrained`. "
],
"text/plain": [
"get_text_classifier
[source]get_text_classifier
(**`arch`**:`Callable`, **`vocab_sz`**:`int`, **`n_class`**:`int`, **`bptt`**:`int`=***`70`***, **`max_len`**:`int`=***`1400`***, **`config`**:`dict`=***`None`***, **`drop_mult`**:`float`=***`1.0`***, **`lin_ftrs`**:`Collection`\\[`int`\\]=***`None`***, **`ps`**:`Collection`\\[`float`\\]=***`None`***) → [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n",
"\n",
"Create a text classifier from `arch` and its `config`, maybe `pretrained`. "
],
"text/plain": [
"forward
[source]forward
(**`input`**:`LongTensor`) → `Tuple`\\[`Tensor`, `Tensor`\\]\n",
"\n",
"Defines the computation performed at every call. Should be overridden by all subclasses.\n",
"\n",
".. note::\n",
" Although the recipe for forward pass needs to be defined within\n",
" this function, one should call the :class:`Module` instance afterwards\n",
" instead of this since the former takes care of running the\n",
" registered hooks while the latter silently ignores them. "
],
"text/plain": [
"show_results
[source]show_results
(**`ds_type`**=***`concat
[source]concat
(**`arrs`**:`Collection`\\[`Tensor`\\]) → `Tensor`\n",
"\n",
"Concatenate the `arrs` along the batch dimension. "
],
"text/plain": [
"class
MultiBatchEncoder
[source]MultiBatchEncoder
(**`bptt`**:`int`, **`max_len`**:`int`, **`module`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n",
"\n",
"Create an encoder over `module` that can process a full sentence. "
],
"text/plain": [
"decode_spec_tokens
[source]decode_spec_tokens
(**`tokens`**)"
],
"text/plain": [
"reset
[source]reset
()"
],
"text/plain": [
"