docstr: Recurrent Network.
  
  Simple LSTM or GRU layer followed by output layer
  
  Args:
      cell_type (str, optional): Recurrent cell type ["LSTM", "GRU"]. Defaults to "LSTM".
      hidden_size (int, optional): hidden recurrent size - the most important hyperparameter along with
          ``rnn_layers``. Defaults to 10.
      rnn_layers (int, optional): Number of RNN layers - important hyperparameter. Defaults to 2.
      dropout (float, optional): Dropout in RNN layers. Defaults to 0.1.
      static_categoricals: integer of positions of static categorical variables
      static_reals: integer of positions of static continuous variables
      time_varying_categoricals_encoder: integer of positions of categorical variables for encoder
      time_varying_categoricals_decoder: integer of positions of categorical variables for decoder
      time_varying_reals_encoder: integer of positions of continuous variables for encoder
      time_varying_reals_decoder: integer of positions of continuous variables for decoder
      categorical_groups: dictionary where values
          are list of categorical variables that are forming together a new categorical
          variable which is the key in the dictionary
      x_reals: order of continuous variables in tensor passed to forward function
      x_categoricals: order of categorical variables in tensor passed to forward function
      embedding_sizes: dictionary mapping (string) indices to tuple of number of categorical classes and
          embedding size
      embedding_paddings: list of indices for embeddings which transform the zero's embedding to a zero vector
      embedding_labels: dictionary mapping (string) indices to list of categorical labels
      output_size (Union[int, List[int]], optional): number of outputs (e.g. number of quantiles for
          QuantileLoss and one target or list of output sizes).
      target (str, optional): Target variable or list of target variables. Defaults to None.
      target_lags (Dict[str, Dict[str, int]]): dictionary of target names mapped to list of time steps by
          which the variable should be lagged.
          Lags can be useful to indicate seasonality to the models. If you know the seasonalit(ies) of your data,
          add at least the target variables with the corresponding lags to improve performance.
          Defaults to no lags, i.e. an empty dictionary.
      loss (MultiHorizonMetric, optional): loss: loss function taking prediction and targets.
      logging_metrics (nn.ModuleList, optional): Metrics to log during training.
          Defaults to nn.ModuleList([SMAPE(), MAE(), RMSE(), MAPE(), MASE()]).
