{ "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 [AWD-LSTM](https://arxiv.org/pdf/1708.02182.pdf) from Stephen Merity et al. 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.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 five different dropouts in 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.\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).\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.\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). This one isn’t applied to the last output, but rather…\n", "- the fifth one is the output dropout, it’s applied to the last output of the model (and like the others, it’s applied the same way through the first dimension)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic functions to get a model" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

get_language_model[source]

\n", "\n", "> get_language_model(`vocab_sz`:`int`, `emb_sz`:`int`, `n_hid`:`int`, `n_layers`:`int`, `pad_token`:`int`, `tie_weights`:`bool`=`True`, `qrnn`:`bool`=`False`, `bias`:`bool`=`True`, `bidir`:`bool`=`False`, `output_p`:`float`=`0.4`, `hidden_p`:`float`=`0.2`, `input_p`:`float`=`0.6`, `embed_p`:`float`=`0.1`, `weight_p`:`float`=`0.5`) → [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n", "\n", "Create a full AWD-LSTM. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(get_language_model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first embedding of `vocab_sz` by `emb_sz`, a hidden size of `n_hid`, RNNs with `n_layers` that can be bidirectional if `bidir` is True. The last RNN as an output size of `emb_sz` so that we can use the same decoder as the encoder if `tie_weights` is True. The decoder is a `Linear` layer with or without `bias`. If `qrnn` is set to True, we use [QRNN cells] instead of LSTMS. `pad_token` is the token used for padding.\n", "\n", "`embed_p` is used for the embedding dropout, `input_p` is used for the input dropout, `weight_p` is used for the weight dropout, `hidden_p` is used for the hidden dropout and `output_p` is used for the output dropout.\n", "\n", "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.html#RNNTrainer)). 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.html#RNNTrainer)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

get_rnn_classifier[source]

\n", "\n", "> get_rnn_classifier(`bptt`:`int`, `max_seq`:`int`, `vocab_sz`:`int`, `emb_sz`:`int`, `n_hid`:`int`, `n_layers`:`int`, `pad_token`:`int`, `layers`:`Collection`\\[`int`\\], `drops`:`Collection`\\[`float`\\], `bidir`:`bool`=`False`, `qrnn`:`bool`=`False`, `hidden_p`:`float`=`0.2`, `input_p`:`float`=`0.6`, `embed_p`:`float`=`0.1`, `weight_p`:`float`=`0.5`) → [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n", "\n", "Create a RNN classifier model. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(get_rnn_classifier)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This model uses an encoder taken from an AWD-LSTM with arguments `vocab_sz`, `emb_sz`, `n_hid`, `n_layers`, `bias`, `bidir`, `qrnn`, `pad_token` and the dropouts parameters. 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.\n", "\n", "The decoder use a concatenation of the last outputs, a `MaxPooling` of all the ouputs 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 follwoing the numbers in `n_layers`. The dropouts probabilities are read in `drops`.\n", "\n", "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.html#RNNTrainer)). 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.html#RNNTrainer)." ] }, { "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]

\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", "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.5721, -2.2245, -3.1669, -0.3286, -1.3392, -1.3890, 1.3677],\n", " [ 1.9181, 0.8162, 0.0547, -1.1909, 1.8688, -1.0324, 2.9438],\n", " [-1.1319, 0.4245, 6.3649, -2.0573, -0.0647, -0.1660, -0.8208],\n", " [-0.0000, 0.0000, 0.0000, -0.0000, 0.0000, 0.0000, -0.0000],\n", " [-0.0000, 0.0000, 0.0000, -0.0000, 0.0000, 0.0000, -0.0000],\n", " [ 1.9181, 0.8162, 0.0547, -1.1909, 1.8688, -1.0324, 2.9438],\n", " [-0.0000, 0.0000, 0.0000, -0.0000, 0.0000, 0.0000, -0.0000],\n", " [ 0.4246, 1.7266, -0.3707, 2.8732, -1.4541, 0.6501, 3.0350]],\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]

\n", "\n", "> RNNDropout(`p`:`float`=`0.5`) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\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([[[-1.3750, 0.0598, 0.5507, -0.1219, -1.4071, 0.5813, 0.9757],\n", " [-0.2612, -2.2168, -0.3012, -0.4310, -1.3489, 0.9916, 1.1717],\n", " [-1.7778, -0.7739, -2.2230, 0.5438, -0.2032, 0.7374, 1.1300]],\n", " \n", " [[-1.9824, -1.6155, -0.1078, -2.2462, -0.5045, -0.5635, 0.5041],\n", " [ 0.3810, 0.7194, 0.7611, 0.9812, 1.0620, 0.9317, 0.3176],\n", " [-1.8882, -0.0156, -1.4240, -0.0359, 0.6856, 0.0072, -0.6026]],\n", " \n", " [[-0.3039, -0.5425, -1.2921, -1.1725, -0.2109, 0.2727, -0.6178],\n", " [ 1.5460, 0.5858, -0.3476, -0.5885, -0.5179, 0.1737, -0.1857],\n", " [-0.1227, 0.1517, 0.1305, -0.4547, -0.8123, 0.0917, 0.1694]]]),\n", " tensor([[[-1.9642, 0.0000, 0.7867, -0.0000, -2.0101, 0.0000, 1.3939],\n", " [-0.3732, -0.0000, -0.4303, -0.0000, -1.9269, 0.0000, 1.6738],\n", " [-2.5398, -0.0000, -3.1757, 0.0000, -0.2903, 0.0000, 1.6143]],\n", " \n", " [[-2.8320, -0.0000, -0.0000, -3.2089, -0.0000, -0.8050, 0.7201],\n", " [ 0.5443, 0.0000, 0.0000, 1.4017, 0.0000, 1.3310, 0.4538],\n", " [-2.6975, -0.0000, -0.0000, -0.0513, 0.0000, 0.0104, -0.8609]],\n", " \n", " [[-0.0000, -0.7749, -1.8458, -1.6750, -0.3014, 0.0000, -0.0000],\n", " [ 0.0000, 0.8369, -0.4966, -0.8407, -0.7398, 0.0000, -0.0000],\n", " [-0.0000, 0.2167, 0.1864, -0.6496, -1.1605, 0.0000, 0.0000]]]))" ] }, "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]

