I’m trying to run shap on my tensorflow (time series model) and getting error.
I built a simple example which I got the same error:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Masking, LSTM, Dense
import numpy as np
import shap
model = Sequential()
model.add(Masking(mask_value=0.0, input_shape=(90, 8)))
model.add(LSTM(100, return_sequences=True))
model.add(LSTM(70, return_sequences=True))
model.add(LSTM(70, return_sequences=False)) # Output shape (None, 70)
model.add(Dense(50, activation='relu'))
model.add(Dense(52, activation='softmax'))
num_samples = 5000
input_shape = (num_samples, 90, 8)
X_train = np.random.rand(*input_shape)
y_train_int = tf.random.uniform(shape=(num_samples,), minval=0, maxval=51, dtype=tf.int32)
y_train_categorical = tf.keras.utils.to_categorical(y_train_int, num_classes=52)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
batch_size = 256
epochs = 5
model.fit(X_train, y_train_categorical, batch_size=batch_size, epochs=epochs, validation_split=0.2)
de = shap.DeepExplainer(model, data=X_train[:100])
Now, running the following code line:
shap_values = de.shap_values(X_train[101:102])
gives error:
AttributeError: Exception encountered when calling layer "lstm" " f"(type LSTM).
'TFDeep' object has no attribute 'between_tensors'
Call arguments received by layer "lstm" " f"(type LSTM):
• inputs=tf.Tensor(shape=(200, 90, 8), dtype=float32)
• mask=tf.Tensor(shape=(200, 90), dtype=bool)
• training=False
• initial_state=None
Version:
shap.__version__ == 0.44.1
tf.__version__ == 2.10.0
What is wrong ?
how can I fix and run shap on my example ?