class_docstr: ATTENTION:
      Currently, innvestigating a network only works if all
      layers that have to be inverted are specified explicitly
      and registered as a module. If., for example,
      only the functional max_poolnd is used, the inversion will not work.
class_docstr_tok: ["ATTENTION",":","Currently",",","innvestigating","a","network","only","works","if","all","layers","that","have","to","be","inverted","are","specified","explicitly","and","registered","as","a","module",".","If.",",","for","example",",","only","the","functional","max_poolnd","is","used",",","the","inversion","will","not","work","."]
docstr: Model wrapper for pytorch models to 'innvestigate' them
  with layer-wise relevance propagation (LRP) as introduced by Bach et. al
  (https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0130140).
  Given a class level probability produced by the model under consideration,
  the LRP algorithm attributes this probability to the nodes in each layer.
  This allows for visualizing the relevance of input pixels on the resulting
  class probability.
  
  Args:
      the_model: Pytorch model, e.g. a pytorch.nn.Sequential consisting of
                  different layers. Not all layers are supported yet.
      lrp_exponent: Exponent for rescaling the importance values per node
                      in a layer when using the e-rule method.
      beta: Beta value allows for placing more (large beta) emphasis on
              nodes that positively contribute to the activation of a given node
              in the subsequent layer. Low beta value allows for placing more emphasis
              on inhibitory neurons in a layer. Only relevant for method 'b-rule'.
      epsilon: Stabilizing term to avoid numerical instabilities if the norm (denominator
              for distributing the relevance) is close to zero.
      method: Different rules for the LRP algorithm, b-rule allows for placing
              more or less focus on positive / negative contributions, whereas
              the e-rule treats them equally. For more information,
              see the paper linked above.
docstr_tok: ["Model","wrapper","for","pytorch","models","to","'innvestigate","'","them","with","layer-wise","relevance","propagation","(","LRP",")","as","introduced","by","Bach","et",".","al","(","https",":","\/\/journals.plos.org\/plosone\/article","?","id=10.1371\/journal.pone.0130140",")",".","Given","a","class","level","probability","produced","by","the","model","under","consideration",",","the","LRP","algorithm","attributes","this","probability","to","the","nodes","in","each","layer",".","This","allows","for","visualizing","the","relevance","of","input","pixels","on","the","resulting","class","probability",".","Args",":","the_model",":","Pytorch","model",",","e.g",".","a","pytorch.nn.Sequential","consisting","of","different","layers",".","Not","all","layers","are","supported","yet",".","lrp_exponent",":","Exponent","for","rescaling","the","importance","values","per","node","in","a","layer","when","using","the","e-rule","method",".","beta",":","Beta","value","allows","for","placing","more","(","large","beta",")","emphasis","on","nodes","that","positively","contribute","to","the","activation","of","a","given","node","in","the","subsequent","layer",".","Low","beta","value","allows","for","placing","more","emphasis","on","inhibitory","neurons","in","a","layer",".","Only","relevant","for","method","'","b-rule","'",".","epsilon",":","Stabilizing","term","to","avoid","numerical","instabilities","if","the","norm","(","denominator","for","distributing","the","relevance",")","is","close","to","zero",".","method",":","Different","rules","for","the","LRP","algorithm",",","b-rule","allows","for","placing","more","or","less","focus","on","positive","\/","negative","contributions",",","whereas","the","e-rule","treats","them","equally",".","For","more","information",",","see","the","paper","linked","above","."]
code: def __init__(
      self,
      the_model,
      lrp_exponent=1,
      beta=0.5,
      epsilon=1e-6,
      method="e-rule",
  ):
      """
      Model wrapper for pytorch models to 'innvestigate' them
      with layer-wise relevance propagation (LRP) as introduced by Bach et. al
      (https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0130140).
      Given a class level probability produced by the model under consideration,
      the LRP algorithm attributes this probability to the nodes in each layer.
      This allows for visualizing the relevance of input pixels on the resulting
      class probability.
  
      Args:
          the_model: Pytorch model, e.g. a pytorch.nn.Sequential consisting of
                      different layers. Not all layers are supported yet.
          lrp_exponent: Exponent for rescaling the importance values per node
                          in a layer when using the e-rule method.
          beta: Beta value allows for placing more (large beta) emphasis on
                  nodes that positively contribute to the activation of a given node
                  in the subsequent layer. Low beta value allows for placing more emphasis
                  on inhibitory neurons in a layer. Only relevant for method 'b-rule'.
          epsilon: Stabilizing term to avoid numerical instabilities if the norm (denominator
                  for distributing the relevance) is close to zero.
          method: Different rules for the LRP algorithm, b-rule allows for placing
                  more or less focus on positive / negative contributions, whereas
                  the e-rule treats them equally. For more information,
                  see the paper linked above.
      """
      super(InnvestigateModel, self).__init__()
      self.model = the_model
      self.device = torch.device("cpu", 0)
      self.prediction = None
      self.r_values_per_layer = None
      self.only_max_score = None
      # Initialize the 'Relevance Propagator' with the chosen rule.
      # This will be used to back-propagate the relevance values
      # through the layers in the innvestigate method.
      self.inverter = RelevancePropagator(
          lrp_exponent=lrp_exponent,
          beta=beta,
          method=method,
          epsilon=epsilon,
          device=self.device,
      )
  
      # Parsing the individual model layers
      self.register_hooks(self.model)
      if method == "b-rule" and float(beta) in (-1.0, 0):
          which = "positive" if beta == -1 else "negative"
          which_opp = "negative" if beta == -1 else "positive"
          print(
              "WARNING: With the chosen beta value, "
              "only " + which + " contributions "
              "will be taken into account.\nHence, "
              "if in any layer only "
              + which_opp
              + " contributions exist, the "
              "overall relevance will not be conserved.\n"
          )
  
file_path: tensorwatch/tensorwatch/saliency/epsilon_lrp.py
imports: import torch
  import numpy
  from .inverter_util import RelevancePropagator
label_desc: Use image node_id 1 to create a new InnvestigateModel object from inherited base classes: torch.nn with example: obj = InnvestigateModel(the_model, lrp_exponent, beta, epsilon, method)