class_docstr: Provides an interface for activation maximization via gradient descent.
  
  This class implements the gradient ascent algorithm in order to perform
  activation maximization with convolutional neural networks (CNN).
  
  `Activation maximization <https://pdfs.semanticscholar.org/65d9/94fb778a8d9e0f632659fb33a082949a50d3.pdf>`_
  is one form of feature visualization that allows us to visualize what CNN
  filters are "looking for", by applying each filter to an input image and
  updating the input image so as to maximize the activation of the filter of
  interest (i.e. treating it as a gradient ascent task with activation as the
  loss). The implementation is inspired by `this demo <https://blog.keras.io/category/demo.html>`_
  by Francois Chollet.
  
  Args:
      model: A neural network model from `torchvision.models
          <https://pytorch.org/docs/stable/torchvision/models.html>`_,
          typically without the fully-connected part of the network.
          e.g. torchvisions.alexnet(pretrained=True).features
      img_size (int, optional, default=224): The size of an input image to be
          optimized.
      lr (float, optional, default=1.): The step size (or learning rate) of
          the gradient ascent.
      use_gpu (bool, optional, default=False): Use GPU if set to True and
          `torch.cuda.is_available()`.
class_docstr_tok: ["Provides","an","interface","for","activation","maximization","via","gradient","descent",".","This","class","implements","the","gradient","ascent","algorithm","in","order","to","perform","activation","maximization","with","convolutional","neural","networks","(","CNN",")",".","`","Activation","maximization","<","https",":","\/\/pdfs.semanticscholar.org\/65d9\/94fb778a8d9e0f632659fb33a082949a50d3.pdf",">","`","_","is","one","form","of","feature","visualization","that","allows","us","to","visualize","what","CNN","filters","are","``","looking","for","''",",","by","applying","each","filter","to","an","input","image","and","updating","the","input","image","so","as","to","maximize","the","activation","of","the","filter","of","interest","(","i.e",".","treating","it","as","a","gradient","ascent","task","with","activation","as","the","loss",")",".","The","implementation","is","inspired","by","`","this","demo","<","https",":","\/\/blog.keras.io\/category\/demo.html",">","`","_","by","Francois","Chollet",".","Args",":","model",":","A","neural","network","model","from","`","torchvision.models","<","https",":","\/\/pytorch.org\/docs\/stable\/torchvision\/models.html",">","`","_",",","typically","without","the","fully-connected","part","of","the","network",".","e.g",".","torchvisions.alexnet","(","pretrained=True",")",".features","img_size","(","int",",","optional",",","default=224",")",":","The","size","of","an","input","image","to","be","optimized",".","lr","(","float",",","optional",",","default=1",".",")",":","The","step","size","(","or","learning","rate",")","of","the","gradient","ascent",".","use_gpu","(","bool",",","optional",",","default=False",")",":","Use","GPU","if","set","to","True","and","`","torch.cuda.is_available","(",")","`","."]
docstr: Optimizes for the target layer/filter and visualizes the output.
  
  A method that combines optimization and visualization. There are
  mainly 3 types of operations, given a target layer:
  
  1. If `filter_idxs` is provided as an integer, it optimizes for the
      filter specified and plots the output.
  2. If `filter_idxs` is provided as a list of integers, it optimizes for
      all the filters specified and plots the output.
  3. if `filter_idx` is not provided, i.e. None, it randomly chooses
      `num_subplots` number of filters from the layer provided and
      plots the output.
  
  It also returns the output of the optimization, if specified with
  `return_output=True`.
  
  Args:
      layer (torch.nn.modules.conv.Conv2d): The target Conv2d layer from
          which the filter to be chosen, based on `filter_idx`.
      filter_idxs (int or list of int, optional, default=None): The index
          or indecies of the target filter(s).
      lr (float, optional, default=.1): The step size of optimization.
      num_iter (int, optional, default=30): The number of iteration for
          the gradient ascent operation.
      num_subplots (int, optional, default=4): The number of filters to
          optimize for and visualize. Relevant in case 3 above.
      figsize (tuple, optional, default=(4, 4)): The size of the plot.
          Relevant in case 1 above.
      title (str, optional default='Conv2d'): The title of the plot.
      return_output (bool, optional, default=False): Returns the
          output(s) of optimization if set to True.
  
  
  Returns:
      For a single optimization (i.e. case 1 above):
          output (list of torch.Tensor): With dimentions
              :math:`(num_iter, C, H, W)`. The size of the image is
              determined by `img_size` attribute which defaults to 224.
      For multiple optimization (i.e. case 2 or 3 above):
          output (list of list of torch.Tensor): With dimentions
              :math:`(num_subplots, num_iter, C, H, W)`. The size of the
              image is determined by `img_size` attribute which defaults
              to 224.
docstr_tok: ["Optimizes","for","the","target","layer\/filter","and","visualizes","the","output",".","A","method","that","combines","optimization","and","visualization",".","There","are","mainly","3","types","of","operations",",","given","a","target","layer",":","1",".","If","`","filter_idxs","`","is","provided","as","an","integer",",","it","optimizes","for","the","filter","specified","and","plots","the","output",".","2",".","If","`","filter_idxs","`","is","provided","as","a","list","of","integers",",","it","optimizes","for","all","the","filters","specified","and","plots","the","output",".","3.","if","`","filter_idx","`","is","not","provided",",","i.e",".","None",",","it","randomly","chooses","`","num_subplots","`","number","of","filters","from","the","layer","provided","and","plots","the","output",".","It","also","returns","the","output","of","the","optimization",",","if","specified","with","`","return_output=True","`",".","Args",":","layer","(","torch.nn.modules.conv.Conv2d",")",":","The","target","Conv2d","layer","from","which","the","filter","to","be","chosen",",","based","on","`","filter_idx","`",".","filter_idxs","(","int","or","list","of","int",",","optional",",","default=None",")",":","The","index","or","indecies","of","the","target","filter","(","s",")",".","lr","(","float",",","optional",",","default=.1",")",":","The","step","size","of","optimization",".","num_iter","(","int",",","optional",",","default=30",")",":","The","number","of","iteration","for","the","gradient","ascent","operation",".","num_subplots","(","int",",","optional",",","default=4",")",":","The","number","of","filters","to","optimize","for","and","visualize",".","Relevant","in","case","3","above",".","figsize","(","tuple",",","optional",",","default=","(","4",",","4",")",")",":","The","size","of","the","plot",".","Relevant","in","case","1","above",".","title","(","str",",","optional","default='Conv2d","'",")",":","The","title","of","the","plot",".","return_output","(","bool",",","optional",",","default=False",")",":","Returns","the","output","(","s",")","of","optimization","if","set","to","True",".","Returns",":","For","a","single","optimization","(","i.e",".","case","1","above",")",":","output","(","list","of","torch.Tensor",")",":","With","dimentions",":","math",":","`","(","num_iter",",","C",",","H",",","W",")","`",".","The","size","of","the","image","is","determined","by","`","img_size","`","attribute","which","defaults","to","224",".","For","multiple","optimization","(","i.e",".","case","2","or","3","above",")",":","output","(","list","of","list","of","torch.Tensor",")",":","With","dimentions",":","math",":","`","(","num_subplots",",","num_iter",",","C",",","H",",","W",")","`",".","The","size","of","the","image","is","determined","by","`","img_size","`","attribute","which","defaults","to","224","."]
code: def visualize(
      self,
      layer,
      filter_idxs=None,
      lr=1.0,
      num_iter=30,
      num_subplots=4,
      figsize=(4, 4),
      title="Conv2d",
      return_output=False,
  ):
      """Optimizes for the target layer/filter and visualizes the output.
  
      A method that combines optimization and visualization. There are
      mainly 3 types of operations, given a target layer:
  
      1. If `filter_idxs` is provided as an integer, it optimizes for the
          filter specified and plots the output.
      2. If `filter_idxs` is provided as a list of integers, it optimizes for
          all the filters specified and plots the output.
      3. if `filter_idx` is not provided, i.e. None, it randomly chooses
          `num_subplots` number of filters from the layer provided and
          plots the output.
  
      It also returns the output of the optimization, if specified with
      `return_output=True`.
  
      Args:
          layer (torch.nn.modules.conv.Conv2d): The target Conv2d layer from
              which the filter to be chosen, based on `filter_idx`.
          filter_idxs (int or list of int, optional, default=None): The index
              or indecies of the target filter(s).
          lr (float, optional, default=.1): The step size of optimization.
          num_iter (int, optional, default=30): The number of iteration for
              the gradient ascent operation.
          num_subplots (int, optional, default=4): The number of filters to
              optimize for and visualize. Relevant in case 3 above.
          figsize (tuple, optional, default=(4, 4)): The size of the plot.
              Relevant in case 1 above.
          title (str, optional default='Conv2d'): The title of the plot.
          return_output (bool, optional, default=False): Returns the
              output(s) of optimization if set to True.
  
  
      Returns:
          For a single optimization (i.e. case 1 above):
              output (list of torch.Tensor): With dimentions
                  :math:`(num_iter, C, H, W)`. The size of the image is
                  determined by `img_size` attribute which defaults to 224.
          For multiple optimization (i.e. case 2 or 3 above):
              output (list of list of torch.Tensor): With dimentions
                  :math:`(num_subplots, num_iter, C, H, W)`. The size of the
                  image is determined by `img_size` attribute which defaults
                  to 224.
  
      """
  
      self._lr = lr
  
      if type(filter_idxs) == int:
          self._visualize_filter(
              layer,
              filter_idxs,
              num_iter=num_iter,
              figsize=figsize,
              title=title,
          )
      else:
          num_total_filters = layer.out_channels
  
          if filter_idxs is None:
              num_subplots = min(num_total_filters, num_subplots)
              filter_idxs = np.random.choice(
                  range(num_total_filters), size=num_subplots
              )
  
          self._visualize_filters(
              layer,
              filter_idxs,
              num_iter,
              len(filter_idxs),
              title=title,
          )
  
      if return_output:
          return self.output
  
imports: import numpy
  import matplotlib.pyplot
  import torch
  import torch.nn
  from flashtorch.utils import apply_transforms, format_for_plotting, load_image, standardize_and_clip
label_desc: Use image node_id 9 for calling the GradientAscent obj's underlying member method code with example usage: obj.visualize(layer, filter_idxs, lr, num_iter, num_subplots, figsize, title, return_output) and returns: self