timesead.models.common.rnn ========================== .. py:module:: timesead.models.common.rnn .. autoapi-nested-parse:: Implementations of basic recurrent neural networks. Classes ------- .. autoapisummary:: timesead.models.common.rnn.RNN Module Contents --------------- .. py:class:: RNN(layer_type: str, model: str, input_dimension: int, hidden_dimensions: Union[Sequence[int], int], n_recurrent_layers: Optional[int] = None, recurrent_activation: str = 'tanh', recurrent_bias: bool = True, bidirectional: bool = False, projection_size: int = 0, dilation: Optional[Sequence[int]] = None, dropout: float = 0.0) Bases: :py:obj:`torch.nn.Module` Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool General framework of a recurrent neural network. :param layer_type: The type of recurrent layer (RNN, LSTM, GRU). :type layer_type: str :param model: Type of model (s2s, s2as, s2fh, s2mh) :type model: str :param input_dimension: Number of dimensions of the feature space of the input data. :type input_dimension: int :param hidden_dimensions: Number of dimensions of each hidden state for all recurrent layers. :type hidden_dimensions: Union[List[int], int] :param n_recurrent_layers: Number of recurrent layers, if hidden_dimensions is the same for all layers (int). :type n_recurrent_layers: Optional[int] :param recurrent_activation: Activation function to use in each recurrent layer (has to be defined in torch.nn). :type recurrent_activation: str :param recurrent_bias: Whether to use bias in the computations of the recurrent layers. :type recurrent_bias: bool :param bidirectional: Use bidirectional recurrent layers. :type bidirectional: bool :param projection_size: Parameter relevant for LSTM layers only. See pytorch docs for details. :type projection_size: int .. py:attribute:: dilation :value: None .. py:attribute:: dropout .. py:attribute:: recurrent_layers .. py:attribute:: model .. py:method:: apply_dilated_layer(layer: torch.nn.Module, inputs: Union[torch.Tensor, torch.nn.utils.rnn.PackedSequence], dilation: int, hidden: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]] = None) -> Tuple[Union[torch.Tensor, torch.nn.utils.rnn.PackedSequence], Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]] .. py:method:: forward(inputs: Union[torch.Tensor, torch.nn.utils.rnn.PackedSequence], hidden_states: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]] = None, return_hidden: bool = False) -> Union[Tuple[torch.Tensor, Ellipsis], torch.Tensor, torch.nn.utils.rnn.PackedSequence] Apply the base RNN to the sequence and return the hidden states from every step in the sequence. :param inputs: Tensor or sequence of shape (T, B, ...) :param hidden_states: Hidden states of shape (num_layers, B, ...). For LSTM this must be a tuple of two tensors. :param return_hidden: Whether the method should return hidden states of the RNN too :return: If return_hidden=False this is a tensor or PackedSequence of shape (T, B, ...), otherwise a Tuple (output, hidden)