docstr_tok: ["Recurrent","Network",".","Simple","LSTM","or","GRU","layer","followed","by","output","layer","Args",":","cell_type","(","str",",","optional",")",":","Recurrent","cell","type","[","``","LSTM","''",",","``","GRU","''","]",".","Defaults","to","``","LSTM","''",".","hidden_size","(","int",",","optional",")",":","hidden","recurrent","size","-","the","most","important","hyperparameter","along","with","``","rnn_layers","``",".","Defaults","to","10.","rnn_layers","(","int",",","optional",")",":","Number","of","RNN","layers","-","important","hyperparameter",".","Defaults","to","2.","dropout","(","float",",","optional",")",":","Dropout","in","RNN","layers",".","Defaults","to","0.1.","static_categoricals",":","integer","of","positions","of","static","categorical","variables","static_reals",":","integer","of","positions","of","static","continuous","variables","time_varying_categoricals_encoder",":","integer","of","positions","of","categorical","variables","for","encoder","time_varying_categoricals_decoder",":","integer","of","positions","of","categorical","variables","for","decoder","time_varying_reals_encoder",":","integer","of","positions","of","continuous","variables","for","encoder","time_varying_reals_decoder",":","integer","of","positions","of","continuous","variables","for","decoder","categorical_groups",":","dictionary","where","values","are","list","of","categorical","variables","that","are","forming","together","a","new","categorical","variable","which","is","the","key","in","the","dictionary","x_reals",":","order","of","continuous","variables","in","tensor","passed","to","forward","function","x_categoricals",":","order","of","categorical","variables","in","tensor","passed","to","forward","function","embedding_sizes",":","dictionary","mapping","(","string",")","indices","to","tuple","of","number","of","categorical","classes","and","embedding","size","embedding_paddings",":","list","of","indices","for","embeddings","which","transform","the","zero","'s","embedding","to","a","zero","vector","embedding_labels",":","dictionary","mapping","(","string",")","indices","to","list","of","categorical","labels","output_size","(","Union","[","int",",","List","[","int","]","]",",","optional",")",":","number","of","outputs","(","e.g",".","number","of","quantiles","for","QuantileLoss","and","one","target","or","list","of","output","sizes",")",".","target","(","str",",","optional",")",":","Target","variable","or","list","of","target","variables",".","Defaults","to","None",".","target_lags","(","Dict","[","str",",","Dict","[","str",",","int","]","]",")",":","dictionary","of","target","names","mapped","to","list","of","time","steps","by","which","the","variable","should","be","lagged",".","Lags","can","be","useful","to","indicate","seasonality","to","the","models",".","If","you","know","the","seasonalit","(","ies",")","of","your","data",",","add","at","least","the","target","variables","with","the","corresponding","lags","to","improve","performance",".","Defaults","to","no","lags",",","i.e",".","an","empty","dictionary",".","loss","(","MultiHorizonMetric",",","optional",")",":","loss",":","loss","function","taking","prediction","and","targets",".","logging_metrics","(","nn.ModuleList",",","optional",")",":","Metrics","to","log","during","training",".","Defaults","to","nn.ModuleList","(","[","SMAPE","(",")",",","MAE","(",")",",","RMSE","(",")",",","MAPE","(",")",",","MASE","(",")","]",")","."]
code: def __init__(
      self,
      cell_type: str = "LSTM",
      hidden_size: int = 10,
      rnn_layers: int = 2,
      dropout: float = 0.1,
      static_categoricals: List[str] = [],
      static_reals: List[str] = [],
      time_varying_categoricals_encoder: List[str] = [],
      time_varying_categoricals_decoder: List[str] = [],
      categorical_groups: Dict[str, List[str]] = {},
      time_varying_reals_encoder: List[str] = [],
      time_varying_reals_decoder: List[str] = [],
      embedding_sizes: Dict[str, Tuple[int, int]] = {},
      embedding_paddings: List[str] = [],
      embedding_labels: Dict[str, np.ndarray] = {},
      x_reals: List[str] = [],
      x_categoricals: List[str] = [],
      output_size: Union[int, List[int]] = 1,
      target: Union[str, List[str]] = None,
      target_lags: Dict[str, List[int]] = {},
      loss: MultiHorizonMetric = None,
      logging_metrics: nn.ModuleList = None,
      **kwargs,
  ):
      """
      Recurrent Network.
  
      Simple LSTM or GRU layer followed by output layer
  
      Args:
          cell_type (str, optional): Recurrent cell type ["LSTM", "GRU"]. Defaults to "LSTM".
          hidden_size (int, optional): hidden recurrent size - the most important hyperparameter along with
              ``rnn_layers``. Defaults to 10.
          rnn_layers (int, optional): Number of RNN layers - important hyperparameter. Defaults to 2.
          dropout (float, optional): Dropout in RNN layers. Defaults to 0.1.
          static_categoricals: integer of positions of static categorical variables
          static_reals: integer of positions of static continuous variables
          time_varying_categoricals_encoder: integer of positions of categorical variables for encoder
          time_varying_categoricals_decoder: integer of positions of categorical variables for decoder
          time_varying_reals_encoder: integer of positions of continuous variables for encoder
          time_varying_reals_decoder: integer of positions of continuous variables for decoder
          categorical_groups: dictionary where values
              are list of categorical variables that are forming together a new categorical
              variable which is the key in the dictionary
          x_reals: order of continuous variables in tensor passed to forward function
          x_categoricals: order of categorical variables in tensor passed to forward function
          embedding_sizes: dictionary mapping (string) indices to tuple of number of categorical classes and
              embedding size
          embedding_paddings: list of indices for embeddings which transform the zero's embedding to a zero vector
          embedding_labels: dictionary mapping (string) indices to list of categorical labels
          output_size (Union[int, List[int]], optional): number of outputs (e.g. number of quantiles for
              QuantileLoss and one target or list of output sizes).
          target (str, optional): Target variable or list of target variables. Defaults to None.
          target_lags (Dict[str, Dict[str, int]]): dictionary of target names mapped to list of time steps by
              which the variable should be lagged.
              Lags can be useful to indicate seasonality to the models. If you know the seasonalit(ies) of your data,
              add at least the target variables with the corresponding lags to improve performance.
              Defaults to no lags, i.e. an empty dictionary.
          loss (MultiHorizonMetric, optional): loss: loss function taking prediction and targets.
          logging_metrics (nn.ModuleList, optional): Metrics to log during training.
              Defaults to nn.ModuleList([SMAPE(), MAE(), RMSE(), MAPE(), MASE()]).
      """
      if loss is None:
          loss = MAE()
      if logging_metrics is None:
          logging_metrics = nn.ModuleList(
              [SMAPE(), MAE(), RMSE(), MAPE(), MASE()]
          )
      self.save_hyperparameters()
      # store loss function separately as it is a module
      super().__init__(
          loss=loss, logging_metrics=logging_metrics, **kwargs
      )
  
      self.embeddings = MultiEmbedding(
          embedding_sizes=embedding_sizes,
          embedding_paddings=embedding_paddings,
          categorical_groups=categorical_groups,
          x_categoricals=x_categoricals,
      )
  
      lagged_target_names = [
          l for lags in target_lags.values() for l in lags
      ]
      assert set(self.encoder_variables) - set(to_list(target)) - set(
          lagged_target_names
      ) == set(self.decoder_variables) - set(
          lagged_target_names
      ), "Encoder and decoder variables have to be the same apart from target variable"
      for targeti in to_list(target):
          assert (
              targeti in time_varying_reals_encoder
          ), f"target {targeti} has to be real"  # todo: remove this restriction
      assert (
          isinstance(target, str)
          and isinstance(loss, MultiHorizonMetric)
      ) or (
          isinstance(target, (list, tuple))
          and isinstance(loss, MultiLoss)
          and len(loss) == len(target)
      ), "number of targets should be equivalent to number of loss metrics"
  
      rnn_class = get_rnn(cell_type)
      cont_size = len(self.reals)
      cat_size = sum(self.embeddings.output_size.values())
      input_size = cont_size + cat_size
      self.rnn = rnn_class(
          input_size=input_size,
          hidden_size=self.hparams.hidden_size,
          num_layers=self.hparams.rnn_layers,
          dropout=self.hparams.dropout
          if self.hparams.rnn_layers > 1
          else 0,
          batch_first=True,
      )
  
      # add linear layers for argument projects
      if isinstance(target, str):  # single target
          self.output_projector = nn.Linear(
              self.hparams.hidden_size, self.hparams.output_size
          )
          assert not isinstance(
              self.loss, QuantileLoss
          ), "QuantileLoss does not work with recurrent network"
      else:  # multi target
          self.output_projector = nn.ModuleList(
              [
                  nn.Linear(self.hparams.hidden_size, size)
                  for size in self.hparams.output_size
              ]
          )
          for l in self.loss:
              assert not isinstance(
                  l, QuantileLoss
              ), "QuantileLoss does not work with recurrent network"
  
