1111
1212class Interpreter (ABC ):
1313 """
14- Interpreter is the base abstract class for all interpretation algorithms.
15- Interpreters should (1) prepare the ``self.predict_fn`` that outputs probability predictions, gradients or other
16- desired intermediate results of the model, and (2) implement the core function ``interpret`` of the interpretation
17- algorithm.
14+ Interpreter is the base abstract class for all Interpreters.
15+ The implementation of Interpreters should (1) prepare the ``self.predict_fn`` that outputs probability predictions,
16+ gradients or other desired intermediate results of the model, and (2) implement the core function ``interpret`` of
17+ the interpretation algorithm.
18+ This kind of implementation works for all post-poc interpretation algorithms. While there are other algorithms that
19+ may have different features, this kind of implementation can cover most of them. So we follow this design for all
20+ Interpreters in this library.
1821
1922 Three sub-abstract Interpreters that implement ``self.predict_fn`` are currently provided in this file:
2023 ``InputGradientInterpreter``, ``InputOutputInterpreter``, ``IntermediateLayerInterpreter``. For each of them, the
2124 implemented ``predict_fn`` can be used by several different algorithms. Therefore, the further implementations can
22- focus on the core algorithm.
23-
24- Args:
25- paddle_model (callable): A model with ``forward`` and possibly ``backward`` functions.
26- device (str): The device used for running `paddle_model`, options: ``cpu``, ``gpu:0``, ``gpu:1`` etc.
27- use_cuda (bool): Would be deprecated soon. Use ``device`` directly.
25+ focus on the core algorithm. More sub-abstract Interpreters will be provided if necessary.
2826 """
2927
3028 def __init__ (self , paddle_model : callable , device : str , use_cuda : bool = None , ** kwargs ):
29+ """
30+
31+ Args:
32+ paddle_model (callable): A model with ``forward`` and possibly ``backward`` functions.
33+ device (str): The device used for running `paddle_model`, options: ``cpu``, ``gpu:0``, ``gpu:1`` etc.
34+ """
3135 self .device = device
3236 self .paddle_model = paddle_model
3337 self .predict_fn = None
@@ -54,13 +58,16 @@ def interpret(self, **kwargs):
5458 raise NotImplementedError
5559
5660 def _build_predict_fn (self , ** kwargs ):
57- """Build self.predict_fn for interpreters."""
61+ """ Build self.predict_fn for interpreters. This will be called by interpret(). """
5862 raise NotImplementedError
5963
6064 def _paddle_env_setup (self ):
6165 """Prepare the environment setup. This is not always necessary because the setup can be done within the
62- function of ``_build_predict_fn``. This function is a simple implementation for disabling gradient computation.
66+ function of ``_build_predict_fn``.
6367 """
68+ #######################################################################
69+ # This is a simple implementation for disabling gradient computation. #
70+ #######################################################################
6471 import paddle
6572 if not paddle .is_compiled_with_cuda () and self .device [:3 ] == 'gpu' :
6673 print ("Paddle is not installed with GPU support. Change to CPU version now." )
@@ -79,11 +86,16 @@ class InputGradientInterpreter(Interpreter):
7986 ``InputGradientInterpreter`` are used by input gradient based Interpreters. Interpreters that are derived from
8087 ``InputGradientInterpreter``: ``GradShapCVInterpreter``, ``IntGradCVInterpreter``, ``SmoothGradInterpreter``.
8188
82- The ``predict_fn`` provided by this interpreter will output input gradient given an input.
83-
89+ The ``predict_fn`` in this interpreter will return input gradient given an input.
8490 """
8591
8692 def __init__ (self , paddle_model : callable , device : str , use_cuda : bool = None , ** kwargs ):
93+ """
94+
95+ Args:
96+ paddle_model (callable): A model with ``forward`` and possibly ``backward`` functions.
97+ device (str): The device used for running `paddle_model`, options: ``cpu``, ``gpu:0``, ``gpu:1`` etc.
98+ """
8799 Interpreter .__init__ (self , paddle_model , device , use_cuda , ** kwargs )
88100 assert hasattr (paddle_model , 'forward' ), \
89101 "paddle_model has to be " \
@@ -197,6 +209,12 @@ class InputOutputInterpreter(Interpreter):
197209 """
198210
199211 def __init__ (self , paddle_model : callable , device : str , use_cuda : bool = None , ** kwargs ):
212+ """
213+
214+ Args:
215+ paddle_model (callable): A model with ``forward`` and possibly ``backward`` functions.
216+ device (str): The device used for running `paddle_model`, options: ``cpu``, ``gpu:0``, ``gpu:1`` etc.
217+ """
200218 Interpreter .__init__ (self , paddle_model , device , use_cuda , ** kwargs )
201219 assert hasattr (paddle_model , 'forward' ), \
202220 "paddle_model has to be " \
@@ -259,11 +277,17 @@ class IntermediateLayerInterpreter(Interpreter):
259277 Interpreters that are derived from ``IntermediateLayerInterpreter``:
260278 ``RolloutInterpreter``, ``ScoreCAMInterpreter``.
261279
262- The ``predict_fn`` provided by this interpreter will output the model's intermediate outputs given an input.
263-
280+ The ``predict_fn`` provided by this interpreter will return the model's intermediate outputs given an input.
264281 """
265282
266283 def __init__ (self , paddle_model : callable , device : str , use_cuda : bool = None , ** kwargs ):
284+ """
285+
286+ Args:
287+ paddle_model (callable): A model with ``forward`` and possibly ``backward`` functions.
288+ device (str): The device used for running `paddle_model`, options: ``cpu``, ``gpu:0``, ``gpu:1`` etc.
289+ """
290+
267291 Interpreter .__init__ (self , paddle_model , device , use_cuda , ** kwargs )
268292 assert hasattr (paddle_model , 'forward' ), \
269293 "paddle_model has to be " \
@@ -272,16 +296,15 @@ def __init__(self, paddle_model: callable, device: str, use_cuda: bool = None, *
272296 def _build_predict_fn (self , rebuild : bool = False , target_layer : str = None , target_layer_pattern : str = None ):
273297 """Build self.predict_fn for IntermediateLayer based algorithms.
274298 The model is supposed to be a classification model.
275- target_layer and target_layer_pattern cannot be set at the same time.
299+ `` target_layer`` and `` target_layer_pattern`` cannot be set at the same time. See the arguments below .
276300
277301 Args:
278302 rebuild (bool, optional): forces to rebuild. Defaults to False.
279- target_layer (str, optional): the name of the desired layer whose features will output.
280- This is used when there is only one layer to output. Conflict with ``target_layer_pattern``.
281- Defaults to None.
282- target_layer_pattern (str, optional): the pattern name of the layers whose features will output.
283- This is used when there are several layers to output and they share a common pattern name.
284- Conflict with ``target_layer``. Defaults to None.
303+ target_layer (str, optional): the name of the desired layer whose features will output. This is used when
304+ there is only one layer to output. Conflict with ``target_layer_pattern``. Defaults to None.
305+ target_layer_pattern (str, optional): the pattern name of the layers whose features will output. This is
306+ used when there are several layers to output and they share a common pattern name. Conflict with
307+ ``target_layer``. Defaults to None.
285308 """
286309
287310 if self .predict_fn is not None :
0 commit comments