{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Implementation of the language models" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [], "source": [ "from fastai.gen_doc.nbdoc import *\n", "from fastai.text import * \n", "from fastai.text.models import * " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[`text.models`](/text.models.html#text.models) module fully implements the encoder for an [AWD-LSTM](https://arxiv.org/pdf/1708.02182.pdf), the [transformer model](https://arxiv.org/abs/1706.03762) and the [transformer XL model](https://arxiv.org/abs/1901.02860). They can then plugged in with a decoder to make a language model, or some classifying layers to make a text classifier." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Language model modules" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": false }, "outputs": [ { "data": { "text/markdown": [ "

class AWD_LSTM[source][test]

\n", "\n", "> AWD_LSTM(**`vocab_sz`**:`int`, **`emb_sz`**:`int`, **`n_hid`**:`int`, **`n_layers`**:`int`, **`pad_token`**:`int`=***`1`***, **`hidden_p`**:`float`=***`0.2`***, **`input_p`**:`float`=***`0.6`***, **`embed_p`**:`float`=***`0.1`***, **`weight_p`**:`float`=***`0.5`***, **`qrnn`**:`bool`=***`False`***, **`bidir`**:`bool`=***`False`***) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n", "\n", "
×

No tests found for AWD_LSTM. To contribute a test please refer to this guide and this discussion.

\n", "\n", "AWD-LSTM/QRNN inspired by https://arxiv.org/abs/1708.02182. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(AWD_LSTM, title_level=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The main idea of the article is to use a [RNN](http://www.pnas.org/content/79/8/2554) with dropout everywhere, but in an intelligent way. There is a difference with the usual dropout, which is why you’ll see a [`RNNDropout`](/text.models.awd_lstm.html#RNNDropout) module: we zero things, as is usual in dropout, but we always zero the same thing according to the sequence dimension (which is the first dimension in pytorch). This ensures consistency when updating the hidden state through the whole sentences/articles. \n", "\n", "This being given, there are a total four different dropouts in the encoder of the AWD-LSTM:\n", "\n", "- the first one, embedding dropout, is applied when we look the ids of our tokens inside the embedding matrix (to transform them from numbers to a vector of float). We zero some lines of it, so random ids are sent to a vector of zeros instead of being sent to their embedding vector. This is the `embed_p` parameter.\n", "- the second one, input dropout, is applied to the result of the embedding with dropout. We forget random pieces of the embedding matrix (but as stated in the last paragraph, the same ones in the sequence dimension). This is the `input_p` parameter.\n", "- the third one is the weight dropout. It’s the trickiest to implement as we randomly replace by 0s some weights of the hidden-to-hidden matrix inside the RNN: this needs to be done in a way that ensure the gradients are still computed and the initial weights still updated. This is the `weight_p` parameter.\n", "- the fourth one is the hidden dropout. It’s applied to the output of one of the layers of the RNN before it’s used as input of the next layer (again same coordinates are zeroed in the sequence dimension). It isn’t applied to the last output (which will get its own dropout in the decoder).This is the `hidden_p` parameter.\n", "\n", "The other attributes are `vocab_sz` for the number of tokens in your vocabulary, `emb_sz` for the embedding size, `n_hid` for the hidden size of your inner LSTMs (or QRNNs), `n_layers` the number of layers and `pad_token` for the index of an eventual padding token (1 by default in fastai).\n", "\n", "The flag `qrnn=True` replace the inner LSTMs by [QRNNs](https://arxiv.org/abs/1611.01576)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

reset[source][test]

\n", "\n", "> reset()\n", "\n", "
×

No tests found for reset. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Reset the hidden states. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(AWD_LSTM.reset)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": false }, "outputs": [ { "data": { "text/markdown": [ "

class Transformer[source][test]

\n", "\n", "> Transformer(**`vocab_sz`**:`int`, **`ctx_len`**:`int`, **`n_layers`**:`int`, **`n_heads`**:`int`, **`d_model`**:`int`, **`d_head`**:`int`, **`d_inner`**:`int`, **`resid_p`**:`float`=***`0.0`***, **`attn_p`**:`float`=***`0.0`***, **`ff_p`**:`float`=***`0.0`***, **`embed_p`**:`float`=***`0.0`***, **`bias`**:`bool`=***`True`***, **`scale`**:`bool`=***`True`***, **`act`**:[`Activation`](/text.models.transformer.html#Activation)=***``***, **`double_drop`**:`bool`=***`True`***, **`attn_cls`**:`Callable`=***`'MultiHeadAttention'`***, **`learned_pos_enc`**:`bool`=***`True`***, **`mask`**:`bool`=***`True`***) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n", "\n", "
×

No tests found for Transformer. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Transformer model: https://arxiv.org/abs/1706.03762. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(Transformer, title_level=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The main idea of this article is to use regular neural net for NLP instead of an RNN, but with lots of attention layers. Intuitively, those attention layers tell the model to pay more interest to this or that world when trying to predict its output. \n", "\n", "It starts from embeddings from `vocab_sz` (number of tokens) to `d_model` (which is basically the hidden size throughout the model), and it will look at inputs of size batch_size by `ctx_len` (for context length). We add a positional encoding to the embeddings (since a regular neural net has no idea of the order of words), either learned or coming from [`PositionalEncoding`](/text.models.transformer.html#PositionalEncoding) depending on `learned_pos_enc`. We then have a dropout of `embed_p` followed by `n_layers` blocks of [`MultiHeadAttention`](/text.models.transformer.html#MultiHeadAttention) followed by [`feed_forward`](/text.models.transformer.html#feed_forward). \n", "\n", "In the attention we use `n_heads` with each a hidden state of `d_head` (will default to `d_model//n_heads`). If `mask=True`, a mask will make sure no attention is paid to future tokens (which would be cheating when training a language model). If `scale=True`, the attention scores are scaled by a factor `1 / math.sqrt(d_head)`. A dropout of `attn_p` is applied to the attention scores, then the final result get applied a dropout of `resid_p` before being summed to the original input (residual connection before the layer norm).\n", "\n", "In feed forward, we have two linear layers from `d_model` to `d_inner` and then back. Those have `bias` if that flag is `True` and a dropout of `ff_p` is applied, after each if `double_drop=True`, or just at the end otherwise. `act` is used in the middle as a non-linearity. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": false }, "outputs": [ { "data": { "text/markdown": [ "

class TransformerXL[source][test]

\n", "\n", "> TransformerXL(**`vocab_sz`**:`int`, **`ctx_len`**:`int`, **`n_layers`**:`int`, **`n_heads`**:`int`, **`d_model`**:`int`, **`d_head`**:`int`, **`d_inner`**:`int`, **`resid_p`**:`float`=***`0.0`***, **`attn_p`**:`float`=***`0.0`***, **`ff_p`**:`float`=***`0.0`***, **`embed_p`**:`float`=***`0.0`***, **`bias`**:`bool`=***`False`***, **`scale`**:`bool`=***`True`***, **`act`**:[`Activation`](/text.models.transformer.html#Activation)=***``***, **`double_drop`**:`bool`=***`True`***, **`attn_cls`**:`Callable`=***`'MultiHeadRelativeAttention'`***, **`learned_pos_enc`**:`bool`=***`False`***, **`mask`**:`bool`=***`True`***, **`mem_len`**:`int`=***`0`***) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n", "\n", "
×

No tests found for TransformerXL. To contribute a test please refer to this guide and this discussion.

\n", "\n", "TransformerXL model: https://arxiv.org/abs/1901.02860. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(TransformerXL, title_level=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "TransformerXL is a transformer architecture with a sort of hidden state formed by the results of the intermediate layers on previous tokens. Its size is determined by `mem_len`. By using this context, those models are capable of learning longer dependencies and can also be used for faster text generation at inference: a regular transformer model would have to reexamine the whole of sequence of indexes generated so far, whereas we can feed the new tokens one by one to a transformer XL (like we do with a regular RNN)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": false }, "outputs": [ { "data": { "text/markdown": [ "

reset[source][test]

\n", "\n", "> reset()\n", "\n", "
×

No tests found for reset. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Reset the internal memory. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(TransformerXL.reset)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Decoders" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": false }, "outputs": [ { "data": { "text/markdown": [ "

class LinearDecoder[source][test]

\n", "\n", "> LinearDecoder(**`n_out`**:`int`, **`n_hid`**:`int`, **`output_p`**:`float`, **`tie_encoder`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)=***`None`***, **`bias`**:`bool`=***`True`***) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n", "\n", "
×

No tests found for LinearDecoder. To contribute a test please refer to this guide and this discussion.

\n", "\n", "To go on top of a RNNCore module and create a Language Model. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(LinearDecoder, title_level=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a the decoder to go on top of an [`RNNCore`](/text.models.awd_lstm.html#RNNCore) encoder and create a language model. `n_hid` is the dimension of the last hidden state of the encoder, `n_out` the size of the output. Dropout of `output_p` is applied. If a `tie_encoder` is passed, it will be used for the weights of the linear layer, that will have `bias` or not." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": false }, "outputs": [ { "data": { "text/markdown": [ "

class PoolingLinearClassifier[source][test]

\n", "\n", "> PoolingLinearClassifier(**`layers`**:`Collection`\\[`int`\\], **`drops`**:`Collection`\\[`float`\\]) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n", "\n", "
×

No tests found for PoolingLinearClassifier. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Create a linear classifier with pooling. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(PoolingLinearClassifier, title_level=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The last output, `MaxPooling` of all the outputs and `AvgPooling` of all the outputs are concatenated, then blocks of [`bn_drop_lin`](/layers.html#bn_drop_lin) are stacked, according to the values in [`layers`](/layers.html#layers) and `drops`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic NLP modules" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On top of the pytorch or the fastai [`layers`](/layers.html#layers), the language models use some custom layers specific to NLP." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class EmbeddingDropout[source][test]

\n", "\n", "> EmbeddingDropout(**`emb`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), **`embed_p`**:`float`) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n", "\n", "
×

No tests found for EmbeddingDropout. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Apply dropout with probabily `embed_p` to an embedding layer `emb`. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(EmbeddingDropout, title_level=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each row of the embedding matrix has a probability `embed_p` of being replaced by zeros while the others are rescaled accordingly. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[-0.7379, -1.3970, -0.4075, -0.1676, 2.0396, 3.2226, 0.7128],\n", " [-0.0000, 0.0000, 0.0000, -0.0000, -0.0000, 0.0000, 0.0000],\n", " [-3.2579, 2.2972, -1.8704, -0.4090, 2.6477, -1.5015, 0.7158],\n", " [ 2.1455, 1.0571, -0.6086, 3.5700, 2.6271, -3.1353, 0.7277],\n", " [-3.7003, -1.8846, 0.2029, -0.6839, 0.2968, -2.0199, 1.3127],\n", " [-0.0000, 0.0000, -0.0000, -0.0000, 0.0000, 0.0000, -0.0000],\n", " [-0.0051, 2.7428, 3.0068, 0.6242, 1.2747, 0.9262, 0.4070],\n", " [ 1.9312, 3.0524, -1.2806, 1.5910, -2.1789, -0.1636, -3.4924]],\n", " grad_fn=)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "enc = nn.Embedding(100, 7, padding_idx=1)\n", "enc_dp = EmbeddingDropout(enc, 0.5)\n", "tst_input = torch.randint(0,100,(8,))\n", "enc_dp(tst_input)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class RNNDropout[source][test]

\n", "\n", "> RNNDropout(**`p`**:`float`=***`0.5`***) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n", "\n", "
×

No tests found for RNNDropout. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Dropout with probability `p` that is consistent on the seq_len dimension. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(RNNDropout, title_level=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(tensor([[[-2.1156, 0.9734, 0.2428, 0.9396, 0.4072, -0.8197, 0.3718],\n", " [ 0.4838, 1.3077, -0.8239, -0.6557, 1.3938, 0.6086, -0.2622],\n", " [ 0.2372, -0.1627, 0.3117, -0.4811, -1.0841, -0.5207, -0.5131]],\n", " \n", " [[-0.6924, 0.4122, 0.2517, -1.0120, 0.6808, 0.8800, -0.7463],\n", " [-0.9498, 0.7655, 0.7471, -0.2767, 1.2155, -0.1042, -2.1443],\n", " [-1.2342, 1.9187, -0.8481, -0.4115, -1.3223, 1.4266, -1.4150]],\n", " \n", " [[ 0.1539, 0.3142, 0.2158, 1.1411, 0.1316, 0.6158, -1.5078],\n", " [-1.0177, -0.9230, 0.9994, 0.1140, 0.7432, 0.4353, 0.0096],\n", " [-0.8231, 1.0086, 1.7685, 0.3304, -0.0896, -1.0513, -1.3017]]]),\n", " tensor([[[-3.0223, 1.3905, 0.0000, 0.0000, 0.5818, -0.0000, 0.5312],\n", " [ 0.6911, 1.8681, -0.0000, -0.0000, 1.9911, 0.0000, -0.3745],\n", " [ 0.3389, -0.2324, 0.0000, -0.0000, -1.5487, -0.0000, -0.7331]],\n", " \n", " [[-0.9892, 0.5889, 0.3596, -1.4458, 0.9725, 1.2571, -0.0000],\n", " [-1.3569, 1.0936, 1.0673, -0.3953, 1.7364, -0.1489, -0.0000],\n", " [-1.7631, 2.7410, -1.2116, -0.5879, -1.8889, 2.0380, -0.0000]],\n", " \n", " [[ 0.0000, 0.4489, 0.0000, 1.6301, 0.1880, 0.8797, -2.1539],\n", " [-0.0000, -1.3186, 0.0000, 0.1628, 1.0617, 0.6218, 0.0137],\n", " [-0.0000, 1.4408, 0.0000, 0.4720, -0.1280, -1.5019, -1.8595]]]))" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dp = RNNDropout(0.3)\n", "tst_input = torch.randn(3,3,7)\n", "tst_input, dp(tst_input)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class WeightDropout[source][test]

\n", "\n", "> WeightDropout(**`module`**:[`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module), **`weight_p`**:`float`, **`layer_names`**:`StrList`=***`['weight_hh_l0']`***) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n", "\n", "
×

No tests found for WeightDropout. To contribute a test please refer to this guide and this discussion.

\n", "\n", "A module that warps another layer in which some weights will be replaced by 0 during training. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(WeightDropout, title_level=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Applies dropout of probability `weight_p` to the layers in `layer_names` of `module` in training mode. A copy of those weights is kept so that the dropout mask can change at every batch." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Parameter containing:\n", "tensor([[-0.0702, 0.5725],\n", " [-0.3910, 0.6512],\n", " [-0.2203, -0.4315],\n", " [ 0.2750, -0.2917],\n", " [-0.4890, -0.3094],\n", " [ 0.4638, -0.3807],\n", " [-0.2290, -0.6964],\n", " [ 0.1224, 0.4043]], requires_grad=True)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "module = nn.LSTM(5, 2)\n", "dp_module = WeightDropout(module, 0.4)\n", "getattr(dp_module.module, 'weight_hh_l0')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's at the beginning of a forward pass that the dropout is applied to the weights." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[-0.0000, 0.0000],\n", " [-0.6517, 0.0000],\n", " [-0.0000, -0.7191],\n", " [ 0.4583, -0.0000],\n", " [-0.0000, -0.0000],\n", " [ 0.7730, -0.6345],\n", " [-0.0000, -1.1607],\n", " [ 0.2040, 0.6739]], grad_fn=)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tst_input = torch.randn(4,20,5)\n", "h = (torch.zeros(1,20,2), torch.zeros(1,20,2))\n", "x,h = dp_module(tst_input,h)\n", "getattr(dp_module.module, 'weight_hh_l0')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class PositionalEncoding[source][test]

\n", "\n", "> PositionalEncoding(**`d`**:`int`) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n", "\n", "
×

No tests found for PositionalEncoding. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Encode the position with a sinusoid. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(PositionalEncoding, title_level=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class DecoderLayer[source][test]

\n", "\n", "> DecoderLayer(**`n_heads`**:`int`, **`d_model`**:`int`, **`d_head`**:`int`, **`d_inner`**:`int`, **`resid_p`**:`float`=***`0.0`***, **`attn_p`**:`float`=***`0.0`***, **`ff_p`**:`float`=***`0.0`***, **`bias`**:`bool`=***`True`***, **`scale`**:`bool`=***`True`***, **`act`**:[`Activation`](/text.models.transformer.html#Activation)=***``***, **`double_drop`**:`bool`=***`True`***, **`attn_cls`**:`Callable`=***`'MultiHeadAttention'`***) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n", "\n", "
×

No tests found for DecoderLayer. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Basic block of a Transformer model. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(DecoderLayer, title_level=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class MultiHeadAttention[source][test]

\n", "\n", "> MultiHeadAttention(**`n_heads`**:`int`, **`d_model`**:`int`, **`d_head`**:`int`=***`None`***, **`resid_p`**:`float`=***`0.0`***, **`attn_p`**:`float`=***`0.0`***, **`bias`**:`bool`=***`True`***, **`scale`**:`bool`=***`True`***) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n", "\n", "
×

No tests found for MultiHeadAttention. To contribute a test please refer to this guide and this discussion.

\n", "\n", "MutiHeadAttention. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(MultiHeadAttention, title_level=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class MultiHeadRelativeAttention[source][test]

\n", "\n", "> MultiHeadRelativeAttention(**`n_heads`**:`int`, **`d_model`**:`int`, **`d_head`**:`int`, **`resid_p`**:`float`=***`0.0`***, **`attn_p`**:`float`=***`0.0`***, **`bias`**:`bool`=***`True`***, **`scale`**:`bool`=***`True`***) :: [`MultiHeadAttention`](/text.models.transformer.html#MultiHeadAttention)\n", "\n", "
×

No tests found for MultiHeadRelativeAttention. To contribute a test please refer to this guide and this discussion.

\n", "\n", "MutiHeadAttention with relative positional encoding. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(MultiHeadRelativeAttention, title_level=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class SequentialRNN[source][test]

\n", "\n", "> SequentialRNN(**\\*`args`**) :: [`Sequential`](https://pytorch.org/docs/stable/nn.html#torch.nn.Sequential)\n", "\n", "
×

No tests found for SequentialRNN. To contribute a test please refer to this guide and this discussion.

\n", "\n", "A sequential module that passes the reset call to its children. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(SequentialRNN, title_level=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

reset[source][test]

\n", "\n", "> reset()\n", "\n", "
×

No tests found for reset. To contribute a test please refer to this guide and this discussion.

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(SequentialRNN.reset)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Call the `reset` function of [`self.children`](/torch_core.html#children) (if they have one)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

dropout_mask[source][test]

\n", "\n", "> dropout_mask(**`x`**:`Tensor`, **`sz`**:`Collection`\\[`int`\\], **`p`**:`float`)\n", "\n", "
×

No tests found for dropout_mask. To contribute a test please refer to this guide and this discussion.

\n", "\n", "Return a dropout mask of the same type as `x`, size `sz`, with probability `p` to cancel an element. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(dropout_mask)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[0.0000, 1.4286, 1.4286, 0.0000, 1.4286, 1.4286, 0.0000],\n", " [1.4286, 1.4286, 1.4286, 0.0000, 1.4286, 0.0000, 0.0000],\n", " [1.4286, 0.0000, 1.4286, 0.0000, 0.0000, 0.0000, 1.4286]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tst_input = torch.randn(3,3,7)\n", "dropout_mask(tst_input, (3,7), 0.3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Such a mask is then expanded in the sequence length dimension and multiplied by the input to do an [`RNNDropout`](/text.models.awd_lstm.html#RNNDropout)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

feed_forward[source][test]

\n", "\n", "> feed_forward(**`d_model`**:`int`, **`d_ff`**:`int`, **`ff_p`**:`float`=***`0.0`***, **`act`**:[`Activation`](/text.models.transformer.html#Activation)=***``***, **`double_drop`**:`bool`=***`True`***)\n", "\n", "
×

No tests found for feed_forward. To contribute a test please refer to this guide and this discussion.

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(feed_forward)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Undocumented Methods - Methods moved below this line will intentionally be hidden" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": false }, "outputs": [ { "data": { "text/markdown": [ "

forward[source][test]

\n", "\n", "> forward(**\\*`args`**:`ArgStar`)\n", "\n", "
×

No tests found for forward. To contribute a test please refer to this guide and this discussion.

\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": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(WeightDropout.forward)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": false }, "outputs": [ { "data": { "text/markdown": [ "

forward[source][test]

\n", "\n", "> forward(**`words`**:`LongTensor`, **`scale`**:`Optional`\\[`float`\\]=***`None`***) → `Tensor`\n", "\n", "
×

No tests found for forward. To contribute a test please refer to this guide and this discussion.

\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": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(EmbeddingDropout.forward)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": false }, "outputs": [ { "data": { "text/markdown": [ "

forward[source][test]

\n", "\n", "> forward(**`x`**:`Tensor`) → `Tensor`\n", "\n", "
×

No tests found for forward. To contribute a test please refer to this guide and this discussion.

\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": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(RNNDropout.forward)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

reset[source][test]

\n", "\n", "> reset()\n", "\n", "
×

No tests found for reset. To contribute a test please refer to this guide and this discussion.

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(WeightDropout.reset)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

forward[source][test]

\n", "\n", "> forward(**`input`**:`Tuple`\\[`Tensor`, `Tensor`, `Tensor`\\]) → `Tuple`\\[`Tensor`, `Tensor`, `Tensor`\\]\n", "\n", "
×

No tests found for forward. To contribute a test please refer to this guide and this discussion.

\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": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(PoolingLinearClassifier.forward)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

forward[source][test]

\n", "\n", "> forward(**`input`**:`Tuple`\\[`Tensor`, `Tensor`\\]) → `Tuple`\\[`Tensor`, `Tensor`, `Tensor`\\]\n", "\n", "
×

No tests found for forward. To contribute a test please refer to this guide and this discussion.

\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": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(LinearDecoder.forward)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## New Methods - Please document or move to the undocumented section" ] } ], "metadata": { "jekyll": { "keywords": "fastai", "summary": "Implementation of the AWD-LSTM and the RNN models", "title": "text.models" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }