class_docstr: Forecaster base model for lstm, seq2seq, tcn and nbeats forecasters.
class_docstr_tok: ["Forecaster","base","model","for","lstm",",","seq2seq",",","tcn","and","nbeats","forecasters","."]
docstr: Calculate confidence interval of data based on Monte Carlo dropout(MC dropout).
  Related paper : https://arxiv.org/abs/1709.01907
  
  :param data: The data support following formats:
  
         | 1. a numpy ndarray x:
         | x's shape is (num_samples, lookback, feature_dim) where lookback and feature_dim
         | should be the same as past_seq_len and input_feature_num.
         |
         | 2. pytorch dataloader:
         | the dataloader needs to return at least x in each iteration
         | with the shape as following:
         | x's shape is (num_samples, lookback, feature_dim) where lookback and feature_dim
         | should be the same as past_seq_len and input_feature_num.
         | If returns x and y only get x.
         |
         | 3. A bigdl.chronos.data.tsdataset.TSDataset instance:
         | Forecaster will automatically process the TSDataset.
         | By default, TSDataset will be transformed to a pytorch dataloader,
         | which is memory-friendly while a little bit slower.
         | Users may call `roll` on the TSDataset before calling `fit`
         | Then the training speed will be faster but will consume more memory.
  
  :param validation_data: The validation_data support following formats:
  
         | 1. a numpy ndarray tuple (x, y):
         | x's shape is (num_samples, lookback, feature_dim) where lookback and feature_dim
         | should be the same as past_seq_len and input_feature_num.
         | y's shape is (num_samples, horizon, target_dim), where horizon and target_dim
         | should be the same as future_seq_len and output_feature_num.
         |
         | 2. pytorch dataloader:
         | the dataloader should return x, y in each iteration with the shape as following:
         | x's shape is (num_samples, lookback, feature_dim) where lookback and feature_dim
         | should be the same as past_seq_len and input_feature_num.
         | y's shape is (num_samples, horizon, target_dim), where horizon and target_dim
         | should be the same as future_seq_len and output_feature_num.
         |
         | 3. A bigdl.chronos.data.tsdataset.TSDataset instance:
         | Forecaster will automatically process the TSDataset.
         | By default, TSDataset will be transformed to a pytorch dataloader,
         | which is memory-friendly while a little bit slower.
         | Users may call `roll` on the TSDataset before calling `fit`
         | Then the training speed will be faster but will consume more memory.
  
  :param batch_size: predict batch size. The value will not affect predict
         result but will affect resources cost(e.g. memory and time).
  :param repetition_times: Defines repeate how many times to calculate model
                           uncertainty based on MC Dropout.
  
  :return: prediction and standard deviation which are both numpy array
           with shape (num_samples, horizon, target_dim)
