Repository: understandable-machine-intelligence-lab/Quantus
Issue: Whether some interpretable methods for regression tasks can be added
Root cause: Unimplemented Feature Gap

Through some tests, I found that most of the current methods are only applicable to classification tasks.  When I use the following 1d CNN for regression task prediction, I always report an error like: gather_out_cpu(): Expected dtype int64 for index.

metric_init = quantus.MaxSensitivity(nr_samples=10,
    lower_bound=0.1,
    norm_numerator=quantus.fro_norm,
    norm_denominator=quantus.fro_norm,
    perturb_func=quantus.uniform_noise,
    similarity_func=quantus.difference,
    disable_warnings=True,
    normalise=True,
    abs=True)

scores_saliency = metric_init(model=cnn,
                                x_batch=test_x,
                                y_batch=y_test,
                                  a_batch=None,
                                  device=device,
                                  explain_func=quantus.explain,
                                  explain_func_kwargs={"method": "Saliency"})#Gradient

At the same time, I found that when I use indicators to evaluate the Tensorflow model, there is always the parameter device. Does this mean that I have to follow both tensorflow and pythoch

The error code of the regression task is as follows：
File "C:\Users\.conda\envs\pytorch1\lib\site-packages\captum\attr\_core\saliency.py", line 131, in attribute
    self.forward_func, inputs, target, additional_forward_args
  File "C:\Users\.conda\envs\pytorch1\lib\site-packages\captum\_utils\gradient.py", line 112, in compute_gradients
    outputs = _run_forward(forward_fn, inputs, target_ind, additional_forward_args)
  File "C:\Users\.conda\envs\pytorch1\lib\site-packages\captum\_utils\common.py", line 461, in _run_forward
    return _select_targets(output, target)
  File "C:\Users\.conda\envs\pytorch1\lib\site-packages\captum\_utils\common.py", line 478, in _select_targets
    return torch.gather(output, 1, target.reshape(len(output), 1))
RuntimeError: gather_out_cpu(): Expected dtype int64 for index

I guess the error is caused by the fact that the output of the regression task is not an integer.


I tried the following methods and found that the results can be obtained：y_test = y_test.numpy().astype(np.int64)

At the same time, when I perform the 1dcnn test in TensorFlow：
metric_init = quantus.MaxSensitivity(nr_samples=10,
    lower_bound=0.1,
    norm_numerator=quantus.fro_norm,
    norm_denominator=quantus.fro_norm,
    perturb_func=quantus.uniform_noise,
    similarity_func=quantus.difference,
    disable_warnings=True,
    normalise=True,
    abs=True,)

scores_grad = metric_init(model=model,
                            x_batch=X_train,
                            y_batch=y_train,
                            a_batch=None,
                            explain_func=quantus.explain,
                            explain_func_kwargs={"method": "Gradient"})

 I find that it always reports errors：

File "C:\Users\.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in wrapper
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
    C:\Users\.conda\envs\tf-gpu\lib\site-packages\tf_explain\utils\image.py:40 transform_to_normalized_grayscale  *
        normalized_tensor = tf.cast(
    C:\Users\.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\util\dispatch.py:206 wrapper  **
        return target(*args, **kwargs)
    C:\Users\.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\ops\image_ops_impl.py:1928 per_image_standardization
        image = _AssertAtLeast3DImage(image)
    C:\Users\.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\ops\image_ops_impl.py:200 _AssertAtLeast3DImage
        _CheckAtLeast3DImage(image, require_static=False), image)
    C:\Users\.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\ops\image_ops_impl.py:225 _CheckAtLeast3DImage
        image.shape)
    ValueError: 'image' (shape (1, 9)) must be at least three-dimensional.

The reason for the error seems to be that low dimensional data is not supported

The same data and model can run normally in Pytorch and obtain explanatory results

At the same time, I found a bug when testing the Faithful Correlation. The following errors are always reported：、
AttributeError: 'list' object has no attribute 'shape'
But when I test other indicators, I can output the results normally.


Thank you @9527-ly we'll look into this!

> Thank you @9527-ly we'll look into this!

It's my honor to communicate with you. At the same time, I suggest adding modelless methods such as SHAP to better support various models and prediction tasks.

> Thank you @9527-ly we'll look into this!

I sincerely suggest that these evaluation metrics can take the regression task into account. It is hoped that this toolkit can be applied to more models and a wider range of prediction tasks. In other words, when selecting a metric, you can have a parameter to specify what task type the metric is used for.

