Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: 0.45.0 update breaks pytorch example on docs #3581

Open
2 of 4 tasks
hongfanmeng opened this issue Mar 20, 2024 · 1 comment · May be fixed by #3591
Open
2 of 4 tasks

BUG: 0.45.0 update breaks pytorch example on docs #3581

hongfanmeng opened this issue Mar 20, 2024 · 1 comment · May be fixed by #3591
Labels
bug Indicates an unexpected problem or unintended behaviour

Comments

@hongfanmeng
Copy link

hongfanmeng commented Mar 20, 2024

Issue Description

Update 0.45.0 has a breaking change

After the update, the pytorch example in the documentation process shap_values ​​incorrectly
https://shap.readthedocs.io/en/latest/example_notebooks/image_examples/image_classification/PyTorch%20Deep%20Explainer%20MNIST%20example.html

shap_numpy = [np.swapaxes(np.swapaxes(s, 1, -1), 1, 2) for s in shap_values]
test_numpy = np.swapaxes(np.swapaxes(test_images.numpy(), 1, -1), 1, 2)

Causing an error in the following code

# plot the feature attributions
shap.image_plot(list(shap_numpy), -test_numpy)

Updating the processing of output to the following version should resolve the issue

# shap_numpy = [np.swapaxes(np.swapaxes(s, 1, -1), 1, 2) for s in shap_values]
shap_numpy = list(np.transpose(shap_values, (4, 0, 2, 3, 1)))
test_numpy = np.swapaxes(np.swapaxes(test_images.numpy(), 1, -1), 1, 2)

Minimal Reproducible Example

See above

Traceback

IndexError                                Traceback (most recent call last)
Cell In[17], line 2
      1 # plot the feature attributions
----> 2 shap.image_plot(list(shap_numpy), -test_numpy)

File ~/miniconda3/envs/iiot/lib/python3.11/site-packages/shap/plots/_image.py:154, in image(shap_values, pixel_values, labels, true_labels, width, aspect, hspace, labelpad, cmap, show)
    152     axes[row, 0].set_title(true_labels[row], **label_kwargs)
    153 axes[row, 0].axis('off')
--> 154 if len(shap_values[0][row].shape) == 2:
    155     abs_vals = np.stack([np.abs(shap_values[i]) for i in range(len(shap_values))], 0).flatten()
    156 else:

IndexError: index 1 is out of bounds for axis 0 with size 1

Expected Behavior

No response

Bug report checklist

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest release of shap.
  • I have confirmed this bug exists on the master branch of shap.
  • I'd be interested in making a PR to fix this bug

Installed Versions

0.45.0

@hongfanmeng hongfanmeng added the bug Indicates an unexpected problem or unintended behaviour label Mar 20, 2024
@CloseChoice CloseChoice linked a pull request Mar 25, 2024 that will close this issue
2 tasks
@CloseChoice
Copy link
Collaborator

Thanks for reporting and a special thanks for figuring out a solution. I created a PR using exactly the code you suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behaviour
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants