/usr/local/lib64/python3.6/site-packages/torch/nn/modules/__pycache__
NameSizeModeActions
activation.cpython-36.pyc476350644editdlrm
adaptive.cpython-36.pyc97250644editdlrm
batchnorm.cpython-36.pyc310390644editdlrm
channelshuffle.cpython-36.pyc19010644editdlrm
container.cpython-36.pyc279700644editdlrm
conv.cpython-36.pyc577890644editdlrm
distance.cpython-36.pyc36840644editdlrm
dropout.cpython-36.pyc103100644editdlrm
flatten.cpython-36.pyc58130644editdlrm
fold.cpython-36.pyc128070644editdlrm
instancenorm.cpython-36.pyc188080644editdlrm
lazy.cpython-36.pyc116900644editdlrm
linear.cpython-36.pyc101880644editdlrm
loss.cpython-36.pyc914650644editdlrm
module.cpython-36.pyc665300644editdlrm
normalization.cpython-36.pyc113600644editdlrm
padding.cpython-36.pyc222170644editdlrm
pixelshuffle.cpython-36.pyc43980644editdlrm
pooling.cpython-36.pyc532030644editdlrm
rnn.cpython-36.pyc446530644editdlrm
sparse.cpython-36.pyc209560644editdlrm
transformer.cpython-36.pyc207370644editdlrm
upsampling.cpython-36.pyc107800644editdlrm
utils.cpython-36.pyc25280644editdlrm
_functions.cpython-36.pyc54410644editdlrm
__init__.cpython-36.pyc52360644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/torch/nn/modules/__pycache__/rnn.cpython-36.pyc (44653B)
3 Eg@s0ddlZddlZddlZddlmZmZmZmZmZm Z ddl Z ddl m Z ddl m Z ddlmZddlmZdd lmZd d lmZejejd Zd e e ee d ddZGddde ZGdddeZGdddeZGdddeZGddde ZGdddeZGdddeZ GdddeZ!dS)!N)ListTupleOptionaloverloadUnioncast)Tensor)Module) Parameter)PackedSequence)init)_VF)RNN_TANHRNN_RELU)tensor permutationdimreturncCs |j||S)N)Z index_select)rrrr@/usr/local/lib64/python3.6/site-packages/torch/nn/modules/rnn.pyapply_permutationsrc seZdZUddddddddd g Zd gZeee e e  e  ee ed7eeeee e ee edd fdd ZfddZddddZfddZddddZeeeddddZeeeeeeefdd d!Zd8eeeeefedd#d$d%Zeeeed&d'd(Zeeed)d*d+Zd9eee feeeeee fefd,d-d.Z!edd/d0Z"fd1d2Z#e$e%e%e&dd3d4Z'fd5d6Z(Z)S):RNNBasemode input_size hidden_size num_layersbias batch_firstdropout bidirectional proj_size all_weightsr TFrN) rrrrrr r!r"r#rc s| | d} ttj|_|_|_|_|_|_t |_ |_ | _ |rZdnd} t |tj sd|ko~dkn st |trtd|dkr|dkrtjdj||| dkrtd| |krtd|d krd |}n>|d krd |}n,|d kr|}n|dkr|}n td|g_g_xt|D]xt| D]v}| dkrh| n|}dkrz|n|| }ttj||ff| }ttj||ff| }ttj|f| }ttj|f| }f}j dkr|r||||f}n||f}n6ttj| |ff| }|r,|||||f}n |||f}|dkrDdndddg}|rb|ddg7}j dkrx|dg7}fdd|D}x$t||D]\}}t||qWjj|jj|qTWqBWfddjD_j j!dS)N)devicedtyper r rzbdropout should be a number in range [0, 1] representing the probability of an element being zeroedzdropout option adds dropout after all but last recurrent layer, so non-zero dropout expects num_layers greater than 1, but got dropout={} and num_layers={}zEproj_size should be a positive integer or zero to disable projectionsz,proj_size has to be smaller than hidden_sizeLSTMGRUrrrzUnrecognized RNN mode: _reversezweight_ih_l{}{}zweight_hh_l{}{}z bias_ih_l{}{}z bias_hh_l{}{}zweight_hr_l{}{}csg|]}|jqSr)format).0x)layersuffixrr rsz$RNNBase.__init__..csg|]}fdd|qS)cst|rt|SdS)N)hasattrgetattr)wn)selfrrysz-RNNBase.__init__...r)r.r5)r6rrr2ys)"superr__init__rrrrrr floatr!r"r# isinstancenumbersNumberbool ValueErrorwarningswarnr-_flat_weights_names _all_weightsranger torchemptyzipsetattrextendappend _flat_weightsflatten_parametersreset_parameters)r6rrrrrr r!r"r#r&r'factory_kwargsnum_directionsZ gate_size directionreal_hidden_sizeZlayer_input_sizeZw_ihZw_hhZb_ihZb_hhZ layer_paramsZw_hrZ param_namesnameparam) __class__)r0r6r1rr9's   (              zRNNBase.__init__cs@t|dr*||jkr*|jj|}||j|<tt|j||dS)NrB)r3rBindexrKr8r __setattr__)r6attrvalueidx)rTrrrV~s  zRNNBase.__setattr__)rcCsZt|jt|jkrdSx|jD]}t|ts dSq W|jd}|j}xJ|jD]@}t|jt s|jj|k s|jj stj j j |j rNdSqNWt dd|jD}t|t|jkrdStj j|ddlj j j}tjftjrB|jrdnd}|jdkr|d7}tj|j||j|j|j|j|j|j|jt|j WdQRXWdQRXdS)zResets parameter data pointer so that they can use faster code paths. Right now, this works only if the module is on the GPU and cuDNN is enabled. Otherwise, it's a no-op. Nrcss|]}|jVqdS)N)Zdata_ptr)r.prrr sz-RNNBase.flatten_parameters..r)r r )lenrKrBr;rr'dataZis_cudarEbackendsZcudnnZ is_acceptablesetZcudaZ device_ofZtorch.backends.cudnn.rnnrnnZno_gradZ_use_cudnn_rnn_flatten_weightrr#Z_cudnn_rnn_flatten_weightrZget_cudnn_moderrrr r>r")r6wZfirst_fwr'fwZunique_data_ptrsr`Z num_weightsrrrrLs6         zRNNBase.flatten_parameterscs2ttj|}fddjD_j|S)Ncsg|]}fdd|qS)cst|rt|SdS)N)r3r4)r5)r6rrr7sz+RNNBase._apply...r)r.r5)r6rrr2sz"RNNBase._apply..)r8r_applyrBrKrL)r6fnret)rT)r6rrcszRNNBase._applycCs6dtj|j}x |jD]}tj|| |qWdS)Ng?)mathsqrtr parametersruniform_)r6stdvweightrrrrMszRNNBase.reset_parameters)input batch_sizesrcCs\|dk r dnd}|j|kr0tdj||j|j|jdkrXtdj|j|jddS)Nr rz%input must have {} dimensions, got {}r z?input.size(-1) must be equal to input_size. Expected {}, got {}rn)r RuntimeErrorr-rsize)r6rlrmZexpected_input_dimrrr check_inputs zRNNBase.check_inputcCsr|dk rt|d}n|jr&|jdn|jd}|jr:dnd}|jdkr\|j|||jf}n|j|||jf}|S)Nrr r )intr rpr"r#rr)r6rlrm mini_batchrOexpected_hidden_sizerrrget_expected_hidden_sizes   z RNNBase.get_expected_hidden_sizeExpected hidden size {}, got {})hxrtmsgrcCs(|j|kr$t|j|t|jdS)N)rpror-list)r6rwrtrxrrrcheck_hidden_sizes zRNNBase.check_hidden_size)rlhiddenrmcCs(|j|||j||}|j||dS)N)rqrurz)r6rlr{rmrtrrrcheck_forward_argss  zRNNBase.check_forward_args)rwrcCs|dkr |St||S)N)r)r6rwrrrrpermute_hiddenszRNNBase.permute_hidden)rlrwrc Cs^t|t}|r(|\}}}}t|d}n0tt|}d}|jrF|jdn|jd}d}d}|dkrtt|}|jrtdnd}tj |j |||j |j |j d}n |j||}|dk sttt|}|j|||t|j} |dkr| |||j|j|j |j|j|j|j } n$| ||||j|j|j |j|j|j } | d} | d} |rNt| |||} | |j| |fS)Nrr r )r'r&)r;r rrrrr rpr"rEzerosrrr'r&r}AssertionErrorr| _rnn_implsrrKrr!training) r6rlrwZ is_packedrmsorted_indicesunsorted_indicesmax_batch_sizerO_implresultoutputr{rrrforwards<          zRNNBase.forwardcCs~d}|jdkr|d7}|jdkr(|d7}|jdk r:|d7}|jdk rL|d 7}|jdkr^|d 7}|jdk rp|d 7}|jf|jS) Nz{input_size}, {hidden_size}rz, proj_size={proj_size}r z, num_layers={num_layers}Tz , bias={bias}Fz, batch_first={batch_first}z, dropout={dropout}z, bidirectional={bidirectional})r#rrr r!r"r-__dict__)r6srrr extra_reprs      zRNNBase.extra_reprcsttj|d|kr"|d_d|kr0d_tjddtrHdSj}jrXdnd}g_ g_x:t |D],x$t |D]}|dkrdnddd d d d g}fd d|D}j rjdkrj|g7_j j |n,j|ddg7_j j |ddqjdkrrj|ddg|ddg7_j j |dd|ddgqj|ddg7_j j |ddqWqtWfddj D_ dS)Nr$r#rr r r+r,zweight_ih_l{}{}zweight_hh_l{}{}z bias_ih_l{}{}z bias_hh_l{}{}zweight_hr_l{}{}csg|]}|jqSr)r-)r.r/)r0r1rrr2?sz(RNNBase.__setstate__..r)csg|]}fdd|qS)cst|rt|SdS)N)r3r4)r5)r6rrr7Nsz1RNNBase.__setstate__...r)r.r5)r6rrr2Nsrnrn)r8r __setstate__rCr#r;strrr"rBrDrrIrK)r6drrOrPweights)rT)r0r6r1rr*s:   &$zRNNBase.__setstate__csfddjDS)Ncsg|]}fdd|DqS)csg|]}t|qSr)r4)r.rk)r6rrr2Rsz2RNNBase.all_weights...r)r.r)r6rrr2Rsz'RNNBase.all_weights..)rC)r6r)r6rr$PszRNNBase.all_weightscs2tt|j}|jdd|_|jdd|_|S)N)r8r_replicate_for_data_parallelrKrB)r6Zreplica)rTrrrTsz$RNNBase._replicate_for_data_parallel)r TFr%FrNN)rv)N)*__name__ __module__ __qualname__ __constants__Z__jit_unused_properties__rrrrrrrr>rr r:r!r"r#r9rVrLrcrMrrrqrrurzr|r}rr rrrpropertyrr r$r __classcell__rr)rTrrs<   &T 0   0) &rcs eZdZdZfddZZS)RNNaFApplies a multi-layer Elman RNN with :math:`\tanh` or :math:`\text{ReLU}` non-linearity to an input sequence. For each element in the input sequence, each layer computes the following function: .. math:: h_t = \tanh(W_{ih} x_t + b_{ih} + W_{hh} h_{(t-1)} + b_{hh}) where :math:`h_t` is the hidden state at time `t`, :math:`x_t` is the input at time `t`, and :math:`h_{(t-1)}` is the hidden state of the previous layer at time `t-1` or the initial hidden state at time `0`. If :attr:`nonlinearity` is ``'relu'``, then :math:`\text{ReLU}` is used instead of :math:`\tanh`. Args: input_size: The number of expected features in the input `x` hidden_size: The number of features in the hidden state `h` num_layers: Number of recurrent layers. E.g., setting ``num_layers=2`` would mean stacking two RNNs together to form a `stacked RNN`, with the second RNN taking in outputs of the first RNN and computing the final results. Default: 1 nonlinearity: The non-linearity to use. Can be either ``'tanh'`` or ``'relu'``. Default: ``'tanh'`` bias: If ``False``, then the layer does not use bias weights `b_ih` and `b_hh`. Default: ``True`` batch_first: If ``True``, then the input and output tensors are provided as `(batch, seq, feature)` instead of `(seq, batch, feature)`. Note that this does not apply to hidden or cell states. See the Inputs/Outputs sections below for details. Default: ``False`` dropout: If non-zero, introduces a `Dropout` layer on the outputs of each RNN layer except the last layer, with dropout probability equal to :attr:`dropout`. Default: 0 bidirectional: If ``True``, becomes a bidirectional RNN. Default: ``False`` Inputs: input, h_0 * **input**: tensor of shape :math:`(L, N, H_{in})` when ``batch_first=False`` or :math:`(N, L, H_{in})` when ``batch_first=True`` containing the features of the input sequence. The input can also be a packed variable length sequence. See :func:`torch.nn.utils.rnn.pack_padded_sequence` or :func:`torch.nn.utils.rnn.pack_sequence` for details. * **h_0**: tensor of shape :math:`(D * \text{num\_layers}, N, H_{out})` containing the initial hidden state for each element in the batch. Defaults to zeros if not provided. where: .. math:: \begin{aligned} N ={} & \text{batch size} \\ L ={} & \text{sequence length} \\ D ={} & 2 \text{ if bidirectional=True otherwise } 1 \\ H_{in} ={} & \text{input\_size} \\ H_{out} ={} & \text{hidden\_size} \end{aligned} Outputs: output, h_n * **output**: tensor of shape :math:`(L, N, D * H_{out})` when ``batch_first=False`` or :math:`(N, L, D * H_{out})` when ``batch_first=True`` containing the output features `(h_t)` from the last layer of the RNN, for each `t`. If a :class:`torch.nn.utils.rnn.PackedSequence` has been given as the input, the output will also be a packed sequence. * **h_n**: tensor of shape :math:`(D * \text{num\_layers}, N, H_{out})` containing the final hidden state for each element in the batch. Attributes: weight_ih_l[k]: the learnable input-hidden weights of the k-th layer, of shape `(hidden_size, input_size)` for `k = 0`. Otherwise, the shape is `(hidden_size, num_directions * hidden_size)` weight_hh_l[k]: the learnable hidden-hidden weights of the k-th layer, of shape `(hidden_size, hidden_size)` bias_ih_l[k]: the learnable input-hidden bias of the k-th layer, of shape `(hidden_size)` bias_hh_l[k]: the learnable hidden-hidden bias of the k-th layer, of shape `(hidden_size)` .. note:: All the weights and biases are initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where :math:`k = \frac{1}{\text{hidden\_size}}` .. note:: For bidirectional RNNs, forward and backward are directions 0 and 1 respectively. Example of splitting the output layers when ``batch_first=False``: ``output.view(seq_len, batch, num_directions, hidden_size)``. .. include:: ../cudnn_rnn_determinism.rst .. include:: ../cudnn_persistent_rnn.rst Examples:: >>> rnn = nn.RNN(10, 20, 2) >>> input = torch.randn(5, 3, 10) >>> h0 = torch.randn(2, 3, 20) >>> output, hn = rnn(input, h0) csjd|krtd|jdd|_|jdkr.d}n |jdkr>d}ntdj|jtt|j|f||dS) Nr#z=proj_size argument is only supported for LSTM, not RNN or GRU nonlinearitytanhrrelurzUnknown nonlinearity '{}')r?poprr-r8rr9)r6argskwargsr)rTrrr9s  z RNN.__init__)rrr__doc__r9rrr)rTrr]s^rc seZdZdZfddZeeeeeeefdddZ eeeefeeddd Z eeefeeeeefd d d Z e e jjdeeeeefeeeeeffdddZe e jjdeeeeefeeeeeffdddZdddZZS)r(aApplies a multi-layer long short-term memory (LSTM) RNN to an input sequence. For each element in the input sequence, each layer computes the following function: .. math:: \begin{array}{ll} \\ i_t = \sigma(W_{ii} x_t + b_{ii} + W_{hi} h_{t-1} + b_{hi}) \\ f_t = \sigma(W_{if} x_t + b_{if} + W_{hf} h_{t-1} + b_{hf}) \\ g_t = \tanh(W_{ig} x_t + b_{ig} + W_{hg} h_{t-1} + b_{hg}) \\ o_t = \sigma(W_{io} x_t + b_{io} + W_{ho} h_{t-1} + b_{ho}) \\ c_t = f_t \odot c_{t-1} + i_t \odot g_t \\ h_t = o_t \odot \tanh(c_t) \\ \end{array} where :math:`h_t` is the hidden state at time `t`, :math:`c_t` is the cell state at time `t`, :math:`x_t` is the input at time `t`, :math:`h_{t-1}` is the hidden state of the layer at time `t-1` or the initial hidden state at time `0`, and :math:`i_t`, :math:`f_t`, :math:`g_t`, :math:`o_t` are the input, forget, cell, and output gates, respectively. :math:`\sigma` is the sigmoid function, and :math:`\odot` is the Hadamard product. In a multilayer LSTM, the input :math:`x^{(l)}_t` of the :math:`l` -th layer (:math:`l >= 2`) is the hidden state :math:`h^{(l-1)}_t` of the previous layer multiplied by dropout :math:`\delta^{(l-1)}_t` where each :math:`\delta^{(l-1)}_t` is a Bernoulli random variable which is :math:`0` with probability :attr:`dropout`. If ``proj_size > 0`` is specified, LSTM with projections will be used. This changes the LSTM cell in the following way. First, the dimension of :math:`h_t` will be changed from ``hidden_size`` to ``proj_size`` (dimensions of :math:`W_{hi}` will be changed accordingly). Second, the output hidden state of each layer will be multiplied by a learnable projection matrix: :math:`h_t = W_{hr}h_t`. Note that as a consequence of this, the output of LSTM network will be of different shape as well. See Inputs/Outputs sections below for exact dimensions of all variables. You can find more details in https://arxiv.org/abs/1402.1128. Args: input_size: The number of expected features in the input `x` hidden_size: The number of features in the hidden state `h` num_layers: Number of recurrent layers. E.g., setting ``num_layers=2`` would mean stacking two LSTMs together to form a `stacked LSTM`, with the second LSTM taking in outputs of the first LSTM and computing the final results. Default: 1 bias: If ``False``, then the layer does not use bias weights `b_ih` and `b_hh`. Default: ``True`` batch_first: If ``True``, then the input and output tensors are provided as `(batch, seq, feature)` instead of `(seq, batch, feature)`. Note that this does not apply to hidden or cell states. See the Inputs/Outputs sections below for details. Default: ``False`` dropout: If non-zero, introduces a `Dropout` layer on the outputs of each LSTM layer except the last layer, with dropout probability equal to :attr:`dropout`. Default: 0 bidirectional: If ``True``, becomes a bidirectional LSTM. Default: ``False`` proj_size: If ``> 0``, will use LSTM with projections of corresponding size. Default: 0 Inputs: input, (h_0, c_0) * **input**: tensor of shape :math:`(L, N, H_{in})` when ``batch_first=False`` or :math:`(N, L, H_{in})` when ``batch_first=True`` containing the features of the input sequence. The input can also be a packed variable length sequence. See :func:`torch.nn.utils.rnn.pack_padded_sequence` or :func:`torch.nn.utils.rnn.pack_sequence` for details. * **h_0**: tensor of shape :math:`(D * \text{num\_layers}, N, H_{out})` containing the initial hidden state for each element in the batch. Defaults to zeros if (h_0, c_0) is not provided. * **c_0**: tensor of shape :math:`(D * \text{num\_layers}, N, H_{cell})` containing the initial cell state for each element in the batch. Defaults to zeros if (h_0, c_0) is not provided. where: .. math:: \begin{aligned} N ={} & \text{batch size} \\ L ={} & \text{sequence length} \\ D ={} & 2 \text{ if bidirectional=True otherwise } 1 \\ H_{in} ={} & \text{input\_size} \\ H_{cell} ={} & \text{hidden\_size} \\ H_{out} ={} & \text{proj\_size if } \text{proj\_size}>0 \text{ otherwise hidden\_size} \\ \end{aligned} Outputs: output, (h_n, c_n) * **output**: tensor of shape :math:`(L, N, D * H_{out})` when ``batch_first=False`` or :math:`(N, L, D * H_{out})` when ``batch_first=True`` containing the output features `(h_t)` from the last layer of the LSTM, for each `t`. If a :class:`torch.nn.utils.rnn.PackedSequence` has been given as the input, the output will also be a packed sequence. * **h_n**: tensor of shape :math:`(D * \text{num\_layers}, N, H_{out})` containing the final hidden state for each element in the batch. * **c_n**: tensor of shape :math:`(D * \text{num\_layers}, N, H_{cell})` containing the final cell state for each element in the batch. Attributes: weight_ih_l[k] : the learnable input-hidden weights of the :math:`\text{k}^{th}` layer `(W_ii|W_if|W_ig|W_io)`, of shape `(4*hidden_size, input_size)` for `k = 0`. Otherwise, the shape is `(4*hidden_size, num_directions * hidden_size)`. If ``proj_size > 0`` was specified, the shape will be `(4*hidden_size, num_directions * proj_size)` for `k > 0` weight_hh_l[k] : the learnable hidden-hidden weights of the :math:`\text{k}^{th}` layer `(W_hi|W_hf|W_hg|W_ho)`, of shape `(4*hidden_size, hidden_size)`. If ``proj_size > 0`` was specified, the shape will be `(4*hidden_size, proj_size)`. bias_ih_l[k] : the learnable input-hidden bias of the :math:`\text{k}^{th}` layer `(b_ii|b_if|b_ig|b_io)`, of shape `(4*hidden_size)` bias_hh_l[k] : the learnable hidden-hidden bias of the :math:`\text{k}^{th}` layer `(b_hi|b_hf|b_hg|b_ho)`, of shape `(4*hidden_size)` weight_hr_l[k] : the learnable projection weights of the :math:`\text{k}^{th}` layer of shape `(proj_size, hidden_size)`. Only present when ``proj_size > 0`` was specified. weight_ih_l[k]_reverse: Analogous to `weight_ih_l[k]` for the reverse direction. Only present when ``bidirectional=True``. weight_hh_l[k]_reverse: Analogous to `weight_hh_l[k]` for the reverse direction. Only present when ``bidirectional=True``. bias_ih_l[k]_reverse: Analogous to `bias_ih_l[k]` for the reverse direction. Only present when ``bidirectional=True``. bias_hh_l[k]_reverse: Analogous to `bias_hh_l[k]` for the reverse direction. Only present when ``bidirectional=True``. weight_hr_l[k]_reverse: Analogous to `weight_hr_l[k]` for the reverse direction. Only present when ``bidirectional=True`` and ``proj_size > 0`` was specified. .. note:: All the weights and biases are initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where :math:`k = \frac{1}{\text{hidden\_size}}` .. note:: For bidirectional LSTMs, forward and backward are directions 0 and 1 respectively. Example of splitting the output layers when ``batch_first=False``: ``output.view(seq_len, batch, num_directions, hidden_size)``. .. include:: ../cudnn_rnn_determinism.rst .. include:: ../cudnn_persistent_rnn.rst Examples:: >>> rnn = nn.LSTM(10, 20, 2) >>> input = torch.randn(5, 3, 10) >>> h0 = torch.randn(2, 3, 20) >>> c0 = torch.randn(2, 3, 20) >>> output, (hn, cn) = rnn(input, (h0, c0)) cstt|jd||dS)Nr()r()r8r(r9)r6rr)rTrrr9dsz LSTM.__init__)rlrmrcCsT|dk rt|d}n|jr&|jdn|jd}|jr:dnd}|j|||jf}|S)Nrr r )rrr rpr"rr)r6rlrmrsrOrtrrrget_expected_cell_sizegs zLSTM.get_expected_cell_size)rlr{rmcCsD|j|||j|d|j||d|j|d|j||ddS)Nrz"Expected hidden[0] size {}, got {}r z"Expected hidden[1] size {}, got {})rqrzrur)r6rlr{rmrrrr|ss  zLSTM.check_forward_args)rwrrcCs(|dkr |St|d|t|d|fS)Nrr )r)r6rwrrrrr}szLSTM.permute_hiddenN)rlrwrcCsdS)Nr)r6rlrwrrrrsz LSTM.forwardcCsdS)Nr)r6rlrwrrrrsc Cs|}t|tr,|\}}}}|d}t|}n&d}|jr@|jdn|jd}d}d}|dkr|jrddnd}|jdkrx|jn|j} tj |j ||| |j |j d} tj |j |||j|j |j d} | | f}n |j ||}|j||||dkrtj|||j|j|j |j|j|j|j } n&tj||||j|j|j |j|j|j } | d} | dd}t|trxt| |||}||j ||fS| |j ||fSdS)Nrr r )r'r&)r;r rrr rpr"r#rrEr~rr'r&r}r|rZlstmrKrr!r)r6rlrw orig_inputrmrrrrOrQZh_zerosZc_zerosrrr{ output_packedrrrrs@          )N)N)N)rrrrr9rrrrrrr|r}rrE _jit_internal_overload_methodrr rrr)rTrr(s&     ""r(cseZdZdZfddZeejjd e e e e e e fdddZ eejjd e e e e e e fdddZ d d dZ ZS) r*aApplies a multi-layer gated recurrent unit (GRU) RNN to an input sequence. For each element in the input sequence, each layer computes the following function: .. math:: \begin{array}{ll} r_t = \sigma(W_{ir} x_t + b_{ir} + W_{hr} h_{(t-1)} + b_{hr}) \\ z_t = \sigma(W_{iz} x_t + b_{iz} + W_{hz} h_{(t-1)} + b_{hz}) \\ n_t = \tanh(W_{in} x_t + b_{in} + r_t * (W_{hn} h_{(t-1)}+ b_{hn})) \\ h_t = (1 - z_t) * n_t + z_t * h_{(t-1)} \end{array} where :math:`h_t` is the hidden state at time `t`, :math:`x_t` is the input at time `t`, :math:`h_{(t-1)}` is the hidden state of the layer at time `t-1` or the initial hidden state at time `0`, and :math:`r_t`, :math:`z_t`, :math:`n_t` are the reset, update, and new gates, respectively. :math:`\sigma` is the sigmoid function, and :math:`*` is the Hadamard product. In a multilayer GRU, the input :math:`x^{(l)}_t` of the :math:`l` -th layer (:math:`l >= 2`) is the hidden state :math:`h^{(l-1)}_t` of the previous layer multiplied by dropout :math:`\delta^{(l-1)}_t` where each :math:`\delta^{(l-1)}_t` is a Bernoulli random variable which is :math:`0` with probability :attr:`dropout`. Args: input_size: The number of expected features in the input `x` hidden_size: The number of features in the hidden state `h` num_layers: Number of recurrent layers. E.g., setting ``num_layers=2`` would mean stacking two GRUs together to form a `stacked GRU`, with the second GRU taking in outputs of the first GRU and computing the final results. Default: 1 bias: If ``False``, then the layer does not use bias weights `b_ih` and `b_hh`. Default: ``True`` batch_first: If ``True``, then the input and output tensors are provided as `(batch, seq, feature)` instead of `(seq, batch, feature)`. Note that this does not apply to hidden or cell states. See the Inputs/Outputs sections below for details. Default: ``False`` dropout: If non-zero, introduces a `Dropout` layer on the outputs of each GRU layer except the last layer, with dropout probability equal to :attr:`dropout`. Default: 0 bidirectional: If ``True``, becomes a bidirectional GRU. Default: ``False`` Inputs: input, h_0 * **input**: tensor of shape :math:`(L, N, H_{in})` when ``batch_first=False`` or :math:`(N, L, H_{in})` when ``batch_first=True`` containing the features of the input sequence. The input can also be a packed variable length sequence. See :func:`torch.nn.utils.rnn.pack_padded_sequence` or :func:`torch.nn.utils.rnn.pack_sequence` for details. * **h_0**: tensor of shape :math:`(D * \text{num\_layers}, N, H_{out})` containing the initial hidden state for each element in the batch. Defaults to zeros if not provided. where: .. math:: \begin{aligned} N ={} & \text{batch size} \\ L ={} & \text{sequence length} \\ D ={} & 2 \text{ if bidirectional=True otherwise } 1 \\ H_{in} ={} & \text{input\_size} \\ H_{out} ={} & \text{hidden\_size} \end{aligned} Outputs: output, h_n * **output**: tensor of shape :math:`(L, N, D * H_{out})` when ``batch_first=False`` or :math:`(N, L, D * H_{out})` when ``batch_first=True`` containing the output features `(h_t)` from the last layer of the GRU, for each `t`. If a :class:`torch.nn.utils.rnn.PackedSequence` has been given as the input, the output will also be a packed sequence. * **h_n**: tensor of shape :math:`(D * \text{num\_layers}, N, H_{out})` containing the final hidden state for each element in the batch. Attributes: weight_ih_l[k] : the learnable input-hidden weights of the :math:`\text{k}^{th}` layer (W_ir|W_iz|W_in), of shape `(3*hidden_size, input_size)` for `k = 0`. Otherwise, the shape is `(3*hidden_size, num_directions * hidden_size)` weight_hh_l[k] : the learnable hidden-hidden weights of the :math:`\text{k}^{th}` layer (W_hr|W_hz|W_hn), of shape `(3*hidden_size, hidden_size)` bias_ih_l[k] : the learnable input-hidden bias of the :math:`\text{k}^{th}` layer (b_ir|b_iz|b_in), of shape `(3*hidden_size)` bias_hh_l[k] : the learnable hidden-hidden bias of the :math:`\text{k}^{th}` layer (b_hr|b_hz|b_hn), of shape `(3*hidden_size)` .. note:: All the weights and biases are initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where :math:`k = \frac{1}{\text{hidden\_size}}` .. note:: For bidirectional GRUs, forward and backward are directions 0 and 1 respectively. Example of splitting the output layers when ``batch_first=False``: ``output.view(seq_len, batch, num_directions, hidden_size)``. .. include:: ../cudnn_persistent_rnn.rst Examples:: >>> rnn = nn.GRU(10, 20, 2) >>> input = torch.randn(5, 3, 10) >>> h0 = torch.randn(2, 3, 20) >>> output, hn = rnn(input, h0) cs*d|krtdtt|jd||dS)Nr#z=proj_size argument is only supported for LSTM, not RNN or GRUr*)r*)r?r8r*r9)r6rr)rTrrr9)sz GRU.__init__N)rlrwrcCsdS)Nr)r6rlrwrrrr.sz GRU.forwardcCsdS)Nr)r6rlrwrrrr3sc CsJ|}t|tr,|\}}}}|d}t|}n&d}|jr@|jdn|jd}d}d}|dkr|jrddnd}tj|j|||j |j |j d}n |j ||}|j ||||dkrtj|||j|j|j|j|j|j|j } n&tj||||j|j|j|j|j|j } | d} | d} t|tr6t| |||} | |j | |fS| |j | |fSdS)Nrr r )r'r&)r;r rrr rpr"rEr~rrr'r&r}r|rZgrurKrr!r) r6rlrwrrmrrrrOrrr{rrrrr8s6      )N)N)N)rrrrr9rrErrrrrrr rrr)rTrr*se $$r*csheZdZUdddgZeeee  e  d eeeeddfdd Z e dd d Zddd d ZZS) RNNCellBaserrrN)rrr num_chunksrcs||d}tt|j||_||_||_ttj|||ff||_ ttj|||ff||_ |rttj||f||_ ttj||f||_ n|j dd|j dd|jdS)N)r&r'bias_ihbias_hh)r8rr9rrrr rErF weight_ih weight_hhrrZregister_parameterrM)r6rrrrr&r'rN)rTrrr9ls   zRNNCellBase.__init__)rcCsJd}d|jkr |jdk r |d7}d|jkr<|jdkr<|d7}|jf|jS)Nz{input_size}, {hidden_size}rTz , bias={bias}rrz, nonlinearity={nonlinearity})rrrr-)r6rrrrr~s zRNNCellBase.extra_reprcCs6dtj|j}x |jD]}tj|| |qWdS)Ng?)rfrgrrhrri)r6rjrkrrrrMszRNNCellBase.reset_parameters)NN)rrrrrrrrr>rrrrr9rrrMrrr)rTrras  rcsZeZdZUdZddddgZedeeeedd fd d Z de e e e d d dZ Z S)RNNCellaAn Elman RNN cell with tanh or ReLU non-linearity. .. math:: h' = \tanh(W_{ih} x + b_{ih} + W_{hh} h + b_{hh}) If :attr:`nonlinearity` is `'relu'`, then ReLU is used in place of tanh. Args: input_size: The number of expected features in the input `x` hidden_size: The number of features in the hidden state `h` bias: If ``False``, then the layer does not use bias weights `b_ih` and `b_hh`. Default: ``True`` nonlinearity: The non-linearity to use. Can be either ``'tanh'`` or ``'relu'``. Default: ``'tanh'`` Inputs: input, hidden - **input** of shape `(batch, input_size)`: tensor containing input features - **hidden** of shape `(batch, hidden_size)`: tensor containing the initial hidden state for each element in the batch. Defaults to zero if not provided. Outputs: h' - **h'** of shape `(batch, hidden_size)`: tensor containing the next hidden state for each element in the batch Shape: - Input1: :math:`(N, H_{in})` tensor containing input features where :math:`H_{in}` = `input_size` - Input2: :math:`(N, H_{out})` tensor containing the initial hidden state for each element in the batch where :math:`H_{out}` = `hidden_size` Defaults to zero if not provided. - Output: :math:`(N, H_{out})` tensor containing the next hidden state for each element in the batch Attributes: weight_ih: the learnable input-hidden weights, of shape `(hidden_size, input_size)` weight_hh: the learnable hidden-hidden weights, of shape `(hidden_size, hidden_size)` bias_ih: the learnable input-hidden bias, of shape `(hidden_size)` bias_hh: the learnable hidden-hidden bias, of shape `(hidden_size)` .. note:: All the weights and biases are initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where :math:`k = \frac{1}{\text{hidden\_size}}` Examples:: >>> rnn = nn.RNNCell(10, 20) >>> input = torch.randn(6, 3, 10) >>> hx = torch.randn(3, 20) >>> output = [] >>> for i in range(6): hx = rnn(input[i], hx) output.append(hx) rrrrTrN)rrrrrcs4||d}tt|j|||fddi|||_dS)N)r&r'rr )r8rr9r)r6rrrrr&r'rN)rTrrr9s  zRNNCell.__init__)rlrwrcCs|dkr&tj|jd|j|j|jd}|jdkrNtj|||j |j |j |j }n<|jdkrvtj |||j |j |j |j }n|}tdj|j|S)Nr)r'r&rrzUnknown nonlinearity: {})rEr~rprr'r&rrZ rnn_tanh_cellrrrrZ rnn_relu_cellror-)r6rlrwrerrrrs   zRNNCell.forward)TrNN)N)rrrrrrrrrr>r9rrrrrr)rTrrs 8 rcsVeZdZdZd eeeddfdd Zd eee eefe eefddd Z Z S) LSTMCella A long short-term memory (LSTM) cell. .. math:: \begin{array}{ll} i = \sigma(W_{ii} x + b_{ii} + W_{hi} h + b_{hi}) \\ f = \sigma(W_{if} x + b_{if} + W_{hf} h + b_{hf}) \\ g = \tanh(W_{ig} x + b_{ig} + W_{hg} h + b_{hg}) \\ o = \sigma(W_{io} x + b_{io} + W_{ho} h + b_{ho}) \\ c' = f * c + i * g \\ h' = o * \tanh(c') \\ \end{array} where :math:`\sigma` is the sigmoid function, and :math:`*` is the Hadamard product. Args: input_size: The number of expected features in the input `x` hidden_size: The number of features in the hidden state `h` bias: If ``False``, then the layer does not use bias weights `b_ih` and `b_hh`. Default: ``True`` Inputs: input, (h_0, c_0) - **input** of shape `(batch, input_size)`: tensor containing input features - **h_0** of shape `(batch, hidden_size)`: tensor containing the initial hidden state for each element in the batch. - **c_0** of shape `(batch, hidden_size)`: tensor containing the initial cell state for each element in the batch. If `(h_0, c_0)` is not provided, both **h_0** and **c_0** default to zero. Outputs: (h_1, c_1) - **h_1** of shape `(batch, hidden_size)`: tensor containing the next hidden state for each element in the batch - **c_1** of shape `(batch, hidden_size)`: tensor containing the next cell state for each element in the batch Attributes: weight_ih: the learnable input-hidden weights, of shape `(4*hidden_size, input_size)` weight_hh: the learnable hidden-hidden weights, of shape `(4*hidden_size, hidden_size)` bias_ih: the learnable input-hidden bias, of shape `(4*hidden_size)` bias_hh: the learnable hidden-hidden bias, of shape `(4*hidden_size)` .. note:: All the weights and biases are initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where :math:`k = \frac{1}{\text{hidden\_size}}` Examples:: >>> rnn = nn.LSTMCell(10, 20) # (input_size, hidden_size) >>> input = torch.randn(2, 3, 10) # (time_steps, batch, input_size) >>> hx = torch.randn(3, 20) # (batch, hidden_size) >>> cx = torch.randn(3, 20) >>> output = [] >>> for i in range(input.size()[0]): hx, cx = rnn(input[i], (hx, cx)) output.append(hx) >>> output = torch.stack(output, dim=0) TN)rrrrcs.||d}tt|j|||fddi|dS)N)r&r'rr))r8rr9)r6rrrr&r'rN)rTrrr9"s zLSTMCell.__init__)rlrwrcCsJ|dkr.tj|jd|j|j|jd}||f}tj|||j|j |j |j S)Nr)r'r&) rEr~rprr'r&rZ lstm_cellrrrr)r6rlrwr~rrrr'szLSTMCell.forward)TNN)N) rrrrrrr>r9rrrrrrr)rTrrs<rcsFeZdZdZd eeeddfdd Zd eeeeddd Z Z S) GRUCellaP A gated recurrent unit (GRU) cell .. math:: \begin{array}{ll} r = \sigma(W_{ir} x + b_{ir} + W_{hr} h + b_{hr}) \\ z = \sigma(W_{iz} x + b_{iz} + W_{hz} h + b_{hz}) \\ n = \tanh(W_{in} x + b_{in} + r * (W_{hn} h + b_{hn})) \\ h' = (1 - z) * n + z * h \end{array} where :math:`\sigma` is the sigmoid function, and :math:`*` is the Hadamard product. Args: input_size: The number of expected features in the input `x` hidden_size: The number of features in the hidden state `h` bias: If ``False``, then the layer does not use bias weights `b_ih` and `b_hh`. Default: ``True`` Inputs: input, hidden - **input** of shape `(batch, input_size)`: tensor containing input features - **hidden** of shape `(batch, hidden_size)`: tensor containing the initial hidden state for each element in the batch. Defaults to zero if not provided. Outputs: h' - **h'** of shape `(batch, hidden_size)`: tensor containing the next hidden state for each element in the batch Shape: - Input1: :math:`(N, H_{in})` tensor containing input features where :math:`H_{in}` = `input_size` - Input2: :math:`(N, H_{out})` tensor containing the initial hidden state for each element in the batch where :math:`H_{out}` = `hidden_size` Defaults to zero if not provided. - Output: :math:`(N, H_{out})` tensor containing the next hidden state for each element in the batch Attributes: weight_ih: the learnable input-hidden weights, of shape `(3*hidden_size, input_size)` weight_hh: the learnable hidden-hidden weights, of shape `(3*hidden_size, hidden_size)` bias_ih: the learnable input-hidden bias, of shape `(3*hidden_size)` bias_hh: the learnable hidden-hidden bias, of shape `(3*hidden_size)` .. note:: All the weights and biases are initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where :math:`k = \frac{1}{\text{hidden\_size}}` Examples:: >>> rnn = nn.GRUCell(10, 20) >>> input = torch.randn(6, 3, 10) >>> hx = torch.randn(3, 20) >>> output = [] >>> for i in range(6): hx = rnn(input[i], hx) output.append(hx) TN)rrrrcs.||d}tt|j|||fddi|dS)N)r&r'rr)r8rr9)r6rrrr&r'rN)rTrrr9ps zGRUCell.__init__)rlrwrcCsB|dkr&tj|jd|j|j|jd}tj|||j|j |j |j S)Nr)r'r&) rEr~rprr'r&rZgru_cellrrrr)r6rlrwrrrrus zGRUCell.forward)TNN)N) rrrrrrr>r9rrrrrr)rTrr2s<r)r )"rfr@r<typingrrrrrrrErmoduler parameterr Z utils.rnnr r,rrZrnn_tanhZrnn_relurrrrrrr(r*rrrrrrrrs2        Gym +XN