\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", "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.0712, -0.6369],\n", " [-0.3654, 0.4196],\n", " [-0.6829, 0.6955],\n", " [ 0.6683, -0.4114],\n", " [ 0.5502, -0.1464],\n", " [-0.2557, -0.4861],\n", " [-0.4205, -0.2314],\n", " [ 0.4531, 0.3012]], 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.1186, -1.0615],\n", " [-0.0000, 0.6993],\n", " [-1.1382, 1.1591],\n", " [ 1.1138, -0.6856],\n", " [ 0.0000, -0.2439],\n", " [-0.0000, -0.0000],\n", " [-0.0000, -0.3857],\n", " [ 0.7551, 0.5020]], 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 SequentialRNN[source]

\n", "\n", "> SequentialRNN(`args`) :: [`Sequential`](https://pytorch.org/docs/stable/nn.html#torch.nn.Sequential)\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]

\n", "\n", "> reset()" ], "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]

\n", "\n", "> dropout_mask(`x`:`Tensor`, `sz`:`Collection`\\[`int`\\], `p`:`float`)\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([[1.4286, 0.0000, 1.4286, 1.4286, 1.4286, 1.4286, 1.4286],\n", " [0.0000, 1.4286, 1.4286, 1.4286, 0.0000, 1.4286, 0.0000],\n", " [0.0000, 1.4286, 1.4286, 0.0000, 1.4286, 1.4286, 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.html#RNNDropout)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Language model modules" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class RNNCore[source]

\n", "\n", "> RNNCore(`vocab_sz`:`int`, `emb_sz`:`int`, `n_hid`:`int`, `n_layers`:`int`, `pad_token`:`int`, `bidir`:`bool`=`False`, `hidden_p`:`float`=`0.2`, `input_p`:`float`=`0.6`, `embed_p`:`float`=`0.1`, `weight_p`:`float`=`0.5`, `qrnn`:`bool`=`False`) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\n", "\n", "AWD-LSTM/QRNN inspired by https://arxiv.org/abs/1708.02182. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(RNNCore, title_level=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the encoder of the model with an embedding layer of `vocab_sz` by `emb_sz`, a hidden size of `n_hid`, `n_layers` layers. `pad_token` is passed to the `Embedding`, if `bidir` is True, the model is bidirectional. If `qrnn` is True, we use QRNN cells instead of LSTMs. Dropouts are `embed_p`, `input_p`, `weight_p` and `hidden_p`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

reset[source]

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

class LinearDecoder[source]

\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", "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.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": "markdown", "metadata": {}, "source": [ "## Classifier modules" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class MultiBatchRNNCore[source]

\n", "\n", "> MultiBatchRNNCore(`bptt`:`int`, `max_seq`:`int`, `args`, `kwargs`) :: [`RNNCore`](/text.models.html#RNNCore)\n", "\n", "Create a RNNCore module that can process a full sentence. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(MultiBatchRNNCore, title_level=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Text is passed by chunks of sequence length `bptt` and only the last `max_seq` outputs are kept for the next layer. `args` and `kwargs` are passed to the [`RNNCore`](/text.models.html#RNNCore)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

concat[source]

\n", "\n", "> concat(`arrs`:`Collection`\\[`Tensor`\\]) → `Tensor`\n", "\n", "Concatenate the `arrs` along the batch dimension. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(MultiBatchRNNCore.concat)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

class PoolingLinearClassifier[source]

\n", "\n", "> PoolingLinearClassifier(`layers`:`Collection`\\[`int`\\], `drops`:`Collection`\\[`float`\\]) :: [`Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)\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": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

pool[source]

\n", "\n", "> pool(`x`:`Tensor`, `bs`:`int`, `is_max`:`bool`)\n", "\n", "Pool the tensor along the seq_len dimension. " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(PoolingLinearClassifier.pool)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The input tensor `x` (of batch size `bs`) is pooled along the batch dimension. `is_max` decides if we do an `AvgPooling` or a `MaxPooling`." ] }, { "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": true }, "outputs": [ { "data": { "text/markdown": [ "

forward[source]

\n", "\n", "> forward(`args`:`ArgStar`)\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": true }, "outputs": [ { "data": { "text/markdown": [ "

forward[source]

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

forward[source]

\n", "\n", "> forward(`words`:`LongTensor`, `scale`:`Optional`\\[`float`\\]=`None`) → `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": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(EmbeddingDropout.forward)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "

forward[source]

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

reset[source]

\n", "\n", "> reset()" ], "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]

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

forward[source]

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

forward[source]

\n", "\n", "> forward(`input`:`Tuple`\\[`Tensor`, `Tensor`\\]) → `Tuple`\\[`Tensor`, `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": [ "" ] }, "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 }