class_docstr: Class for generating adversarial neighbors based on gradient-based methods.
  
  The core of this class implements the projected gradient descent (PGD)
  operation:
  ```
  adv_neighbor = input_features
  iterations = 10  # Number of iterations to run PGD.
  for _ in range(iterations):
    grad = gradient(adv_neighbor)
    adv_neighbor = adv_neighbor + adv_step_size * grad
    adv_neighbor = project(adv_neighbor)
  ```
  where `adv_step_size` is the step size (analogous to learning rate) for
  searching/calculating adversarial neighbor, `gradient(x)` calculates the
  gradient of the model at `x`, and `project(v)` projects the vector `v` onto
  the epsilon ball.
  
  Attributes:
    labeled_loss: a scalar (`tf.float32`) tensor calculated from true labels (or
      supervisions).
    adv_config: a `nsl.configs.AdvNeighborConfig` object.
    raise_invalid_gradient: (optional) a Boolean flag indicating whether to
      raise an error when gradients cannot be computed on some input features.
      There are three cases where gradients cannot be computed:  (1) The feature
        is a `tf.SparseTensor`. (2) The feature is in a non-differentiable
        `tf.DType`, like string or integer. (3) The feature is not involved in
        loss computation.  If set to False, those input without gradient will be
        ignored silently and not perturbed. (default=False)
    pgd_model_fn: the model function. Takes in the input_features and produces a
      prediction. This is required for PGD with more than one step.
    pgd_loss_fn: the loss function. Calculates loss between prediction and
      ground truth.
    regularizer_fn: regularization function for pgd_loss_fn.
      This is optional for PGD. It should be a function that accepts:
        1. perturbations: a dense (float32) tensor, a list of dense tensors,
          or a dictionary of feature names and dense tensors. The shape should
          be the same as input_features. This is the perturbation at each step
          of PGD.
        2. keyed_grads_x: a dense (float32) tensor, a list of dense tensors,
          or a dictionary of feature names and dense tensors. The shape should
          be the same as input_features. This is the gradient of the labeled
          loss with respect to input_features.
      The output of this function is added to pgd_loss_fn during PGD steps.
      The default when it is not given is None.
    use_while_loop: A Boolean indicating whether the PGD steps should be done in
      a `tf.while_loop`. This can potentially reduce memory footprint, but is
      not compatible to `pgd_model_fn` with side effects. (default=False)
class_docstr_tok: ["Class","for","generating","adversarial","neighbors","based","on","gradient-based","methods",".","The","core","of","this","class","implements","the","projected","gradient","descent","(","PGD",")","operation",":","``","`","adv_neighbor","=","input_features","iterations","=","10","#","Number","of","iterations","to","run","PGD",".","for","_","in","range","(","iterations",")",":","grad","=","gradient","(","adv_neighbor",")","adv_neighbor","=","adv_neighbor","+","adv_step_size","*","grad","adv_neighbor","=","project","(","adv_neighbor",")","``","`","where","`","adv_step_size","`","is","the","step","size","(","analogous","to","learning","rate",")","for","searching\/calculating","adversarial","neighbor",",","`","gradient","(","x",")","`","calculates","the","gradient","of","the","model","at","`","x","`",",","and","`","project","(","v",")","`","projects","the","vector","`","v","`","onto","the","epsilon","ball",".","Attributes",":","labeled_loss",":","a","scalar","(","`","tf.float32","`",")","tensor","calculated","from","true","labels","(","or","supervisions",")",".","adv_config",":","a","`","nsl.configs.AdvNeighborConfig","`","object",".","raise_invalid_gradient",":","(","optional",")","a","Boolean","flag","indicating","whether","to","raise","an","error","when","gradients","can","not","be","computed","on","some","input","features",".","There","are","three","cases","where","gradients","can","not","be","computed",":","(","1",")","The","feature","is","a","`","tf.SparseTensor","`",".","(","2",")","The","feature","is","in","a","non-differentiable","`","tf.DType","`",",","like","string","or","integer",".","(","3",")","The","feature","is","not","involved","in","loss","computation",".","If","set","to","False",",","those","input","without","gradient","will","be","ignored","silently","and","not","perturbed",".","(","default=False",")","pgd_model_fn",":","the","model","function",".","Takes","in","the","input_features","and","produces","a","prediction",".","This","is","required","for","PGD","with","more","than","one","step",".","pgd_loss_fn",":","the","loss","function",".","Calculates","loss","between","prediction","and","ground","truth",".","regularizer_fn",":","regularization","function","for","pgd_loss_fn",".","This","is","optional","for","PGD",".","It","should","be","a","function","that","accepts",":","1.","perturbations",":","a","dense","(","float32",")","tensor",",","a","list","of","dense","tensors",",","or","a","dictionary","of","feature","names","and","dense","tensors",".","The","shape","should","be","the","same","as","input_features",".","This","is","the","perturbation","at","each","step","of","PGD",".","2.","keyed_grads_x",":","a","dense","(","float32",")","tensor",",","a","list","of","dense","tensors",",","or","a","dictionary","of","feature","names","and","dense","tensors",".","The","shape","should","be","the","same","as","input_features",".","This","is","the","gradient","of","the","labeled","loss","with","respect","to","input_features",".","The","output","of","this","function","is","added","to","pgd_loss_fn","during","PGD","steps",".","The","default","when","it","is","not","given","is","None",".","use_while_loop",":","A","Boolean","indicating","whether","the","PGD","steps","should","be","done","in","a","`","tf.while_loop","`",".","This","can","potentially","reduce","memory","footprint",",","but","is","not","compatible","to","`","pgd_model_fn","`","with","side","effects",".","(","default=False",")"]
code: def _compose_as_dict(self, inputs):
      if isinstance(inputs, collections.abc.Mapping):
          return inputs
      elif isinstance(inputs, (tuple, list)):
          return dict(enumerate(inputs))  # index -> value
      else:
          return {"": inputs} if inputs is not None else {}
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import collections
  from absl import logging
  from neural_structured_learning.lib import abstract_gen_neighbor
  from neural_structured_learning.lib import utils
  import tensorflow
label_desc: Use image node_id 3 for calling the _GenAdvNeighbor obj's underlying member method code with example usage: obj._compose_as_dict(inputs) and returns: inputs, dict, unknown