code: def eval_on_series(
      distr_tcn: DistributionalTCN,
      optimizer: torch.optim.Adam,
      series_tensor: torch.Tensor,
      ts_len: int,
      context_length: int,
      is_train: bool = False,
      return_predictions: bool = False,
      lead_time: int = 1,
  ):
      """
      Arguments
      ----------
      distr_tcn: DistributionalTCN
      otimizer: Optimizer containing parameters, learning rate, etc
      series_tensor: Time series
      ts_len: Length of time series
      context_length: Number of time steps to input
      is_train: True if time series is training set
      return_predictions: True if to return (loss, predictions), False if to return loss only
      lead_time: Number of time steps to predict ahead
      """
      loss_log = []
  
      # Parallelising the training:
      trying_mini_batches = True
      if is_train:
          mini_batch_size = 64
          stride = 1
  
          window_length = context_length + lead_time
  
          unfold_layer = torch.nn.Unfold(
              kernel_size=(1, window_length), stride=stride
          )
  
          ts_windows = (
              unfold_layer(series_tensor.unsqueeze(2))
              .transpose(1, 2)
              .transpose(0, 1)
          )
  
          numb_mini_batches = ts_windows.shape[0] // mini_batch_size
  
          if trying_mini_batches:
              batch_indices = np.arange(ts_len - window_length - 1)
              for i in range(numb_mini_batches):
                  idx = np.random.choice(batch_indices, mini_batch_size)
                  batch_indices = np.setdiff1d(batch_indices, idx)
  
                  ts_chunks = ts_windows[idx, :, :-lead_time]
                  targets = ts_windows[idx, :, -1]
  
                  loss_log.append(
                      train_step_from_batch(
                          ts_chunks, targets, distr_tcn, optimizer
                      )
                  )
  
          else:
              ts_chunks = ts_windows[:, :, :-lead_time]
              targets = ts_windows[:, :, -1]
  
              loss_log.append(train_step_from_batch(ts_chunks, targets))
  
          return loss_log
  
      if return_predictions:
          predictions = {
              "low_lower": [],
              "lower": [],
              "median": [],
              "upper": [],
              "up_upper": [],
          }
  
      for i in range(ts_len - context_length - lead_time - 1):
          ts_chunk = series_tensor[:, :, i : i + context_length]
          target = series_tensor[
              :, :, i + context_length + lead_time - 1
          ]
  
          distr_output = distr_tcn(ts_chunk.float())
  
          if return_predictions:
              predictions["lower"].append(
                  distr_output.icdf(torch.tensor(0.05))
              )
              predictions["median"].append(
                  distr_output.icdf(torch.tensor(0.5))
              )
              predictions["upper"].append(
                  distr_output.icdf(torch.tensor(0.95))
              )
              predictions["low_lower"].append(
                  distr_output.icdf(torch.tensor(0.01))
              )
              predictions["up_upper"].append(
                  distr_output.icdf(torch.tensor(0.99))
              )
  
          loss = -distr_output.log_prob(target.float())
          loss_value = loss.cpu().detach().numpy()[0]
          loss_log.append(loss_value)
  
          if is_train:
              loss.backward()
              optimizer.step()
  
      if return_predictions:
          return loss_log, predictions
      return loss_log
  
file_path: gluonts/src/gluonts/nursery/spliced_binned_pareto/training_functions.py
imports: from typing import Optional
  import matplotlib
  import matplotlib.pyplot
  import numpy
  import pandas
  import torch.nn
  import torch.optim
  from .distr_tcn import DistributionalTCN
label_desc: Use image node_id 2 for calling a global function with example usage: eval_on_series(distr_tcn, optimizer, series_tensor, ts_len, context_length, is_train, return_predictions, lead_time) and returns: loss_log, loss_log, loss_log,  predictions