You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

_attribution.py 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Attribution."""
  16. from typing import Callable
  17. import mindspore as ms
  18. class Attribution:
  19. r"""
  20. Basic class of attributing the salient score
  21. The explainers which explanation through attributing the relevance scores
  22. should inherit this class.
  23. Args:
  24. network (ms.nn.Cell): The black-box model to explanation.
  25. """
  26. def __init__(self, network):
  27. self._verify_model(network)
  28. self._model = network
  29. @staticmethod
  30. def _verify_model(model):
  31. """
  32. Verify the input `network` for __init__ function.
  33. """
  34. if not isinstance(model, ms.nn.Cell):
  35. raise TypeError("The parsed `network` must be a `mindspore.nn.Cell` object.")
  36. __call__: Callable
  37. """
  38. The explainers return the explanations by calling directly on the explanation.
  39. Derived class should overwrite this implementations for different
  40. algorithms.
  41. Args:
  42. input (ms.Tensor): Input tensor to be explained.
  43. Returns:
  44. - saliency map (ms.Tensor): saliency map of the input.
  45. """
  46. @property
  47. def model(self):
  48. return self._model