code: def predict_interval(
      self,
      data,
      validation_data=None,
      batch_size=32,
      repetition_times=5,
  ):
      """
      Calculate confidence interval of data based on Monte Carlo dropout(MC dropout).
      Related paper : https://arxiv.org/abs/1709.01907
  
      :param data: The data support following formats:
  
             | 1. a numpy ndarray x:
             | x's shape is (num_samples, lookback, feature_dim) where lookback and feature_dim
             | should be the same as past_seq_len and input_feature_num.
             |
             | 2. pytorch dataloader:
             | the dataloader needs to return at least x in each iteration
             | with the shape as following:
             | x's shape is (num_samples, lookback, feature_dim) where lookback and feature_dim
             | should be the same as past_seq_len and input_feature_num.
             | If returns x and y only get x.
             |
             | 3. A bigdl.chronos.data.tsdataset.TSDataset instance:
             | Forecaster will automatically process the TSDataset.
             | By default, TSDataset will be transformed to a pytorch dataloader,
             | which is memory-friendly while a little bit slower.
             | Users may call `roll` on the TSDataset before calling `fit`
             | Then the training speed will be faster but will consume more memory.
  
      :param validation_data: The validation_data support following formats:
  
             | 1. a numpy ndarray tuple (x, y):
             | x's shape is (num_samples, lookback, feature_dim) where lookback and feature_dim
             | should be the same as past_seq_len and input_feature_num.
             | y's shape is (num_samples, horizon, target_dim), where horizon and target_dim
             | should be the same as future_seq_len and output_feature_num.
             |
             | 2. pytorch dataloader:
             | the dataloader should return x, y in each iteration with the shape as following:
             | x's shape is (num_samples, lookback, feature_dim) where lookback and feature_dim
             | should be the same as past_seq_len and input_feature_num.
             | y's shape is (num_samples, horizon, target_dim), where horizon and target_dim
             | should be the same as future_seq_len and output_feature_num.
             |
             | 3. A bigdl.chronos.data.tsdataset.TSDataset instance:
             | Forecaster will automatically process the TSDataset.
             | By default, TSDataset will be transformed to a pytorch dataloader,
             | which is memory-friendly while a little bit slower.
             | Users may call `roll` on the TSDataset before calling `fit`
             | Then the training speed will be faster but will consume more memory.
  
      :param batch_size: predict batch size. The value will not affect predict
             result but will affect resources cost(e.g. memory and time).
      :param repetition_times: Defines repeate how many times to calculate model
                               uncertainty based on MC Dropout.
  
      :return: prediction and standard deviation which are both numpy array
               with shape (num_samples, horizon, target_dim)
  
      """
      if self.distributed:
          invalidInputError(
              False,
              "predict interval has not been supported for distributed "
              "forecaster. You can call .to_local() to transform the "
              "forecaster to a non-distributed version.",
          )
  
      if not self.fitted:
          invalidInputError(
              False,
              "You must call fit or restore first before calling predict_interval!",
          )
  
      self.thread_num = set_pytorch_thread(
          self.optimized_model_thread_num, self.thread_num
      )
      if not self.context_enabled:
          self.cxt_manager = ForecasterContextManager(
              self, self.thread_num, True
          )
      else:
          self.cxt_manager = DummyForecasterContextManager()
  
      # step1, according to validation dataset, calculate inherent noise
      if not hasattr(self, "data_noise"):
          invalidInputError(
              validation_data is not None,
              "When call predict_interval for the first time, you must pass in "
              "validation_data to calculate data noise.",
          )
          # data transform
          if isinstance(validation_data, TSDataset):
              _rolled = validation_data.numpy_x is None
              validation_data = validation_data.to_torch_data_loader(
                  batch_size=batch_size,
                  roll=_rolled,
                  lookback=self.data_config["past_seq_len"],
                  horizon=self.data_config["future_seq_len"],
                  feature_col=data.roll_feature,
                  target_col=data.roll_target,
                  shuffle=False,
              )
  
          if isinstance(validation_data, DataLoader):
              input_data = validation_data
              target = np.concatenate(
                  tuple(val[1] for val in validation_data), axis=0
              )
          else:
              input_data, target = validation_data
          self.internal.eval()
          with self.cxt_manager:
              val_yhat = _pytorch_fashion_inference(
                  model=self.internal,
                  input_data=input_data,
                  batch_size=batch_size,
              )
          self.data_noise = Evaluator.evaluate(
              ["mse"], target, val_yhat, aggregate=None
          )[
              0
          ]  # 2d array
  
      # data preprocess
      if isinstance(data, TSDataset):
          _rolled = data.numpy_x is None
          data = data.to_torch_data_loader(
              batch_size=batch_size,
              roll=_rolled,
              lookback=self.data_config["past_seq_len"],
              horizon=self.data_config["future_seq_len"],
              feature_col=data.roll_feature,
              target_col=data.roll_target,
              shuffle=False,
          )
  
      # step2: calculate model uncertainty based MC Dropout
      def apply_dropout(m):
          if type(m) == torch.nn.Dropout:
              m.train()
  
      # turn on dropout
      self.internal.apply(apply_dropout)
  
      y_hat_list = []
      with self.cxt_manager:
          for i in range(repetition_times):
              _yhat = _pytorch_fashion_inference(
                  model=self.internal,
                  input_data=data,
                  batch_size=batch_size,
              )
              y_hat_list.append(_yhat)
      y_hat_mean = np.mean(np.stack(y_hat_list, axis=0), axis=0)
  
      model_bias = np.zeros_like(y_hat_mean)  # 3d array
      for i in range(repetition_times):
          model_bias += (y_hat_list[i] - y_hat_mean) ** 2
      model_bias /= repetition_times
      std_deviation = np.sqrt(self.data_noise + model_bias)
  
      return y_hat_mean, std_deviation
  
imports: from bigdl.chronos.forecaster.abstract import Forecaster
  from bigdl.chronos.forecaster.utils import *
  from bigdl.chronos.metric.forecast_metrics import Evaluator
  from typing import Optional
  import numpy
  import warnings
  import os
  from tempfile import TemporaryDirectory
  import torch
  from torch.utils.data import TensorDataset, DataLoader
  from .utils_hpo import GenericLightningModule, _format_metric_str, _config_has_search_space
  from bigdl.nano.utils.common import invalidOperationError, invalidInputError
  from bigdl.chronos.data.tsdataset import TSDataset
  from bigdl.chronos.pytorch.context_manager import DummyForecasterContextManager, ForecasterContextManager
  from bigdl.chronos.pytorch.utils import _pytorch_fashion_inference
label_desc: Use image node_id 14 for calling the BasePytorchForecaster obj's underlying member method code with example usage: obj.predict_interval(data, validation_data, batch_size, repetition_times) and returns: y_hat_mean,  std_deviation