[docs]classRNNBase(Module):__constants__=['mode','input_size','hidden_size','num_layers','bias','batch_first','dropout','bidirectional','proj_size']__jit_unused_properties__=['all_weights']mode:strinput_size:inthidden_size:intnum_layers:intbias:boolbatch_first:booldropout:floatbidirectional:boolproj_size:intdef__init__(self,mode:str,input_size:int,hidden_size:int,num_layers:int=1,bias:bool=True,batch_first:bool=False,dropout:float=0.,bidirectional:bool=False,proj_size:int=0,device=None,dtype=None)->None:factory_kwargs={'device':device,'dtype':dtype}super(RNNBase,self).__init__()self.mode=modeself.input_size=input_sizeself.hidden_size=hidden_sizeself.num_layers=num_layersself.bias=biasself.batch_first=batch_firstself.dropout=float(dropout)self.bidirectional=bidirectionalself.proj_size=proj_sizenum_directions=2ifbidirectionalelse1ifnotisinstance(dropout,numbers.Number)ornot0<=dropout<=1or \
isinstance(dropout,bool):raiseValueError("dropout should be a number in range [0, 1] ""representing the probability of an element being ""zeroed")ifdropout>0andnum_layers==1:warnings.warn("dropout 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={}".format(dropout,num_layers))ifproj_size<0:raiseValueError("proj_size should be a positive integer or zero to disable projections")ifproj_size>=hidden_size:raiseValueError("proj_size has to be smaller than hidden_size")ifmode=='LSTM':gate_size=4*hidden_sizeelifmode=='GRU':gate_size=3*hidden_sizeelifmode=='RNN_TANH':gate_size=hidden_sizeelifmode=='RNN_RELU':gate_size=hidden_sizeelse:raiseValueError("Unrecognized RNN mode: "+mode)self._flat_weights_names=[]self._all_weights=[]forlayerinrange(num_layers):fordirectioninrange(num_directions):real_hidden_size=proj_sizeifproj_size>0elsehidden_sizelayer_input_size=input_sizeiflayer==0elsereal_hidden_size*num_directionsw_ih=Parameter(torch.empty((gate_size,layer_input_size),**factory_kwargs))w_hh=Parameter(torch.empty((gate_size,real_hidden_size),**factory_kwargs))b_ih=Parameter(torch.empty(gate_size,**factory_kwargs))# Second bias vector included for CuDNN compatibility. Only one# bias vector is needed in standard definition.b_hh=Parameter(torch.empty(gate_size,**factory_kwargs))layer_params:Tuple[Tensor,...]=()ifself.proj_size==0:ifbias:layer_params=(w_ih,w_hh,b_ih,b_hh)else:layer_params=(w_ih,w_hh)else:w_hr=Parameter(torch.empty((proj_size,hidden_size),**factory_kwargs))ifbias:layer_params=(w_ih,w_hh,b_ih,b_hh,w_hr)else:layer_params=(w_ih,w_hh,w_hr)suffix='_reverse'ifdirection==1else''param_names=['weight_ih_l{}{}','weight_hh_l{}{}']ifbias:param_names+=['bias_ih_l{}{}','bias_hh_l{}{}']ifself.proj_size>0:param_names+=['weight_hr_l{}{}']param_names=[x.format(layer,suffix)forxinparam_names]forname,paraminzip(param_names,layer_params):setattr(self,name,param)self._flat_weights_names.extend(param_names)self._all_weights.append(param_names)self._flat_weights=[(lambdawn:getattr(self,wn)ifhasattr(self,wn)elseNone)(wn)forwninself._flat_weights_names]self.flatten_parameters()self.reset_parameters()def__setattr__(self,attr,value):ifhasattr(self,"_flat_weights_names")andattrinself._flat_weights_names:# keep self._flat_weights up to date if you do self.weight = ...idx=self._flat_weights_names.index(attr)self._flat_weights[idx]=valuesuper(RNNBase,self).__setattr__(attr,value)
[docs]defflatten_parameters(self)->None:"""Resets 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. """# Short-circuits if _flat_weights is only partially instantiatediflen(self._flat_weights)!=len(self._flat_weights_names):returnforwinself._flat_weights:ifnotisinstance(w,Tensor):return# Short-circuits if any tensor in self._flat_weights is not acceptable to cuDNN# or the tensors in _flat_weights are of different dtypesfirst_fw=self._flat_weights[0]dtype=first_fw.dtypeforfwinself._flat_weights:if(notisinstance(fw.data,Tensor)ornot(fw.data.dtype==dtype)ornotfw.data.is_cudaornottorch.backends.cudnn.is_acceptable(fw.data)):return# If any parameters alias, we fall back to the slower, copying code path. This is# a sufficient check, because overlapping parameter buffers that don't completely# alias would break the assumptions of the uniqueness check in# Module.named_parameters().unique_data_ptrs=set(p.data_ptr()forpinself._flat_weights)iflen(unique_data_ptrs)!=len(self._flat_weights):returnwithtorch.cuda.device_of(first_fw):importtorch.backends.cudnn.rnnasrnn# Note: no_grad() is necessary since _cudnn_rnn_flatten_weight is# an inplace operation on self._flat_weightswithtorch.no_grad():iftorch._use_cudnn_rnn_flatten_weight():num_weights=4ifself.biaselse2ifself.proj_size>0:num_weights+=1torch._cudnn_rnn_flatten_weight(self._flat_weights,num_weights,self.input_size,rnn.get_cudnn_mode(self.mode),self.hidden_size,self.proj_size,self.num_layers,self.batch_first,bool(self.bidirectional))
def_apply(self,fn):ret=super(RNNBase,self)._apply(fn)# Resets _flat_weights# Note: be v. careful before removing this, as 3rd party device types# likely rely on this behavior to properly .to() modules like LSTM.self._flat_weights=[(lambdawn:getattr(self,wn)ifhasattr(self,wn)elseNone)(wn)forwninself._flat_weights_names]# Flattens params (on CUDA)self.flatten_parameters()returnretdefreset_parameters(self)->None:stdv=1.0/math.sqrt(self.hidden_size)forweightinself.parameters():init.uniform_(weight,-stdv,stdv)defcheck_input(self,input:Tensor,batch_sizes:Optional[Tensor])->None:expected_input_dim=2ifbatch_sizesisnotNoneelse3ifinput.dim()!=expected_input_dim:raiseRuntimeError('input must have {} dimensions, got {}'.format(expected_input_dim,input.dim()))ifself.input_size!=input.size(-1):raiseRuntimeError('input.size(-1) must be equal to input_size. Expected {}, got {}'.format(self.input_size,input.size(-1)))defget_expected_hidden_size(self,input:Tensor,batch_sizes:Optional[Tensor])->Tuple[int,int,int]:ifbatch_sizesisnotNone:mini_batch=int(batch_sizes[0])else:mini_batch=input.size(0)ifself.batch_firstelseinput.size(1)num_directions=2ifself.bidirectionalelse1ifself.proj_size>0:expected_hidden_size=(self.num_layers*num_directions,mini_batch,self.proj_size)else:expected_hidden_size=(self.num_layers*num_directions,mini_batch,self.hidden_size)returnexpected_hidden_sizedefcheck_hidden_size(self,hx:Tensor,expected_hidden_size:Tuple[int,int,int],msg:str='Expected hidden size {}, got {}')->None:ifhx.size()!=expected_hidden_size:raiseRuntimeError(msg.format(expected_hidden_size,list(hx.size())))defcheck_forward_args(self,input:Tensor,hidden:Tensor,batch_sizes:Optional[Tensor]):self.check_input(input,batch_sizes)expected_hidden_size=self.get_expected_hidden_size(input,batch_sizes)self.check_hidden_size(hidden,expected_hidden_size)defpermute_hidden(self,hx:Tensor,permutation:Optional[Tensor]):ifpermutationisNone:returnhxreturnapply_permutation(hx,permutation)defforward(self,input:Union[Tensor,PackedSequence],hx:Optional[Tensor]=None)->Tuple[Union[Tensor,PackedSequence],Tensor]:is_packed=isinstance(input,PackedSequence)ifis_packed:input,batch_sizes,sorted_indices,unsorted_indices=inputmax_batch_size=int(batch_sizes[0])else:input=cast(Tensor,input)batch_sizes=Nonemax_batch_size=input.size(0)ifself.batch_firstelseinput.size(1)sorted_indices=Noneunsorted_indices=NoneifhxisNone:input=cast(Tensor,input)num_directions=2ifself.bidirectionalelse1hx=torch.zeros(self.num_layers*num_directions,max_batch_size,self.hidden_size,dtype=input.dtype,device=input.device)else:# Each batch of the hidden state should match the input sequence that# the user believes he/she is passing in.hx=self.permute_hidden(hx,sorted_indices)asserthxisnotNoneinput=cast(Tensor,input)self.check_forward_args(input,hx,batch_sizes)_impl=_rnn_impls[self.mode]ifbatch_sizesisNone:result=_impl(input,hx,self._flat_weights,self.bias,self.num_layers,self.dropout,self.training,self.bidirectional,self.batch_first)else:result=_impl(input,batch_sizes,hx,self._flat_weights,self.bias,self.num_layers,self.dropout,self.training,self.bidirectional)output:Union[Tensor,PackedSequence]output=result[0]hidden=result[1]ifis_packed:output=PackedSequence(output,batch_sizes,sorted_indices,unsorted_indices)returnoutput,self.permute_hidden(hidden,unsorted_indices)defextra_repr(self)->str:s='{input_size}, {hidden_size}'ifself.proj_size!=0:s+=', proj_size={proj_size}'ifself.num_layers!=1:s+=', num_layers={num_layers}'ifself.biasisnotTrue:s+=', bias={bias}'ifself.batch_firstisnotFalse:s+=', batch_first={batch_first}'ifself.dropout!=0:s+=', dropout={dropout}'ifself.bidirectionalisnotFalse:s+=', bidirectional={bidirectional}'returns.format(**self.__dict__)def__setstate__(self,d):super(RNNBase,self).__setstate__(d)if'all_weights'ind:self._all_weights=d['all_weights']# In PyTorch 1.8 we added a proj_size member variable to LSTM.# LSTMs that were serialized via torch.save(module) before PyTorch 1.8# don't have it, so to preserve compatibility we set proj_size here.if'proj_size'notind:self.proj_size=0ifisinstance(self._all_weights[0][0],str):returnnum_layers=self.num_layersnum_directions=2ifself.bidirectionalelse1self._flat_weights_names=[]self._all_weights=[]forlayerinrange(num_layers):fordirectioninrange(num_directions):suffix='_reverse'ifdirection==1else''weights=['weight_ih_l{}{}','weight_hh_l{}{}','bias_ih_l{}{}','bias_hh_l{}{}','weight_hr_l{}{}']weights=[x.format(layer,suffix)forxinweights]ifself.bias:ifself.proj_size>0:self._all_weights+=[weights]self._flat_weights_names.extend(weights)else:self._all_weights+=[weights[:4]]self._flat_weights_names.extend(weights[:4])else:ifself.proj_size>0:self._all_weights+=[weights[:2]]+[weights[-1:]]self._flat_weights_names.extend(weights[:2]+[weights[-1:]])else:self._all_weights+=[weights[:2]]self._flat_weights_names.extend(weights[:2])self._flat_weights=[(lambdawn:getattr(self,wn)ifhasattr(self,wn)elseNone)(wn)forwninself._flat_weights_names]@propertydefall_weights(self)->List[List[Parameter]]:return[[getattr(self,weight)forweightinweights]forweightsinself._all_weights]def_replicate_for_data_parallel(self):replica=super(RNNBase,self)._replicate_for_data_parallel()# Need to copy these caches, otherwise the replica will share the same# flat weights list.replica._flat_weights=replica._flat_weights[:]replica._flat_weights_names=replica._flat_weights_names[:]returnreplica
[docs]classRNN(RNNBase):r"""Applies 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) """def__init__(self,*args,**kwargs):if'proj_size'inkwargs:raiseValueError("proj_size argument is only supported for LSTM, not RNN or GRU")self.nonlinearity=kwargs.pop('nonlinearity','tanh')ifself.nonlinearity=='tanh':mode='RNN_TANH'elifself.nonlinearity=='relu':mode='RNN_RELU'else:raiseValueError("Unknown nonlinearity '{}'".format(self.nonlinearity))super(RNN,self).__init__(mode,*args,**kwargs)
# XXX: LSTM and GRU implementation is different from RNNBase, this is because:# 1. we want to support nn.LSTM and nn.GRU in TorchScript and TorchScript in# its current state could not support the python Union Type or Any Type# 2. TorchScript static typing does not allow a Function or Callable type in# Dict values, so we have to separately call _VF instead of using _rnn_impls# 3. This is temporary only and in the transition state that we want to make it# on time for the release## More discussion details in https://github.com/pytorch/pytorch/pull/23266## TODO: remove the overriding implementations for LSTM and GRU when TorchScript# support expressing these two modules generally.classLSTM(RNNBase):r"""Applies 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)) """def__init__(self,*args,**kwargs):super(LSTM,self).__init__('LSTM',*args,**kwargs)defget_expected_cell_size(self,input:Tensor,batch_sizes:Optional[Tensor])->Tuple[int,int,int]:ifbatch_sizesisnotNone:mini_batch=int(batch_sizes[0])else:mini_batch=input.size(0)ifself.batch_firstelseinput.size(1)num_directions=2ifself.bidirectionalelse1expected_hidden_size=(self.num_layers*num_directions,mini_batch,self.hidden_size)returnexpected_hidden_size# In the future, we should prevent mypy from applying contravariance rules here.# See torch/nn/modules/module.py::_forward_unimplementeddefcheck_forward_args(self,# type: ignore[override]input:Tensor,hidden:Tuple[Tensor,Tensor],batch_sizes:Optional[Tensor],):self.check_input(input,batch_sizes)self.check_hidden_size(hidden[0],self.get_expected_hidden_size(input,batch_sizes),'Expected hidden[0] size {}, got {}')self.check_hidden_size(hidden[1],self.get_expected_cell_size(input,batch_sizes),'Expected hidden[1] size {}, got {}')# Same as above, see torch/nn/modules/module.py::_forward_unimplementeddefpermute_hidden(self,# type: ignore[override]hx:Tuple[Tensor,Tensor],permutation:Optional[Tensor])->Tuple[Tensor,Tensor]:ifpermutationisNone:returnhxreturnapply_permutation(hx[0],permutation),apply_permutation(hx[1],permutation)# Same as above, see torch/nn/modules/module.py::_forward_unimplemented@overload# type: ignore[override]@torch._jit_internal._overload_method# noqa: F811defforward(self,input:Tensor,hx:Optional[Tuple[Tensor,Tensor]]=None)->Tuple[Tensor,Tuple[Tensor,Tensor]]:# noqa: F811pass# Same as above, see torch/nn/modules/module.py::_forward_unimplemented@overload@torch._jit_internal._overload_method# noqa: F811defforward(self,input:PackedSequence,hx:Optional[Tuple[Tensor,Tensor]]=None)->Tuple[PackedSequence,Tuple[Tensor,Tensor]]:# noqa: F811passdefforward(self,input,hx=None):# noqa: F811orig_input=input# xxx: isinstance check needs to be in conditional for TorchScript to compileifisinstance(orig_input,PackedSequence):input,batch_sizes,sorted_indices,unsorted_indices=inputmax_batch_size=batch_sizes[0]max_batch_size=int(max_batch_size)else:batch_sizes=Nonemax_batch_size=input.size(0)ifself.batch_firstelseinput.size(1)sorted_indices=Noneunsorted_indices=NoneifhxisNone:num_directions=2ifself.bidirectionalelse1real_hidden_size=self.proj_sizeifself.proj_size>0elseself.hidden_sizeh_zeros=torch.zeros(self.num_layers*num_directions,max_batch_size,real_hidden_size,dtype=input.dtype,device=input.device)c_zeros=torch.zeros(self.num_layers*num_directions,max_batch_size,self.hidden_size,dtype=input.dtype,device=input.device)hx=(h_zeros,c_zeros)else:# Each batch of the hidden state should match the input sequence that# the user believes he/she is passing in.hx=self.permute_hidden(hx,sorted_indices)self.check_forward_args(input,hx,batch_sizes)ifbatch_sizesisNone:result=_VF.lstm(input,hx,self._flat_weights,self.bias,self.num_layers,self.dropout,self.training,self.bidirectional,self.batch_first)else:result=_VF.lstm(input,batch_sizes,hx,self._flat_weights,self.bias,self.num_layers,self.dropout,self.training,self.bidirectional)output=result[0]hidden=result[1:]# xxx: isinstance check needs to be in conditional for TorchScript to compileifisinstance(orig_input,PackedSequence):output_packed=PackedSequence(output,batch_sizes,sorted_indices,unsorted_indices)returnoutput_packed,self.permute_hidden(hidden,unsorted_indices)else:returnoutput,self.permute_hidden(hidden,unsorted_indices)classGRU(RNNBase):r"""Applies 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) """def__init__(self,*args,**kwargs):if'proj_size'inkwargs:raiseValueError("proj_size argument is only supported for LSTM, not RNN or GRU")super(GRU,self).__init__('GRU',*args,**kwargs)@overload# type: ignore[override]@torch._jit_internal._overload_method# noqa: F811defforward(self,input:Tensor,hx:Optional[Tensor]=None)->Tuple[Tensor,Tensor]:# noqa: F811pass@overload@torch._jit_internal._overload_method# noqa: F811defforward(self,input:PackedSequence,hx:Optional[Tensor]=None)->Tuple[PackedSequence,Tensor]:# noqa: F811passdefforward(self,input,hx=None):# noqa: F811orig_input=input# xxx: isinstance check needs to be in conditional for TorchScript to compileifisinstance(orig_input,PackedSequence):input,batch_sizes,sorted_indices,unsorted_indices=inputmax_batch_size=batch_sizes[0]max_batch_size=int(max_batch_size)else:batch_sizes=Nonemax_batch_size=input.size(0)ifself.batch_firstelseinput.size(1)sorted_indices=Noneunsorted_indices=NoneifhxisNone:num_directions=2ifself.bidirectionalelse1hx=torch.zeros(self.num_layers*num_directions,max_batch_size,self.hidden_size,dtype=input.dtype,device=input.device)else:# Each batch of the hidden state should match the input sequence that# the user believes he/she is passing in.hx=self.permute_hidden(hx,sorted_indices)self.check_forward_args(input,hx,batch_sizes)ifbatch_sizesisNone:result=_VF.gru(input,hx,self._flat_weights,self.bias,self.num_layers,self.dropout,self.training,self.bidirectional,self.batch_first)else:result=_VF.gru(input,batch_sizes,hx,self._flat_weights,self.bias,self.num_layers,self.dropout,self.training,self.bidirectional)output=result[0]hidden=result[1]# xxx: isinstance check needs to be in conditional for TorchScript to compileifisinstance(orig_input,PackedSequence):output_packed=PackedSequence(output,batch_sizes,sorted_indices,unsorted_indices)returnoutput_packed,self.permute_hidden(hidden,unsorted_indices)else:returnoutput,self.permute_hidden(hidden,unsorted_indices)classRNNCellBase(Module):__constants__=['input_size','hidden_size','bias']input_size:inthidden_size:intbias:boolweight_ih:Tensorweight_hh:Tensor# WARNING: bias_ih and bias_hh purposely not defined here.# See https://github.com/pytorch/pytorch/issues/39670def__init__(self,input_size:int,hidden_size:int,bias:bool,num_chunks:int,device=None,dtype=None)->None:factory_kwargs={'device':device,'dtype':dtype}super(RNNCellBase,self).__init__()self.input_size=input_sizeself.hidden_size=hidden_sizeself.bias=biasself.weight_ih=Parameter(torch.empty((num_chunks*hidden_size,input_size),**factory_kwargs))self.weight_hh=Parameter(torch.empty((num_chunks*hidden_size,hidden_size),**factory_kwargs))ifbias:self.bias_ih=Parameter(torch.empty(num_chunks*hidden_size,**factory_kwargs))self.bias_hh=Parameter(torch.empty(num_chunks*hidden_size,**factory_kwargs))else:self.register_parameter('bias_ih',None)self.register_parameter('bias_hh',None)self.reset_parameters()defextra_repr(self)->str:s='{input_size}, {hidden_size}'if'bias'inself.__dict__andself.biasisnotTrue:s+=', bias={bias}'if'nonlinearity'inself.__dict__andself.nonlinearity!="tanh":s+=', nonlinearity={nonlinearity}'returns.format(**self.__dict__)defreset_parameters(self)->None:stdv=1.0/math.sqrt(self.hidden_size)forweightinself.parameters():init.uniform_(weight,-stdv,stdv)
[docs]classRNNCell(RNNCellBase):r"""An 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) """__constants__=['input_size','hidden_size','bias','nonlinearity']nonlinearity:strdef__init__(self,input_size:int,hidden_size:int,bias:bool=True,nonlinearity:str="tanh",device=None,dtype=None)->None:factory_kwargs={'device':device,'dtype':dtype}super(RNNCell,self).__init__(input_size,hidden_size,bias,num_chunks=1,**factory_kwargs)self.nonlinearity=nonlinearitydefforward(self,input:Tensor,hx:Optional[Tensor]=None)->Tensor:ifhxisNone:hx=torch.zeros(input.size(0),self.hidden_size,dtype=input.dtype,device=input.device)ifself.nonlinearity=="tanh":ret=_VF.rnn_tanh_cell(input,hx,self.weight_ih,self.weight_hh,self.bias_ih,self.bias_hh,)elifself.nonlinearity=="relu":ret=_VF.rnn_relu_cell(input,hx,self.weight_ih,self.weight_hh,self.bias_ih,self.bias_hh,)else:ret=input# TODO: remove when jit supports exception flowraiseRuntimeError("Unknown nonlinearity: {}".format(self.nonlinearity))returnret
classLSTMCell(RNNCellBase):r"""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) """def__init__(self,input_size:int,hidden_size:int,bias:bool=True,device=None,dtype=None)->None:factory_kwargs={'device':device,'dtype':dtype}super(LSTMCell,self).__init__(input_size,hidden_size,bias,num_chunks=4,**factory_kwargs)defforward(self,input:Tensor,hx:Optional[Tuple[Tensor,Tensor]]=None)->Tuple[Tensor,Tensor]:ifhxisNone:zeros=torch.zeros(input.size(0),self.hidden_size,dtype=input.dtype,device=input.device)hx=(zeros,zeros)return_VF.lstm_cell(input,hx,self.weight_ih,self.weight_hh,self.bias_ih,self.bias_hh,)classGRUCell(RNNCellBase):r"""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) """def__init__(self,input_size:int,hidden_size:int,bias:bool=True,device=None,dtype=None)->None:factory_kwargs={'device':device,'dtype':dtype}super(GRUCell,self).__init__(input_size,hidden_size,bias,num_chunks=3,**factory_kwargs)defforward(self,input:Tensor,hx:Optional[Tensor]=None)->Tensor:ifhxisNone:hx=torch.zeros(input.size(0),self.hidden_size,dtype=input.dtype,device=input.device)return_VF.gru_cell(input,hx,self.weight_ih,self.weight_hh,self.bias_ih,self.bias_hh,)
Docs
Access comprehensive developer documentation for PyTorch
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.