docstr: Take training / validation step.
docstr_tok: ["Take","training","\/","validation","step","."]
code: def step(self, x, y, batch_idx) -> Dict[str, torch.Tensor]:
      """
      Take training / validation step.
      """
      log, out = super().step(x, y, batch_idx=batch_idx)
  
      if (
          self.hparams.backcast_loss_ratio > 0 and not self.predicting
      ):  # add loss from backcast
          backcast = out["backcast"]
          backcast_weight = (
              self.hparams.backcast_loss_ratio
              * self.hparams.prediction_length
              / self.hparams.context_length
          )
          backcast_weight = backcast_weight / (
              backcast_weight + 1
          )  # normalize
          forecast_weight = 1 - backcast_weight
          if isinstance(self.loss, MASE):
              backcast_loss = (
                  self.loss(
                      backcast, x["encoder_target"], x["decoder_target"]
                  )
                  * backcast_weight
              )
          else:
              backcast_loss = (
                  self.loss(backcast, x["encoder_target"])
                  * backcast_weight
              )
          label = ["val", "train"][self.training]
          self.log(
              f"{label}_backcast_loss",
              backcast_loss,
              on_epoch=True,
              on_step=self.training,
              batch_size=len(x["decoder_target"]),
          )
          self.log(
              f"{label}_forecast_loss",
              log["loss"],
              on_epoch=True,
              on_step=self.training,
              batch_size=len(x["decoder_target"]),
          )
          log["loss"] = log["loss"] * forecast_weight + backcast_loss
  
      self.log_interpretation(x, out, batch_idx=batch_idx)
      return log, out
  
file_path: pytorch-forecasting/pytorch_forecasting/models/nbeats/__init__.py
imports: from typing import Dict, List
  import matplotlib.pyplot
  import torch
  from torch import nn
  from pytorch_forecasting.data import TimeSeriesDataSet
  from pytorch_forecasting.data.encoders import NaNLabelEncoder
  from pytorch_forecasting.metrics import MAE, MAPE, MASE, RMSE, SMAPE, MultiHorizonMetric
  from pytorch_forecasting.models.base_model import BaseModel
  from pytorch_forecasting.models.nbeats.sub_modules import NBEATSGenericBlock, NBEATSSeasonalBlock, NBEATSTrendBlock
label_desc: Use image node_id 4 for calling the NBeats obj's underlying member method code with example usage: obj.step(x, y, batch_idx) and returns: log,  out