docstr: Computes the loss for training DeepAR, all inputs tensors representing
  time series have NTC layout.
  
  Parameters
  ----------
  F
  feat_static_cat : (batch_size, num_features)
  feat_static_real : (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)
  future_observed_values : (batch_size, prediction_length, *target_shape)
  
  Returns loss with shape (batch_size, context + prediction_length, 1)
  -------
docstr_tok: ["Computes","the","loss","for","training","DeepAR",",","all","inputs","tensors","representing","time","series","have","NTC","layout",".","Parameters","--","--","--","--","--","F","feat_static_cat",":","(","batch_size",",","num_features",")","feat_static_real",":","(","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",")","future_observed_values",":","(","batch_size",",","prediction_length",",","*","target_shape",")","Returns","loss","with","shape","(","batch_size",",","context","+","prediction_length",",","1",")","--","--","--","-"]
code: def hybrid_forward(
      self,
      F,
      feat_static_cat: Tensor,
      feat_static_real: Tensor,
      past_time_feat: Tensor,
      past_target: Tensor,
      past_observed_values: Tensor,
      past_is_pad: Optional[Tensor],
      future_time_feat: Tensor,
      future_target: Tensor,
      future_observed_values: Tensor,
  ) -> Tensor:
      """
      Computes the loss for training DeepAR, all inputs tensors representing
      time series have NTC layout.
  
      Parameters
      ----------
      F
      feat_static_cat : (batch_size, num_features)
      feat_static_real : (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)
      future_observed_values : (batch_size, prediction_length, *target_shape)
  
      Returns loss with shape (batch_size, context + prediction_length, 1)
      -------
      """
  
      outputs = self.distribution(
          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,
          past_is_pad=past_is_pad,
          future_time_feat=future_time_feat,
          future_target=future_target,
          future_observed_values=future_observed_values,
          return_rnn_outputs=True,
      )
      # since return_rnn_outputs=True, assert:
      assert isinstance(outputs, tuple)
      distr, rnn_outputs = outputs
  
      # put together target sequence
      # (batch_size, seq_len, *target_shape)
      target = F.concat(
          past_target.slice_axis(
              axis=1,
              begin=self.history_length - self.context_length,
              end=None,
          ),
          future_target,
          dim=1,
      )
  
      # (batch_size, seq_len)
      loss = distr.loss(target)
  
      # (batch_size, seq_len, *target_shape)
      observed_values = F.concat(
          past_observed_values.slice_axis(
              axis=1,
              begin=self.history_length - self.context_length,
              end=self.history_length,
          ),
          future_observed_values,
          dim=1,
      )
  
      # mask the loss at one time step iff one or more observations is
      # missing in the target dimensions (batch_size, seq_len)
      loss_weights = (
          observed_values
          if (len(self.target_shape) == 0)
          else observed_values.min(axis=-1, keepdims=False)
      )
  
      weighted_loss = weighted_average(
          F=F,
          x=loss,
          weights=loss_weights,
          axis=1,
          include_zeros_in_denominator=self.include_zeros_in_denominator,
      )
  
      # need to mask possible nans and -inf
      loss = F.where(
          condition=loss_weights, x=loss, y=F.zeros_like(loss)
      )
  
      # rnn_outputs is already merged into a single tensor
      assert not isinstance(rnn_outputs, list)
      # it seems that the trainer only uses the first return value for
      # backward so we only add regularization to weighted_loss
      if self.alpha:
          ar_loss = self.ar_loss(rnn_outputs)
          weighted_loss = weighted_loss + ar_loss
      if self.beta:
          tar_loss = self.tar_loss(rnn_outputs)
          weighted_loss = weighted_loss + tar_loss
      return weighted_loss, loss
  
imports: from typing import List, Optional, Tuple, Union, Type
  import mxnet
  import numpy
  from mxnet.gluon.contrib.rnn import VariationalDropoutCell
  from mxnet.gluon.rnn import ZoneoutCell
  from gluonts.core.component import validated
  from gluonts.itertools import prod
  from gluonts.mx import Tensor
  from gluonts.mx.block.dropout import RNNZoneoutCell, VariationalZoneoutCell
  from gluonts.mx.block.feature import FeatureEmbedder
  from gluonts.mx.block.regularization import ActivationRegularizationLoss, TemporalActivationRegularizationLoss
  from gluonts.mx.block.scaler import MeanScaler, NOPScaler
  from gluonts.mx.distribution import Distribution, DistributionOutput
  from gluonts.mx.distribution.distribution import getF
  from gluonts.mx.util import weighted_average, mx_switch
  from mxnet import autograd
label_desc: Use image node_id 3 for calling the DeepARTrainingNetwork obj's underlying member method code with example usage: obj.hybrid_forward(F, feat_static_cat, feat_static_real, past_time_feat, past_target, past_observed_values, past_is_pad, future_time_feat, future_target, future_observed_values) and returns: weighted_loss,  loss