class_docstr: Relative Representation Stability leverages the stability of an explanation with respect
  to the change in the output logits.
  
      `RRS(x, x', ex, ex') = max \frac{||\frac{e_x - e_{x'}}{e_x}||_p}
      {max (||\frac{L_x - L_{x'}}{L_x}||_p, \epsilon_{min})},`
  
  where `L(·)` denotes the internal model representation, e.g., output embeddings of hidden layers.
  
  References:
      1) Chirag Agarwal, et. al., 2022. "Rethinking stability for attribution based explanations.",
      https://arxiv.org/pdf/2203.06877.pdf
  
  Attributes:
      -  _name: The name of the metric.
      - _data_applicability: The data types that the metric implementation currently supports.
      - _models: The model types that this metric can work with.
      - score_direction: How to interpret the scores, whether higher/ lower values are considered better.
      - evaluation_category: What property/ explanation quality that this metric measures.
class_docstr_tok: ["Relative","Representation","Stability","leverages","the","stability","of","an","explanation","with","respect","to","the","change","in","the","output","logits",".","`","RRS","(","x",",","x","'",",","ex",",","ex","'",")","=","max","\\frac","{","||\\frac","{","e_x","-","e_","{","x","'","}","}","{","e_x","}","||_p","}","{","max","(","||\\frac","{","L_x","-","L_","{","x","'","}","}","{","L_x","}","||_p",",","\\epsilon_","{","min","}",")","}",",","`","where","`","L","(","\u00b7",")","`","denotes","the","internal","model","representation",",","e.g.",",","output","embeddings","of","hidden","layers",".","References",":","1",")","Chirag","Agarwal",",","et",".","al.",",","2022",".","``","Rethinking","stability","for","attribution","based","explanations",".","``",",","https",":","\/\/arxiv.org\/pdf\/2203.06877.pdf","Attributes",":","-","_name",":","The","name","of","the","metric",".","-","_data_applicability",":","The","data","types","that","the","metric","implementation","currently","supports",".","-","_models",":","The","model","types","that","this","metric","can","work","with",".","-","score_direction",":","How","to","interpret","the","scores",",","whether","higher\/","lower","values","are","considered","better",".","-","evaluation_category",":","What","property\/","explanation","quality","that","this","metric","measures","."]
docstr: Computes relative representation stabilities maximization objective
  as defined here https://arxiv.org/pdf/2203.06877.pdf by the authors.
  
  Parameters
  ----------
  l_x: np.ndarray
      Internal representation for x_batch.
  l_xs: np.ndarray
      Internal representation for xs_batch.
  e_x: np.ndarray
      Explanations for x.
  e_xs: np.ndarray
      Explanations for xs.
  
  Returns
  -------
  rrs_obj: np.ndarray
      RRS maximization objective.
docstr_tok: ["Computes","relative","representation","stabilities","maximization","objective","as","defined","here","https",":","\/\/arxiv.org\/pdf\/2203.06877.pdf","by","the","authors",".","Parameters","--","--","--","--","--","l_x",":","np.ndarray","Internal","representation","for","x_batch",".","l_xs",":","np.ndarray","Internal","representation","for","xs_batch",".","e_x",":","np.ndarray","Explanations","for","x.","e_xs",":","np.ndarray","Explanations","for","xs",".","Returns","--","--","--","-","rrs_obj",":","np.ndarray","RRS","maximization","objective","."]
code: def relative_representation_stability_objective(
      self,
      l_x: np.ndarray,
      l_xs: np.ndarray,
      e_x: np.ndarray,
      e_xs: np.ndarray,
  ) -> np.ndarray:
      """
      Computes relative representation stabilities maximization objective
      as defined here https://arxiv.org/pdf/2203.06877.pdf by the authors.
  
      Parameters
      ----------
      l_x: np.ndarray
          Internal representation for x_batch.
      l_xs: np.ndarray
          Internal representation for xs_batch.
      e_x: np.ndarray
          Explanations for x.
      e_xs: np.ndarray
          Explanations for xs.
  
      Returns
      -------
      rrs_obj: np.ndarray
          RRS maximization objective.
      """
  
      num_dim = e_x.ndim
      if num_dim == 4:
          norm_function = lambda arr: np.linalg.norm(
              np.linalg.norm(arr, axis=(-1, -2)), axis=-1
          )  # noqa
      elif num_dim == 3:
          norm_function = lambda arr: np.linalg.norm(
              arr, axis=(-1, -2)
          )  # noqa
      elif num_dim == 2:
          norm_function = lambda arr: np.linalg.norm(arr, axis=-1)
      else:
          raise ValueError(
              "Relative Input Stability only supports 4D, 3D and 2D inputs (batch dimension inclusive)."
          )
  
          # fmt: off
          nominator = (e_x - e_xs) / (e_x + (e_x == 0) * self._eps_min)  # prevent division by 0
          nominator = norm_function(nominator)
      # fmt: on
      denominator = l_x - l_xs
      denominator /= (
          l_x + (l_x == 0) * self._eps_min
      )  # prevent division by 0
      denominator = np.linalg.norm(denominator, axis=-1)
      denominator += (denominator == 0) * self._eps_min
      return nominator / denominator
  
file_path: Quantus/quantus/metrics/robustness/relative_representation_stability.py
imports: from __future__ import annotations
  from typing import TYPE_CHECKING, Callable, Dict, List, Optional
  import numpy
  import sys
  from quantus.functions.normalise_func import normalise_by_average_second_moment_estimate
  from quantus.functions.perturb_func import perturb_batch, uniform_noise
  from quantus.helpers.enums import DataType, EvaluationCategory, ModelType, ScoreDirection
  from quantus.helpers.model.model_interface import ModelInterface
  from quantus.helpers.perturbation_utils import make_changed_prediction_indices_func, make_perturb_func
  from quantus.helpers.warn import warn_parameterisation
  from quantus.metrics.base import Metric
label_desc: Use image node_id 3 for calling the RelativeRepresentationStability obj's underlying member method code with example usage: obj.relative_representation_stability_objective(l_x, l_xs, e_x, e_xs) and returns: unknown