docstr: Computes the loss for training DeepVAR, all inputs tensors representing
  time series have NTC layout.
  
  Parameters
  ----------
  target_dimension_indicator
      Indices of the target dimension (batch_size, target_dim)
  past_time_feat
      Dynamic features of past time series (batch_size, history_length,
      num_features)
  past_target_cdf
      Past marginal CDF transformed target values (batch_size,
      history_length, target_dim)
  past_observed_values
      Indicator whether or not the values were observed (batch_size,
      history_length, target_dim)
  past_is_pad
      Indicator whether the past target values have been padded
      (batch_size, history_length)
  future_time_feat
      Future time features (batch_size, prediction_length, num_features)
  future_target_cdf
      Future marginal CDF transformed target values (batch_size,
      prediction_length, target_dim)
  future_observed_values
      Indicator whether or not the future values were observed
      (batch_size, prediction_length, target_dim)
  
  Returns
  -------
  distr
      Loss with shape (batch_size, 1)
  likelihoods
      Likelihoods for each time step
      (batch_size, context + prediction_length, 1)
  distr_args
      Distribution arguments (context + prediction_length,
      number_of_arguments)
docstr_tok: ["Computes","the","loss","for","training","DeepVAR",",","all","inputs","tensors","representing","time","series","have","NTC","layout",".","Parameters","--","--","--","--","--","target_dimension_indicator","Indices","of","the","target","dimension","(","batch_size",",","target_dim",")","past_time_feat","Dynamic","features","of","past","time","series","(","batch_size",",","history_length",",","num_features",")","past_target_cdf","Past","marginal","CDF","transformed","target","values","(","batch_size",",","history_length",",","target_dim",")","past_observed_values","Indicator","whether","or","not","the","values","were","observed","(","batch_size",",","history_length",",","target_dim",")","past_is_pad","Indicator","whether","the","past","target","values","have","been","padded","(","batch_size",",","history_length",")","future_time_feat","Future","time","features","(","batch_size",",","prediction_length",",","num_features",")","future_target_cdf","Future","marginal","CDF","transformed","target","values","(","batch_size",",","prediction_length",",","target_dim",")","future_observed_values","Indicator","whether","or","not","the","future","values","were","observed","(","batch_size",",","prediction_length",",","target_dim",")","Returns","--","--","--","-","distr","Loss","with","shape","(","batch_size",",","1",")","likelihoods","Likelihoods","for","each","time","step","(","batch_size",",","context","+","prediction_length",",","1",")","distr_args","Distribution","arguments","(","context","+","prediction_length",",","number_of_arguments",")"]
code: def forward(
      self,
      target_dimension_indicator: torch.Tensor,
      past_time_feat: torch.Tensor,
      past_target_cdf: torch.Tensor,
      past_observed_values: torch.Tensor,
      past_is_pad: torch.Tensor,
      future_time_feat: torch.Tensor,
      future_target_cdf: torch.Tensor,
      future_observed_values: torch.Tensor,
  ) -> Tuple[torch.Tensor, ...]:
      """
      Computes the loss for training DeepVAR, all inputs tensors representing
      time series have NTC layout.
  
      Parameters
      ----------
      target_dimension_indicator
          Indices of the target dimension (batch_size, target_dim)
      past_time_feat
          Dynamic features of past time series (batch_size, history_length,
          num_features)
      past_target_cdf
          Past marginal CDF transformed target values (batch_size,
          history_length, target_dim)
      past_observed_values
          Indicator whether or not the values were observed (batch_size,
          history_length, target_dim)
      past_is_pad
          Indicator whether the past target values have been padded
          (batch_size, history_length)
      future_time_feat
          Future time features (batch_size, prediction_length, num_features)
      future_target_cdf
          Future marginal CDF transformed target values (batch_size,
          prediction_length, target_dim)
      future_observed_values
          Indicator whether or not the future values were observed
          (batch_size, prediction_length, target_dim)
  
      Returns
      -------
      distr
          Loss with shape (batch_size, 1)
      likelihoods
          Likelihoods for each time step
          (batch_size, context + prediction_length, 1)
      distr_args
          Distribution arguments (context + prediction_length,
          number_of_arguments)
      """
  
      # seq_len = self.context_length + self.prediction_length
  
      # unroll the decoder in "training mode", i.e. by providing future data
      # as well
      inputs, scale, _ = self.create_network_input(
          past_time_feat=past_time_feat,
          past_target_cdf=past_target_cdf,
          past_observed_values=past_observed_values,
          past_is_pad=past_is_pad,
          future_time_feat=future_time_feat,
          future_target_cdf=future_target_cdf,
          target_dimension_indicator=target_dimension_indicator,
      )
  
      enc_inputs = inputs[:, : self.context_length, ...]
      dec_inputs = inputs[:, self.context_length :, ...]
  
      enc_out = self.transformer.encoder(
          self.encoder_input(enc_inputs).permute(1, 0, 2)
      )
  
      dec_output = self.transformer.decoder(
          self.decoder_input(dec_inputs).permute(1, 0, 2),
          enc_out,
          tgt_mask=self.tgt_mask,
      )
  
      if self.scaling:
          self.flow.scale = scale
  
      # we sum the last axis to have the same shape for all likelihoods
      # (batch_size, subseq_length, 1)
      if self.dequantize:
          future_target_cdf += torch.rand_like(future_target_cdf)
  
      distr_args = self.distr_args(
          decoder_output=dec_output.permute(1, 0, 2)
      )
      # likelihoods = -self.flow.log_prob(target, distr_args).unsqueeze(-1)
      loss = -self.flow.log_prob(
          future_target_cdf, distr_args
      ).unsqueeze(-1)
  
      # # assert_shape(likelihoods, (-1, seq_len, 1))
  
      # past_observed_values = torch.min(
      #     past_observed_values, 1 - past_is_pad.unsqueeze(-1)
      # )
  
      # # (batch_size, subseq_length, target_dim)
      # observed_values = torch.cat(
      #     (
      #         past_observed_values[:, -self.context_length :, ...],
      #         future_observed_values,
      #     ),
      #     dim=1,
      # )
  
      # # mask the loss at one time step if one or more observations is missing
      # # in the target dimensions (batch_size, subseq_length, 1)
      # loss_weights, _ = observed_values.min(dim=-1, keepdim=True)
  
      # # assert_shape(loss_weights, (-1, seq_len, 1))
  
      # loss = weighted_average(likelihoods, weights=loss_weights, dim=1)
  
      # assert_shape(loss, (-1, -1, 1))
  
      # self.distribution = distr
  
      # return (loss.mean(), likelihoods, distr_args)
      return loss.mean(), distr_args
  
file_path: gluonts/src/gluonts/nursery/robust-mts-attack/pts/model/transformer_tempflow/transformer_tempflow_network.py
imports: from typing import List, Optional, Tuple
  import torch
  import torch.nn
  from gluonts.core.component import validated
  from pts.modules import RealNVP, MAF, FlowOutput, MeanScaler, NOPScaler
label_desc: Use image node_id 5 for calling the TransformerTempFlowTrainingNetwork obj's underlying member method code with example usage: obj.forward(target_dimension_indicator, past_time_feat, past_target_cdf, past_observed_values, past_is_pad, future_time_feat, future_target_cdf, future_observed_values) and returns: loss,  distr_args