docstr: Autoencoder (AE) based adversarial detector.
  
  Parameters
  ----------
  threshold
      Threshold used for adversarial score to determine adversarial instances.
  ae
      A trained tf.keras autoencoder model if available.
  model
      A trained tf.keras classification model.
  encoder_net
      Layers for the encoder wrapped in a tf.keras.Sequential class if no 'ae' is specified.
  decoder_net
      Layers for the decoder wrapped in a tf.keras.Sequential class if no 'ae' is specified.
  model_hl
      List with tf.keras models for the hidden layer K-L divergence computation.
  hidden_layer_kld
      Dictionary with as keys the hidden layer(s) of the model which are extracted and used
      during training of the AE, and as values the output dimension for the hidden layer.
  w_model_hl
      Weights assigned to the loss of each model in model_hl.
  temperature
      Temperature used for model prediction scaling.
      Temperature <1 sharpens the prediction probability distribution.
  data_type
      Optionally specifiy the data type (tabular, image or time-series). Added to metadata.
docstr_tok: ["Autoencoder","(","AE",")","based","adversarial","detector",".","Parameters","--","--","--","--","--","threshold","Threshold","used","for","adversarial","score","to","determine","adversarial","instances",".","ae","A","trained","tf.keras","autoencoder","model","if","available",".","model","A","trained","tf.keras","classification","model",".","encoder_net","Layers","for","the","encoder","wrapped","in","a","tf.keras.Sequential","class","if","no","'ae","'","is","specified",".","decoder_net","Layers","for","the","decoder","wrapped","in","a","tf.keras.Sequential","class","if","no","'ae","'","is","specified",".","model_hl","List","with","tf.keras","models","for","the","hidden","layer","K-L","divergence","computation",".","hidden_layer_kld","Dictionary","with","as","keys","the","hidden","layer","(","s",")","of","the","model","which","are","extracted","and","used","during","training","of","the","AE",",","and","as","values","the","output","dimension","for","the","hidden","layer",".","w_model_hl","Weights","assigned","to","the","loss","of","each","model","in","model_hl",".","temperature","Temperature","used","for","model","prediction","scaling",".","Temperature","<","1","sharpens","the","prediction","probability","distribution",".","data_type","Optionally","specifiy","the","data","type","(","tabular",",","image","or","time-series",")",".","Added","to","metadata","."]
code: def __init__(
      self,
      threshold: float = None,
      ae: tf.keras.Model = None,
      model: tf.keras.Model = None,
      encoder_net: tf.keras.Model = None,
      decoder_net: tf.keras.Model = None,
      model_hl: List[tf.keras.Model] = None,
      hidden_layer_kld: dict = None,
      w_model_hl: list = None,
      temperature: float = 1.0,
      data_type: str = None,
  ) -> None:
      """
      Autoencoder (AE) based adversarial detector.
  
      Parameters
      ----------
      threshold
          Threshold used for adversarial score to determine adversarial instances.
      ae
          A trained tf.keras autoencoder model if available.
      model
          A trained tf.keras classification model.
      encoder_net
          Layers for the encoder wrapped in a tf.keras.Sequential class if no 'ae' is specified.
      decoder_net
          Layers for the decoder wrapped in a tf.keras.Sequential class if no 'ae' is specified.
      model_hl
          List with tf.keras models for the hidden layer K-L divergence computation.
      hidden_layer_kld
          Dictionary with as keys the hidden layer(s) of the model which are extracted and used
          during training of the AE, and as values the output dimension for the hidden layer.
      w_model_hl
          Weights assigned to the loss of each model in model_hl.
      temperature
          Temperature used for model prediction scaling.
          Temperature <1 sharpens the prediction probability distribution.
      data_type
          Optionally specifiy the data type (tabular, image or time-series). Added to metadata.
      """
      super().__init__()
  
      if threshold is None:
          logger.warning(
              "No threshold level set. Need to infer threshold using `infer_threshold`."
          )
  
      self.threshold = threshold
      self.model = model
      for layer in self.model.layers:  # freeze model layers
          layer.trainable = False
  
      # check if model can be loaded, otherwise initialize AE model
      if isinstance(ae, tf.keras.Model):
          self.ae = ae
      elif isinstance(encoder_net, tf.keras.Sequential) and isinstance(
          decoder_net, tf.keras.Sequential
      ):
          self.ae = AE(encoder_net, decoder_net)  # define AE model
      else:
          raise TypeError(
              "No valid format detected for `ae` (tf.keras.Model) "
              "or `encoder_net` and `decoder_net` (tf.keras.Sequential)."
          )
  
      # intermediate feature map outputs for KLD and loss weights
      self.hidden_layer_kld = hidden_layer_kld
      if isinstance(model_hl, list):
          self.model_hl = model_hl
      elif isinstance(hidden_layer_kld, dict):
          self.model_hl = []
          for hidden_layer, output_dim in hidden_layer_kld.items():
              self.model_hl.append(
                  DenseHidden(self.model, hidden_layer, output_dim)
              )
      else:
          self.model_hl = None
      self.w_model_hl = w_model_hl
      if self.w_model_hl is None and isinstance(self.model_hl, list):
          self.w_model_hl = list(np.ones(len(self.model_hl)))
  
      self.temperature = temperature
  
      # set metadata
      self.meta["detector_type"] = "adversarial"
      self.meta["data_type"] = data_type
      self.meta["online"] = False
  
imports: import logging
  from typing import Callable, Dict, List, Tuple, Union, cast
  import numpy
  import tensorflow
  from alibi_detect.base import BaseDetector, FitMixin, ThresholdMixin, adversarial_correction_dict, adversarial_prediction_dict
  from alibi_detect.models.tensorflow.autoencoder import AE
  from alibi_detect.models.tensorflow.losses import loss_adv_ae
  from alibi_detect.models.tensorflow.trainer import trainer
  from alibi_detect.utils.tensorflow.prediction import predict_batch
  from alibi_detect.utils._types import OptimizerTF
  from tensorflow.keras.layers import Dense, Flatten
  from tensorflow.keras.losses import kld
  from tensorflow.keras.models import Model
label_desc: Use image node_id 1 to create a new AdversarialAE object from inherited base classes: BaseDetector, FitMixin, ThresholdMixin with example: obj = AdversarialAE(threshold, ae, model, encoder_net, decoder_net, model_hl, hidden_layer_kld, w_model_hl, temperature, data_type)