docstr: PyTorch module implementing a Transformer to be used in `TransformerModel`.
  
  PyTorch module implementing a simple encoder-decoder transformer architecture.
  
  Parameters
  ----------
  input_size
      The dimensionality of the TimeSeries instances that will be fed to the the fit and predict functions.
  output_size
      The dimensionality of the output time series.
  nr_params
      The number of parameters of the likelihood (or 1 if no likelihood is used).
  d_model
      The number of expected features in the transformer encoder/decoder inputs.
  nhead
      The number of heads in the multiheadattention model.
  num_encoder_layers
      The number of encoder layers in the encoder.
  num_decoder_layers
      The number of decoder layers in the decoder.
  dim_feedforward
      The dimension of the feedforward network model.
  dropout
      Fraction of neurons affected by Dropout.
  activation
      The activation function of encoder/decoder intermediate layer.
  norm_type: str | nn.Module | None
      The type of LayerNorm variant to use.
  custom_encoder
      A custom transformer encoder provided by the user (default=None).
  custom_decoder
      A custom transformer decoder provided by the user (default=None).
  **kwargs
      All parameters required for :class:`darts.model.forecasting_models.PLForecastingModule` base class.
  
  Inputs
  ------
  x of shape `(batch_size, input_chunk_length, input_size)`
      Tensor containing the features of the input sequence.
  
  Outputs
  -------
  y of shape `(batch_size, output_chunk_length, target_size, nr_params)`
      Tensor containing the prediction at the last time step of the sequence.
