<jupyter_start><jupyter_text>NLP model creation and training<jupyter_code>from fastai.gen_doc.nbdoc import *
from fastai.text import *<jupyter_output><empty_output><jupyter_text>The main thing here is [`RNNLearner`](/text.learner.htmlRNNLearner). There are also some utility functions to help create and update text models.  Quickly get a learner<jupyter_code>show_doc(language_model_learner)<jupyter_output><empty_output><jupyter_text>The model used is given by `arch` and `config`. It can be:- an [AWD_LSTM](/text.models.htmlAWD_LSTM)([Merity et al.](https://arxiv.org/abs/1708.02182))- a [Transformer](/text.models.htmlTransformer) decoder ([Vaswani et al.](https://arxiv.org/abs/1706.03762))- a [TransformerXL](/text.models.htmlTransformerXL) ([Dai et al.](https://arxiv.org/abs/1901.02860))They each have a default config for language modelling that is in {lower_case_class_name}\_lm\_config if you want to change the default parameter. At this stage, only the AWD LSTM and Tranformer 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.htmlLearner) initialization.If your [`data`](/text.data.htmltext.data) is backward, the pretrained model downloaded will also be a backard one (only available for [`AWD_LSTM`](/text.models.awd_lstm.htmlAWD_LSTM)).<jupyter_code>jekyll_note("Using QRNN (change the flag in the config of the AWD LSTM) requires to have cuda installed (same version as pytorch is using).")
path = untar_data(URLs.IMDB_SAMPLE)
data = TextLMDataBunch.from_csv(path, 'texts.csv')
learn = language_model_learner(data, AWD_LSTM, drop_mult=0.5)
show_doc(text_classifier_learner)<jupyter_output><empty_output><jupyter_text>Here again, the backbone of the model is determined by `arch` and `config`. The input texts are fed into that model by bunch of `bptt` and only the last `max_len` activations are considered. This gives us the backbone of our model. The head then consists of:- a layer that concatenates the final outputs of the RNN with the maximum and average of all the intermediate outputs (on the sequence length dimension),- blocks of ([`nn.BatchNorm1d`](https://pytorch.org/docs/stable/nn.htmltorch.nn.BatchNorm1d), [`nn.Dropout`](https://pytorch.org/docs/stable/nn.htmltorch.nn.Dropout), [`nn.Linear`](https://pytorch.org/docs/stable/nn.htmltorch.nn.Linear), [`nn.ReLU`](https://pytorch.org/docs/stable/nn.htmltorch.nn.ReLU)) layers.The blocks are defined by the `lin_ftrs` and `drops` arguments. Specifically, the first block will have a number of inputs inferred from the backbone arch and the last one will have a number of outputs equal to data.c (which contains the number of classes of the data) and the intermediate blocks have a number of inputs/outputs determined by `lin_ftrs` (of course a block has a number of inputs equal to the number of outputs of the previous block). The dropouts all have a the same value ps if you pass a float, or the corresponding values if you pass a list. Default is to have an intermediate hidden size of 50 (which makes two blocks model_activation -> 50 -> n_classes) with a dropout of 0.1.<jupyter_code>path = untar_data(URLs.IMDB_SAMPLE)
data = TextClasDataBunch.from_csv(path, 'texts.csv')
learn = text_classifier_learner(data, AWD_LSTM, drop_mult=0.5)
show_doc(RNNLearner)<jupyter_output><empty_output><jupyter_text>Handles the whole creation from 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.htmlRNNTrainer). 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.<jupyter_code>show_doc(RNNLearner.get_preds)<jupyter_output><empty_output><jupyter_text>If `ordered=True`, returns the predictions in the order of the dataset, otherwise they will be ordered by the sampler (from the longest text to the shortest). The other arguments are passed [`Learner.get_preds`](/basic_train.htmlLearner.get_preds).<jupyter_code>show_doc(TextClassificationInterpretation,title_level=3)<jupyter_output><empty_output><jupyter_text>The darker the word-shading in the below example, the more it contributes to the classification.  Results here are without any fitting.  After fitting to acceptable accuracy, this class can show you what is being used to produce the classification of a particular case.<jupyter_code>import matplotlib.cm as cm

txt_ci = TextClassificationInterpretation.from_learner(learn)
test_text = "Zombiegeddon was perhaps the GREATEST movie i have ever seen!"
txt_ci.show_intrinsic_attention(test_text,cmap=cm.Purples)<jupyter_output><empty_output><jupyter_text>You can also view the raw attention values with `.intrinsic_attention(text)`<jupyter_code>txt_ci.intrinsic_attention(test_text)[1]<jupyter_output><empty_output><jupyter_text>Create a tabulation showing the first `k` texts in top_losses along with their prediction, actual,loss, and probability of actual class. `max_len` is the maximum number of tokens displayed. If `max_len=None`, it will display all tokens.<jupyter_code>txt_ci.show_top_losses(5)<jupyter_output><empty_output><jupyter_text>Loading and saving<jupyter_code>show_doc(RNNLearner.load_encoder)
show_doc(RNNLearner.save_encoder)
show_doc(RNNLearner.load_pretrained)<jupyter_output><empty_output><jupyter_text>Opens the weights in the `wgts_fname` of `self.model_dir` and the dictionary in `itos_fname` then adapts the pretrained weights to the vocabulary of the data. The two files should be in the models directory of the `learner.path`.  Utility functions<jupyter_code>show_doc(convert_weights)<jupyter_output><empty_output><jupyter_text>Uses the dictionary `stoi_wgts` (mapping of word to id) of the weights to map them to a new dictionary `itos_new` (mapping id to word).  Get predictions<jupyter_code>show_doc(LanguageLearner, title_level=3)
show_doc(LanguageLearner.predict)<jupyter_output><empty_output><jupyter_text>If `no_unk=True` the unknown token is never picked. Words are taken randomly with the distribution of probabilities returned by the model. If `min_p` is not `None`, that value is the minimum probability to be considered in the pool of words. Lowering `temperature` will make the texts less randomized.<jupyter_code>show_doc(LanguageLearner.beam_search)<jupyter_output><empty_output><jupyter_text>Basic functions to get a model<jupyter_code>show_doc(get_language_model)
show_doc(get_text_classifier)<jupyter_output><empty_output><jupyter_text>This model uses an encoder taken from the `arch` on `config`. This encoder is fed the sequence by successive bits of size `bptt` and we only keep the last `max_seq` outputs for the pooling layers.The decoder use a concatenation of the last outputs, a `MaxPooling` of all the outputs and an `AveragePooling` of all the outputs. It then uses a list of `BatchNorm`, `Dropout`, `Linear`, `ReLU` blocks (with no `ReLU` in the last one), using a first layer size of `3*emb_sz` then following the numbers in `n_layers`. The dropouts probabilities are read in `drops`.Note that the model returns a list of three things, the actual output being the first, the two others being the intermediate hidden states before and after dropout (used by the [`RNNTrainer`](/callbacks.rnn.htmlRNNTrainer)). Most loss functions expect one output, so you should use a Callback to remove the other two if you're not using [`RNNTrainer`](/callbacks.rnn.htmlRNNTrainer).  Undocumented Methods - Methods moved below this line will intentionally be hidden  New Methods - Please document or move to the undocumented section<jupyter_code>show_doc(MultiBatchEncoder.forward)
show_doc(LanguageLearner.show_results)
show_doc(MultiBatchEncoder.concat)
show_doc(MultiBatchEncoder)
show_doc(decode_spec_tokens)
show_doc(MultiBatchEncoder.reset)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08300589390962672, 'fraction_numerical': 0.0054027504911591355, 'mean_word_length': 6.465627864344638, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 63, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '33838', 'license': 'Apache-2.0', 'n_tokens_mistral': 2550, 'n_tokens_neox': 2446, 'n_words': 885, 'path': 'docs_src/text.learner.ipynb', 'repo_name': 'gndowns/fastai', 'size': '8144'}