file_path: pytorch-forecasting/pytorch_forecasting/models/rnn/__init__.py
imports: from copy import copy
  from typing import Dict, List, Tuple, Union
  import numpy
  import torch
  import torch.nn
  from pytorch_forecasting.data.encoders import MultiNormalizer, NaNLabelEncoder
  from pytorch_forecasting.data.timeseries import TimeSeriesDataSet
  from pytorch_forecasting.metrics import MAE, MAPE, MASE, RMSE, SMAPE, MultiHorizonMetric, MultiLoss, QuantileLoss
  from pytorch_forecasting.models.base_model import AutoRegressiveBaseModelWithCovariates
  from pytorch_forecasting.models.nn import HiddenState, MultiEmbedding, get_rnn
  from pytorch_forecasting.utils import apply_to_list, to_list
label_desc: Use image node_id 1 to create a new RecurrentNetwork object from inherited base classes: AutoRegressiveBaseModelWithCovariates with example: obj = RecurrentNetwork(cell_type, hidden_size, rnn_layers, dropout, static_categoricals, static_reals, time_varying_categoricals_encoder, time_varying_categoricals_decoder, categorical_groups, time_varying_reals_encoder, time_varying_reals_decoder, embedding_sizes, embedding_paddings, embedding_labels, x_reals, x_categoricals, output_size, target, target_lags, loss, logging_metrics)