docstr_tok: ["PyTorch","module","implementing","a","Transformer","to","be","used","in","`","TransformerModel","`",".","PyTorch","module","implementing","a","simple","encoder-decoder","transformer","architecture",".","Parameters","--","--","--","--","--","input_size","The","dimensionality","of","the","TimeSeries","instances","that","will","be","fed","to","the","the","fit","and","predict","functions",".","output_size","The","dimensionality","of","the","output","time","series",".","nr_params","The","number","of","parameters","of","the","likelihood","(","or","1","if","no","likelihood","is","used",")",".","d_model","The","number","of","expected","features","in","the","transformer","encoder\/decoder","inputs",".","nhead","The","number","of","heads","in","the","multiheadattention","model",".","num_encoder_layers","The","number","of","encoder","layers","in","the","encoder",".","num_decoder_layers","The","number","of","decoder","layers","in","the","decoder",".","dim_feedforward","The","dimension","of","the","feedforward","network","model",".","dropout","Fraction","of","neurons","affected","by","Dropout",".","activation","The","activation","function","of","encoder\/decoder","intermediate","layer",".","norm_type",":","str","|","nn.Module","|","None","The","type","of","LayerNorm","variant","to","use",".","custom_encoder","A","custom","transformer","encoder","provided","by","the","user","(","default=None",")",".","custom_decoder","A","custom","transformer","decoder","provided","by","the","user","(","default=None",")",".","*","*","kwargs","All","parameters","required","for",":","class",":","`","darts.model.forecasting_models.PLForecastingModule","`","base","class",".","Inputs","--","--","--","x","of","shape","`","(","batch_size",",","input_chunk_length",",","input_size",")","`","Tensor","containing","the","features","of","the","input","sequence",".","Outputs","--","--","--","-","y","of","shape","`","(","batch_size",",","output_chunk_length",",","target_size",",","nr_params",")","`","Tensor","containing","the","prediction","at","the","last","time","step","of","the","sequence","."]
code: def __init__(
      self,
      input_size: int,
      output_size: int,
      nr_params: int,
      d_model: int,
      nhead: int,
      num_encoder_layers: int,
      num_decoder_layers: int,
      dim_feedforward: int,
      dropout: float,
      activation: str,
      norm_type: Union[str, nn.Module, None] = None,
      custom_encoder: Optional[nn.Module] = None,
      custom_decoder: Optional[nn.Module] = None,
      **kwargs,
  ):
      """PyTorch module implementing a Transformer to be used in `TransformerModel`.
  
      PyTorch module implementing a simple encoder-decoder transformer architecture.
  
      Parameters
      ----------
      input_size
          The dimensionality of the TimeSeries instances that will be fed to the the fit and predict functions.
      output_size
          The dimensionality of the output time series.
      nr_params
          The number of parameters of the likelihood (or 1 if no likelihood is used).
      d_model
          The number of expected features in the transformer encoder/decoder inputs.
      nhead
          The number of heads in the multiheadattention model.
      num_encoder_layers
          The number of encoder layers in the encoder.
      num_decoder_layers
          The number of decoder layers in the decoder.
      dim_feedforward
          The dimension of the feedforward network model.
      dropout
          Fraction of neurons affected by Dropout.
      activation
          The activation function of encoder/decoder intermediate layer.
      norm_type: str | nn.Module | None
          The type of LayerNorm variant to use.
      custom_encoder
          A custom transformer encoder provided by the user (default=None).
      custom_decoder
          A custom transformer decoder provided by the user (default=None).
      **kwargs
          All parameters required for :class:`darts.model.forecasting_models.PLForecastingModule` base class.
  
      Inputs
      ------
      x of shape `(batch_size, input_chunk_length, input_size)`
          Tensor containing the features of the input sequence.
  
      Outputs
      -------
      y of shape `(batch_size, output_chunk_length, target_size, nr_params)`
          Tensor containing the prediction at the last time step of the sequence.
      """
  
      super().__init__(**kwargs)
  
      self.input_size = input_size
      self.target_size = output_size
      self.nr_params = nr_params
      self.target_length = self.output_chunk_length
  
      self.encoder = nn.Linear(input_size, d_model)
      self.positional_encoding = _PositionalEncoding(
          d_model, dropout, self.input_chunk_length
      )
  
      if isinstance(norm_type, str):
          try:
              self.layer_norm = getattr(layer_norm_variants, norm_type)
          except AttributeError:
              raise_log(
                  AttributeError(
                      "please provide a valid layer norm type"
                  ),
              )
      else:
          self.layer_norm = norm_type
  
      raise_if_not(activation in FFN, f"'{activation}' is not in {FFN}")
      if activation in GLU_FFN:
          raise_if(
              custom_encoder is not None or custom_decoder is not None,
              "Cannot use `custom_encoder` or `custom_decoder` along with an `activation` from "
              f"{GLU_FFN}",
              logger=logger,
          )
          # use glu variant feed-forward layers
          ffn_cls = getattr(glu_variants, activation)
  
          # custom feed-forward layers have activation built-in. reset activation
          activation = None
  
          custom_encoder = _generate_coder(
              d_model,
              dim_feedforward,
              dropout,
              nhead,
              num_encoder_layers,
              self.layer_norm if self.layer_norm else nn.LayerNorm,
              coder_cls=nn.TransformerEncoder,
              layer_cls=CustomFeedForwardEncoderLayer,
              ffn_cls=ffn_cls,
          )
  
          custom_decoder = _generate_coder(
              d_model,
              dim_feedforward,
              dropout,
              nhead,
              num_decoder_layers,
              self.layer_norm if self.layer_norm else nn.LayerNorm,
              coder_cls=nn.TransformerDecoder,
              layer_cls=CustomFeedForwardDecoderLayer,
              ffn_cls=ffn_cls,
          )
  
      # if layer norm set and no GLU variant is used
      if self.layer_norm and custom_decoder is None:
          custom_encoder = _generate_coder(
              d_model,
              dim_feedforward,
              dropout,
              nhead,
              num_encoder_layers,
              self.layer_norm,
              coder_cls=nn.TransformerEncoder,
              layer_cls=nn.TransformerEncoderLayer,
              ffn_cls=None,
          )
  
          custom_decoder = _generate_coder(
              d_model,
              dim_feedforward,
              dropout,
              nhead,
              num_decoder_layers,
              self.layer_norm,
              coder_cls=nn.TransformerDecoder,
              layer_cls=nn.TransformerDecoderLayer,
              ffn_cls=None,
          )
  
      # Defining the Transformer module
      self.transformer = nn.Transformer(
          d_model=d_model,
          nhead=nhead,
          num_encoder_layers=num_encoder_layers,
          num_decoder_layers=num_decoder_layers,
          dim_feedforward=dim_feedforward,
          dropout=dropout,
          activation=activation,
          custom_encoder=custom_encoder,
          custom_decoder=custom_decoder,
      )
  
      self.decoder = nn.Linear(
          d_model,
          self.target_length * self.target_size * self.nr_params,
      )
  
imports: import math
  from typing import Optional, Tuple, Union
  import torch
  import torch.nn
  from darts.logging import get_logger, raise_if, raise_if_not, raise_log
  from darts.models.components import glu_variants, layer_norm_variants
  from darts.models.components.glu_variants import GLU_FFN
  from darts.models.components.transformer import CustomFeedForwardDecoderLayer, CustomFeedForwardEncoderLayer
  from darts.models.forecasting.pl_forecasting_module import PLPastCovariatesModule, io_processor
  from darts.models.forecasting.torch_forecasting_model import PastCovariatesTorchModel
label_desc: Use image node_id 1 to create a new _TransformerModule object from inherited base classes: PLPastCovariatesModule with example: obj = _TransformerModule(input_size, output_size, nr_params, d_model, nhead, num_encoder_layers, num_decoder_layers, dim_feedforward, dropout, activation, norm_type, custom_encoder, custom_decoder)