Thank you for these suggestions. @dilyabareeva can you have a look at the diffs between tf and torch?

> At the same time, when I perform the 1dcnn test in TensorFlow： metric_init = quantus.MaxSensitivity(nr_samples=10, lower_bound=0.1, norm_numerator=quantus.fro_norm, norm_denominator=quantus.fro_norm, perturb_func=quantus.uniform_noise, similarity_func=quantus.difference, disable_warnings=True, normalise=True, abs=True,)
> 
> scores_grad = metric_init(model=model, x_batch=X_train, y_batch=y_train, a_batch=None, explain_func=quantus.explain, explain_func_kwargs={"method": "Gradient"})
> 
> I find that it always reports errors：
> 
> File "C:\Users.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in wrapper raise e.ag_error_metadata.to_exception(e) ValueError: in user code: C:\Users.conda\envs\tf-gpu\lib\site-packages\tf_explain\utils\image.py:40 transform_to_normalized_grayscale * normalized_tensor = tf.cast( C:\Users.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\util\dispatch.py:206 wrapper ** return target(*args, **kwargs) C:\Users.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\ops\image_ops_impl.py:1928 per_image_standardization image = _AssertAtLeast3DImage(image) C:\Users.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\ops\image_ops_impl.py:200 _AssertAtLeast3DImage _CheckAtLeast3DImage(image, require_static=False), image) C:\Users.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\ops\image_ops_impl.py:225 _CheckAtLeast3DImage image.shape) ValueError: 'image' (shape (1, 9)) must be at least three-dimensional.
> 
> The reason for the error seems to be that low dimensional data is not supported
> 
> The same data and model can run normally in Pytorch and obtain explanatory results

@9527-ly Thank you for your feedback! Please be mindful that Quantus allows you to chose explanation methods freely and you can easily plug in your own method by passing attributor (see out tutorial [here](https://github.com/understandable-machine-intelligence-lab/Quantus/blob/main/tutorials/Tutorial_Explore_Explanation_Wrappers.ipynb)). By default, when using tensorflow Quantus employs tf-explain package which unfortunately currently only supports images (not one-dimensional inputs). Please pass a more appropriate attributor as per the tutorial. In the meanwhile, we will work on a more sustainable solution.

> > At the same time, when I perform the 1dcnn test in TensorFlow： metric_init = quantus.MaxSensitivity(nr_samples=10, lower_bound=0.1, norm_numerator=quantus.fro_norm, norm_denominator=quantus.fro_norm, perturb_func=quantus.uniform_noise, similarity_func=quantus.difference, disable_warnings=True, normalise=True, abs=True,)
> > scores_grad = metric_init(model=model, x_batch=X_train, y_batch=y_train, a_batch=None, explain_func=quantus.explain, explain_func_kwargs={"method": "Gradient"})
> > I find that it always reports errors：
> > File "C:\Users.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in wrapper raise e.ag_error_metadata.to_exception(e) ValueError: in user code: C:\Users.conda\envs\tf-gpu\lib\site-packages\tf_explain\utils\image.py:40 transform_to_normalized_grayscale * normalized_tensor = tf.cast( C:\Users.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\util\dispatch.py:206 wrapper ** return target(*args, **kwargs) C:\Users.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\ops\image_ops_impl.py:1928 per_image_standardization image = _AssertAtLeast3DImage(image) C:\Users.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\ops\image_ops_impl.py:200 _AssertAtLeast3DImage _CheckAtLeast3DImage(image, require_static=False), image) C:\Users.conda\envs\tf-gpu\lib\site-packages\tensorflow\python\ops\image_ops_impl.py:225 _CheckAtLeast3DImage image.shape) ValueError: 'image' (shape (1, 9)) must be at least three-dimensional.
> > The reason for the error seems to be that low dimensional data is not supported
> > The same data and model can run normally in Pytorch and obtain explanatory results
> 
> @9527-ly Thank you for your feedback! Please be mindful that Quantus allows you to chose explanation methods freely and you can easily plug in your own method by passing attributor (see out tutorial [here](https://github.com/understandable-machine-intelligence-lab/Quantus/blob/main/tutorials/Tutorial_Explore_Explanation_Wrappers.ipynb)). By default, when using tensorflow Quantus employs tf-explain package which unfortunately currently only supports images (not one-dimensional inputs). Please pass a more appropriate attributor as per the tutorial. In the meanwhile, we will work on a more sustainable solution.

@dilyabareeva .Thanks a lot.