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: Samples segments into a compatible format.
  
  Args:
    data: Sources data to sample and batch
    max_samples: Maximum number of samples in batch
  
  Returns:
    Dictionary of batched data with the maximum samples specified.
docstr_tok: ["Samples","segments","into","a","compatible","format",".","Args",":","data",":","Sources","data","to","sample","and","batch","max_samples",":","Maximum","number","of","samples","in","batch","Returns",":","Dictionary","of","batched","data","with","the","maximum","samples","specified","."]
code: def _batch_sampled_data(self, data, max_samples):
      """Samples segments into a compatible format.
  
      Args:
        data: Sources data to sample and batch
        max_samples: Maximum number of samples in batch
  
      Returns:
        Dictionary of batched data with the maximum samples specified.
      """
  
      if max_samples < 1:
          raise ValueError(
              "Illegal number of samples specified! samples={}".format(
                  max_samples
              )
          )
  
      id_col = self._get_single_col_by_type(InputTypes.ID)
      time_col = self._get_single_col_by_type(InputTypes.TIME)
  
      data.sort_values(by=[id_col, time_col], inplace=True)
  
      print("Getting valid sampling locations.")
      valid_sampling_locations = []
      split_data_map = {}
      for identifier, df in data.groupby(id_col):
          print("Getting locations for {}".format(identifier))
          num_entries = len(df)
          if num_entries >= self.time_steps:
              valid_sampling_locations += [
                  (identifier, self.time_steps + i)
                  for i in range(num_entries - self.time_steps + 1)
              ]
          split_data_map[identifier] = df
  
      inputs = np.zeros((max_samples, self.time_steps, self.input_size))
      outputs = np.zeros(
          (max_samples, self.time_steps, self.output_size)
      )
      time = np.empty((max_samples, self.time_steps, 1), dtype=object)
      identifiers = np.empty(
          (max_samples, self.time_steps, 1), dtype=object
      )
  
      if (
          max_samples > 0
          and len(valid_sampling_locations) > max_samples
      ):
          print("Extracting {} samples...".format(max_samples))
          ranges = [
              valid_sampling_locations[i]
              for i in np.random.choice(
                  len(valid_sampling_locations),
                  max_samples,
                  replace=False,
              )
          ]
      else:
          print(
              "Max samples={} exceeds # available segments={}".format(
                  max_samples, len(valid_sampling_locations)
              )
          )
          ranges = valid_sampling_locations
  
      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}
      ]
  
      for i, tup in enumerate(ranges):
          if (i + 1 % 1000) == 0:
              print(i + 1, "of", max_samples, "samples done...")
          identifier, start_idx = tup
          sliced = split_data_map[identifier].iloc[
              start_idx - self.time_steps : start_idx
          ]
          inputs[i, :, :] = sliced[input_cols]
          outputs[i, :, :] = sliced[[target_col]]
          time[i, :, 0] = sliced[time_col]
          identifiers[i, :, 0] = sliced[id_col]
  
      sampled_data = {
          "inputs": inputs,
          "outputs": outputs[:, self.num_encoder_steps :, :],
          "active_entries": np.ones_like(
              outputs[:, self.num_encoder_steps :, :]
          ),
          "time": time,
          "identifier": identifiers,
      }
  
      return sampled_data
  
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 6 for calling the TemporalFusionTransformer obj's underlying member method code with example usage: obj._batch_sampled_data(data, max_samples) and returns: sampled_data