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: 'tuple' object has no attribute 'as_list' #3645

Closed
2 of 4 tasks
Amirhossein4096 opened this issue May 8, 2024 · 5 comments · Fixed by #3677
Closed
2 of 4 tasks

BUG: 'tuple' object has no attribute 'as_list' #3645

Amirhossein4096 opened this issue May 8, 2024 · 5 comments · Fixed by #3677
Labels
awaiting feedback Indicates that further information is required from the issue creator

Comments

@Amirhossein4096
Copy link

Amirhossein4096 commented May 8, 2024

Issue Description

I used shap to interpret my cnn model. When shap tries to get the output shape in its internal function, i get error. I know it's because of the tensorflow version, how can I fix this error?
There isn't problem in tensorflow==2.15.0
Please solve this problem for tensorflow==2.16.1

Minimal Reproducible Example

import shap
explainer = shap.DeepExplainer(model=model, data = X_train[0:10])
shap_values = explainer.shap_values(X_test[0:10])

Traceback

AttributeError
Traceback (most recent call last)
<ipython-input-9-5d9f53dcf3ce> in <cell line: 1>()
----> 1 explainer = shap.DeepExplainer(model=model, data = X_train[0:10])
      2 shap_values = explainer.shap_values(X_test[0:10])
1 frames
/usr/local/lib/python3.10/dist-packages/shap/explainers/_deep/deep_tf.
py in __init__(self, model, data, 
session, learning_phase_flags)
    170             self.phi_symbolics = [None]
    171         else:
--> 172             noutputs = self.model_output.shape.as_list()[1]
    173             if noutputs is not None:
    174                 self.phi_symbolics = [None for i in range(noutputs)]

AttributeError: 'tuple' object has no attribute 'as_list'

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

my package versions:
tensorflow==2.16.1
shap==0.45.1

@Amirhossein4096 Amirhossein4096 added the bug Indicates an unexpected problem or unintended behaviour label May 8, 2024
@CloseChoice
Copy link
Collaborator

Thanks for reporting @Amirhossein4096 . Would you be so kind and provide an example that reproduces the issue? Your code is not complete since we cannot define the model in it. Would be great if you could trim your code down to the minimal steps needed to produce this bug.

@CloseChoice CloseChoice added awaiting feedback Indicates that further information is required from the issue creator and removed bug Indicates an unexpected problem or unintended behaviour labels May 14, 2024
@NanK77
Copy link

NanK77 commented May 16, 2024

I am using same versions of TensorFlow and Shap. With a LSTM model, I've received same error:

AttributeError Traceback (most recent call last)
Cell In[51], line 11
9 background=shap.sample(scaled_train_X,100)
10 # Create the SHAP DeepExplainer using the model and the reshaped background dataset
---> 11 explainer = shap.DeepExplainer(model, background)
13 # Calculate SHAP values for the reshaped test set
14 shap_values = explainer.shap_values(scaled_test_X)

File ~\AppData\Local\anaconda3\Lib\site-packages\shap\explainers_deep_init_.py:90, in DeepExplainer.init(self, model, data, session, learning_phase_flags)
87 super().init(model, masker)
89 if framework == 'tensorflow':
---> 90 self.explainer = TFDeep(model, data, session, learning_phase_flags)
91 elif framework == 'pytorch':
92 self.explainer = PyTorchDeep(model, data)

File ~\AppData\Local\anaconda3\Lib\site-packages\shap\explainers_deep\deep_tf.py:172, in TFDeep.init(self, model, data, session, learning_phase_flags)
170 self.phi_symbolics = [None]
171 else:
--> 172 noutputs = self.model_output.shape.as_list()[1]
173 if noutputs is not None:
174 self.phi_symbolics = [None for i in range(noutputs)]

AttributeError: 'tuple' object has no attribute 'as_list'

@CloseChoice
Copy link
Collaborator

@NanK77 would you be so kind to share code so that we can reproduce the issue? We need a definition of the data (at best a minimal set of row) and code to construct the model.

@macayla-donegan
Copy link

I'm also running into this issue- I can't really share code that reproduces the issue because I work in health tech, but the inputs to shap.DeepExplainer are a 3 layer model trained in tensorflow:
simple_nn = Sequential()
simple_nn.add(Dense(256, activation='relu', input_shape=(len(self.feature_names),)))
simple_nn.add(Dense(256, activation='relu'))
simple_nn.add(Dropout(0.2))
simple_nn.add(Dense(256, activation='relu'))
simple_nn.add(Dense(1, activation='sigmoid'))

and a dataframe of numerical features with no nans

can someone let me know if they found a workaround for this?

Here's the exact error I'm getting

AttributeError Traceback (most recent call last)
Cell In[60], line 3
1 import shap
----> 3 explainer = shap.DeepExplainer(nn_model, X_train)
4 shap_values = explainer.shap_values(X_test)

File /opt/conda/lib/python3.10/site-packages/shap/explainers/_deep/init.py:84, in DeepExplainer.init(self, model, data, session, learning_phase_flags)
81 framework = 'tensorflow'
83 if framework == 'tensorflow':
---> 84 self.explainer = TFDeep(model, data, session, learning_phase_flags)
85 elif framework == 'pytorch':
86 self.explainer = PyTorchDeep(model, data)

File /opt/conda/lib/python3.10/site-packages/shap/explainers/_deep/deep_tf.py:172, in TFDeep.init(self, model, data, session, learning_phase_flags)
170 self.phi_symbolics = [None]
171 else:
--> 172 noutputs = self.model_output.shape.as_list()[1]
173 if noutputs is not None:
174 self.phi_symbolics = [None for i in range(noutputs)]

AttributeError: 'tuple' object has no attribute 'as_list'

@CloseChoice
Copy link
Collaborator

Sorry, should have known this before, but we currently do not support tensorflow 2.16 right now. See https://github.com/shap/shap/blob/master/pyproject.toml#L74. Will close this here, since support of tf 2.16 and keras 3 is way more complicated than this here. See the mentioned issue #3672

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting feedback Indicates that further information is required from the issue creator
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants