docstr: Computes the loss for training Transformer, all inputs tensors representing time series have NTC layout.
  Parameters
  ----------
  feat_static_cat : (batch_size, num_features)
  feat_static_real: torch.Tensor,  # (batch_size, num_features)
  past_time_feat : (batch_size, history_length, num_features)
  past_target : (batch_size, history_length, *target_shape)
  past_observed_values : (batch_size, history_length, *target_shape, seq_len)
  future_time_feat : (batch_size, prediction_length, num_features)
  future_target : (batch_size, prediction_length, *target_shape)
  Returns
  -------
  Loss with shape (batch_size, context + prediction_length, 1)
docstr_tok: ["Computes","the","loss","for","training","Transformer",",","all","inputs","tensors","representing","time","series","have","NTC","layout",".","Parameters","--","--","--","--","--","feat_static_cat",":","(","batch_size",",","num_features",")","feat_static_real",":","torch.Tensor",",","#","(","batch_size",",","num_features",")","past_time_feat",":","(","batch_size",",","history_length",",","num_features",")","past_target",":","(","batch_size",",","history_length",",","*","target_shape",")","past_observed_values",":","(","batch_size",",","history_length",",","*","target_shape",",","seq_len",")","future_time_feat",":","(","batch_size",",","prediction_length",",","num_features",")","future_target",":","(","batch_size",",","prediction_length",",","*","target_shape",")","Returns","--","--","--","-","Loss","with","shape","(","batch_size",",","context","+","prediction_length",",","1",")"]
code: def forward(
      self,
      feat_static_cat: torch.Tensor,
      feat_static_real: torch.Tensor,
      past_time_feat: torch.Tensor,
      past_target: torch.Tensor,
      past_observed_values: torch.Tensor,
      future_time_feat: torch.Tensor,
      future_target: torch.Tensor,
  ) -> torch.Tensor:
      """
      Computes the loss for training Transformer, all inputs tensors representing time series have NTC layout.
      Parameters
      ----------
      feat_static_cat : (batch_size, num_features)
      feat_static_real: torch.Tensor,  # (batch_size, num_features)
      past_time_feat : (batch_size, history_length, num_features)
      past_target : (batch_size, history_length, *target_shape)
      past_observed_values : (batch_size, history_length, *target_shape, seq_len)
      future_time_feat : (batch_size, prediction_length, num_features)
      future_target : (batch_size, prediction_length, *target_shape)
      Returns
      -------
      Loss with shape (batch_size, context + prediction_length, 1)
      """
  
      # create the inputs for the encoder
      inputs, scale, _ = self.create_network_input(
          feat_static_cat=feat_static_cat,
          feat_static_real=feat_static_real,
          past_time_feat=past_time_feat,
          past_target=past_target,
          past_observed_values=past_observed_values,
          future_time_feat=future_time_feat,
          future_target=future_target,
      )
  
      enc_input = inputs[:, : self.context_length, ...]  # F.slice_axis(
      #     inputs, axis=1, begin=0, end=self.context_length
      # )
      dec_input = inputs[:, self.context_length :, ...]  # F.slice_axis(
      #     inputs, axis=1, begin=self.context_length, end=None
      # )
  
      # pass through encoder [T, B, b_model]
      enc_out = self.transformer.encoder(
          self.encoder_input(enc_input).permute(1, 0, 2)
      )
  
      # input to decoder
      dec_output = self.transformer.decoder(
          self.decoder_input(dec_input).permute(1, 0, 2),
          enc_out,  # memory
          tgt_mask=self.tgt_mask,
      )
  
      # compute loss
      distr_args = self.proj_dist_args(dec_output.permute(1, 0, 2))
      distr = self.distr_output.distribution(distr_args, scale=scale)
      loss = -distr.log_prob(future_target)
  
      return loss.mean()
  
file_path: gluonts/src/gluonts/nursery/robust-mts-attack/pts/model/transformer/transformer_network.py
imports: from typing import List, Optional, Tuple
  import torch
  import torch.nn
  from gluonts.core.component import validated
  from gluonts.torch.modules.distribution_output import DistributionOutput
  from pts.modules import MeanScaler, NOPScaler, FeatureEmbedder
label_desc: Use image node_id 1 for calling the TransformerTrainingNetwork obj's underlying member method code with example usage: obj.forward(feat_static_cat, feat_static_real, past_time_feat, past_target, past_observed_values, future_time_feat, future_target) and returns: loss