class_docstr: Defines Temporal Fusion Transformer.
  
  Attributes:
    name: Name of model
    time_steps: Total number of input time steps per forecast date (i.e. Width
      of Temporal fusion decoder N)
    input_size: Total number of inputs
    output_size: Total number of outputs
    category_counts: Number of categories per categorical variable
    n_multiprocessing_workers: Number of workers to use for parallel
      computations
    column_definition: List of tuples of (string, DataType, InputType) that
      define each column
    quantiles: Quantiles to forecast for TFT
    use_cudnn: Whether to use Keras CuDNNLSTM or standard LSTM layers
    hidden_layer_size: Internal state size of TFT
    dropout_rate: Dropout discard rate
    max_gradient_norm: Maximum norm for gradient clipping
    learning_rate: Initial learning rate of ADAM optimizer
    minibatch_size: Size of minibatches for training
    num_epochs: Maximum number of epochs for training
    early_stopping_patience: Maximum number of iterations of non-improvement
      before early stopping kicks in
    num_encoder_steps: Size of LSTM encoder -- i.e. number of past time steps
      before forecast date to use
    num_stacks: Number of self-attention layers to apply (default is 1 for basic
      TFT)
    num_heads: Number of heads for interpretable mulit-head attention
    model: Keras model for TFT
class_docstr_tok: ["Defines","Temporal","Fusion","Transformer",".","Attributes",":","name",":","Name","of","model","time_steps",":","Total","number","of","input","time","steps","per","forecast","date","(","i.e",".","Width","of","Temporal","fusion","decoder","N",")","input_size",":","Total","number","of","inputs","output_size",":","Total","number","of","outputs","category_counts",":","Number","of","categories","per","categorical","variable","n_multiprocessing_workers",":","Number","of","workers","to","use","for","parallel","computations","column_definition",":","List","of","tuples","of","(","string",",","DataType",",","InputType",")","that","define","each","column","quantiles",":","Quantiles","to","forecast","for","TFT","use_cudnn",":","Whether","to","use","Keras","CuDNNLSTM","or","standard","LSTM","layers","hidden_layer_size",":","Internal","state","size","of","TFT","dropout_rate",":","Dropout","discard","rate","max_gradient_norm",":","Maximum","norm","for","gradient","clipping","learning_rate",":","Initial","learning","rate","of","ADAM","optimizer","minibatch_size",":","Size","of","minibatches","for","training","num_epochs",":","Maximum","number","of","epochs","for","training","early_stopping_patience",":","Maximum","number","of","iterations","of","non-improvement","before","early","stopping","kicks","in","num_encoder_steps",":","Size","of","LSTM","encoder","--","i.e",".","number","of","past","time","steps","before","forecast","date","to","use","num_stacks",":","Number","of","self-attention","layers","to","apply","(","default","is","1","for","basic","TFT",")","num_heads",":","Number","of","heads","for","interpretable","mulit-head","attention","model",":","Keras","model","for","TFT"]
docstr: Batches data for training.
  
  Converts raw dataframe from a 2-D tabular format to a batched 3-D array
  to feed into Keras model.
  
  Args:
    data: DataFrame to batch
  
  Returns:
    Batched Numpy array with shape=(?, self.time_steps, self.input_size)
docstr_tok: ["Batches","data","for","training",".","Converts","raw","dataframe","from","a","2-D","tabular","format","to","a","batched","3-D","array","to","feed","into","Keras","model",".","Args",":","data",":","DataFrame","to","batch","Returns",":","Batched","Numpy","array","with","shape=","(","?",",","self.time_steps",",","self.input_size",")"]
code: def _batch_data(self, data):
      """Batches data for training.
  
      Converts raw dataframe from a 2-D tabular format to a batched 3-D array
      to feed into Keras model.
  
      Args:
        data: DataFrame to batch
  
      Returns:
        Batched Numpy array with shape=(?, self.time_steps, self.input_size)
      """
  
      # Functions.
      def _batch_single_entity(input_data):
          time_steps = len(input_data)
          lags = self.time_steps
          x = input_data.values
          if time_steps >= lags:
              return np.stack(
                  [
                      x[i : time_steps - (lags - 1) + i, :]
                      for i in range(lags)
                  ],
                  axis=1,
              )
  
          else:
              return None
  
      id_col = self._get_single_col_by_type(InputTypes.ID)
      time_col = self._get_single_col_by_type(InputTypes.TIME)
      target_col = self._get_single_col_by_type(InputTypes.TARGET)
      input_cols = [
          tup[0]
          for tup in self.column_definition
          if tup[2] not in {InputTypes.ID, InputTypes.TIME}
      ]
  
      data_map = {}
      for _, sliced in data.groupby(id_col):
          col_mappings = {
              "identifier": [id_col],
              "time": [time_col],
              "outputs": [target_col],
              "inputs": input_cols,
          }
  
          for k in col_mappings:
              cols = col_mappings[k]
              arr = _batch_single_entity(sliced[cols].copy())
  
              if k not in data_map:
                  data_map[k] = [arr]
              else:
                  data_map[k].append(arr)
  
      # Combine all data
      for k in data_map:
          # Wendi: Avoid returning None when the length is not enough
          data_map[k] = np.concatenate(
              [i for i in data_map[k] if i is not None], axis=0
          )
  
      # Shorten target so we only get decoder steps
      data_map["outputs"] = data_map["outputs"][
          :, self.num_encoder_steps :, :
      ]
  
      active_entries = np.ones_like(data_map["outputs"])
      if "active_entries" not in data_map:
          data_map["active_entries"] = active_entries
      else:
          data_map["active_entries"].append(active_entries)
  
      return data_map
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import gc
  import json
  import os
  import shutil
  import data_formatters.base
  import libs.utils
  import numpy
  import pandas
  import tensorflow
label_desc: Use image node_id 7 for calling the TemporalFusionTransformer obj's underlying member method code with example usage: obj._batch_data(data) and returns: data_map, np, None