docstr: Creates an optimizer that minimizes weighted loss function.
  
  Args:
      input_tensor: An input tensor of shape: `(samples, channels, image_dims...)` if `image_data_format=
          channels_first` or `(samples, image_dims..., channels)` if `image_data_format=channels_last`.
      losses: List of ([Loss](vis.losses.md#Loss), weight) tuples.
      input_range: Specifies the input range as a `(min, max)` tuple. This is used to rescale the
          final optimized input to the given range. (Default value=(0, 255))
      wrt_tensor: Short for, with respect to. This instructs the optimizer that the aggregate loss from `losses`
          should be minimized with respect to `wrt_tensor`.
          `wrt_tensor` can be any tensor that is part of the model graph. Default value is set to None
          which means that loss will simply be minimized with respect to `input_tensor`.
      norm_grads: True to normalize gradients. Normalization avoids very small or large gradients and ensures
          a smooth gradient gradient descent process. If you want the actual gradient
          (for example, visualizing attention), set this to false.
docstr_tok: ["Creates","an","optimizer","that","minimizes","weighted","loss","function",".","Args",":","input_tensor",":","An","input","tensor","of","shape",":","`","(","samples",",","channels",",","image_dims","...",")","`","if","`","image_data_format=","channels_first","`","or","`","(","samples",",","image_dims","...",",","channels",")","`","if","`","image_data_format=channels_last","`",".","losses",":","List","of","(","[","Loss","]","(","vis.losses.md","#","Loss",")",",","weight",")","tuples",".","input_range",":","Specifies","the","input","range","as","a","`","(","min",",","max",")","`","tuple",".","This","is","used","to","rescale","the","final","optimized","input","to","the","given","range",".","(","Default","value=","(","0",",","255",")",")","wrt_tensor",":","Short","for",",","with","respect","to",".","This","instructs","the","optimizer","that","the","aggregate","loss","from","`","losses","`","should","be","minimized","with","respect","to","`","wrt_tensor","`",".","`","wrt_tensor","`","can","be","any","tensor","that","is","part","of","the","model","graph",".","Default","value","is","set","to","None","which","means","that","loss","will","simply","be","minimized","with","respect","to","`","input_tensor","`",".","norm_grads",":","True","to","normalize","gradients",".","Normalization","avoids","very","small","or","large","gradients","and","ensures","a","smooth","gradient","gradient","descent","process",".","If","you","want","the","actual","gradient","(","for","example",",","visualizing","attention",")",",","set","this","to","false","."]
code: def __init__(
      self,
      input_tensor,
      losses,
      input_range=(0, 255),
      wrt_tensor=None,
      norm_grads=True,
  ):
      """Creates an optimizer that minimizes weighted loss function.
  
      Args:
          input_tensor: An input tensor of shape: `(samples, channels, image_dims...)` if `image_data_format=
              channels_first` or `(samples, image_dims..., channels)` if `image_data_format=channels_last`.
          losses: List of ([Loss](vis.losses.md#Loss), weight) tuples.
          input_range: Specifies the input range as a `(min, max)` tuple. This is used to rescale the
              final optimized input to the given range. (Default value=(0, 255))
          wrt_tensor: Short for, with respect to. This instructs the optimizer that the aggregate loss from `losses`
              should be minimized with respect to `wrt_tensor`.
              `wrt_tensor` can be any tensor that is part of the model graph. Default value is set to None
              which means that loss will simply be minimized with respect to `input_tensor`.
          norm_grads: True to normalize gradients. Normalization avoids very small or large gradients and ensures
              a smooth gradient gradient descent process. If you want the actual gradient
              (for example, visualizing attention), set this to false.
      """
      self.input_tensor = input_tensor
      self.input_range = input_range
      self.loss_names = []
      self.loss_functions = []
      self.wrt_tensor = (
          self.input_tensor if wrt_tensor is None else wrt_tensor
      )
      if self.input_tensor is self.wrt_tensor:
          self.wrt_tensor_is_input_tensor = True
          self.wrt_tensor = K.identity(self.wrt_tensor)
      else:
          self.wrt_tensor_is_input_tensor = False
  
      overall_loss = None
      for loss, weight in losses:
          # Perf optimization. Don't build loss function with 0 weight.
          if weight != 0:
              loss_fn = weight * loss.build_loss()
              overall_loss = (
                  loss_fn
                  if overall_loss is None
                  else overall_loss + loss_fn
              )
              self.loss_names.append(loss.name)
              self.loss_functions.append(loss_fn)
  
      # Compute gradient of overall with respect to `wrt` tensor.
      if self.wrt_tensor_is_input_tensor:
          grads = K.gradients(overall_loss, self.input_tensor)[0]
      else:
          grads = K.gradients(overall_loss, self.wrt_tensor)[0]
      if norm_grads:
          grads = K.l2_normalize(grads)
  
      # The main function to compute various quantities in optimization loop.
      self.compute_fn = K.function(
          [self.input_tensor, K.learning_phase()],
          self.loss_functions + [overall_loss, grads, self.wrt_tensor],
      )
  
file_path: keras-vis/vis/optimizer.py
imports: from __future__ import absolute_import
  import numpy
  from keras import backend
  from .callbacks import Print
  from .grad_modifiers import get
  from .utils import utils
label_desc: Use image node_id 1 to create a new Optimizer object from inherited base classes: object with example: obj = Optimizer(input_tensor, losses, input_range, wrt_tensor, norm_grads)