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
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='relu'))
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
masking_1 (Masking) (None, 90, 8) 0
lstm_3 (LSTM) (None, 90, 100) 43600
lstm_4 (LSTM) (None, 90, 70) 47880
lstm_5 (LSTM) (None, 70) 39480
dense_2 (Dense) (None, 50) 3550
dense_3 (Dense) (None, 52) 2652
=================================================================
Total params: 137,162
Trainable params: 137,162
Non-trainable params: 0
train the model:
import numpy as np
# Generate synthetic data
num_samples = 5000
input_shape = (num_samples, 90, 8)
output_shape = (num_samples, 52)
# Example input data (random values)
X_train = np.random.rand(*input_shape)
# Example target data (random values)
y_train = np.random.rand(*output_shape)
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
batch_size = 256
epochs = 10
model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.2)
run shap:
import shap
de = shap.DeepExplainer(model, data=X_train[:100])
shap_values = de.shap_values(X_train[101:102])
I'm getting error:
AttributeError: Exception encountered when calling layer "lstm_3" " f"(type LSTM).
'TFDeep' object has no attribute 'between_tensors'
Call arguments received by layer "lstm_3" " 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
versions:
shap.__version__ == 0.44.1
tf.__version__ == 2.10.0
- What is wrong ?
- how can I fix and run shap on my example ?