简体   繁体   中英

SHAP ValueError: Dimension 1 in both shapes must be equal, but are 2 and 1. Shapes are [?,2] and [?,1]

Based on a previously trained feed-forward network, I tried to use SHAP to get the feature importance. I followed all the steps described in the documentation but I am still receiving the following error

ValueError: Dimension 1 in both shapes must be equal, but are 2 and 1. Shapes are [?,2] and [?,1]

The following code produces a reproduciple example that has the same error.

import pandas as pd
from numpy.random import randint
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, BatchNormalization, Dropout, Activation
from keras.optimizers import Adam
import shap

# Train_x data creation
train_x = pd.DataFrame({
    'v1': randint(2, 20, 1489),
    'v2': randint(50, 200, 1489),
    'v3': randint(30, 90, 1489),
    'v4': randint(100, 150, 1489)
})

# Train_y data creation
train_y = randint(0, 2, 1489)

# One-hot encoding as I use categorical cross-entropy
train_y = to_categorical(train_y, num_classes=2)

# Start construction of a DNN Sequential model.
model = Sequential()

# First input layer with a dropout and batch normalization layer following
model.add(Dense(256, input_dim=train_x.shape[1]))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(rate=0.2))

# Output layer
model.add(Dense(2))
model.add(Activation('softmax'))

# Use the Adam optimizer
optimizer = Adam(lr=0.001)

# Compile the model
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

model.summary()

# Fit model
hist = model.fit(train_x, train_y, epochs=100, batch_size=128, shuffle=False, verbose=2)

# SHAP calculation
explainer = shap.DeepExplainer(model, train_x)
shap_values = explainer.shap_values(train_x[:500].values)

where I have an input shape of (None, 4) and a softmax activation function at the end with 2 neurons as I use it for binary classification. The train_x data on the following code snippet are a pandas data frame of shape (1489, 4) .

I tried to change the train_x shape but I had a similar error. Any help would be much appreciated.

Please see below a working example for binary classification with TF:

from numpy.random import randint
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, BatchNormalization, Dropout, Activation
from tensorflow.keras.optimizers import Adam
import shap
import tensorflow
print(shap.__version__, "\n",tensorflow.__version__)

# Train_x data creation
train_x = pd.DataFrame({
    'v1': randint(2, 20, 1489),
    'v2': randint(50, 200, 1489),
    'v3': randint(30, 90, 1489),
    'v4': randint(100, 150, 1489)
})

# Train_y data creation
train_y = randint(0, 2, 1489)

# One-hot encoding as I use categorical cross-entropy
train_y = to_categorical(train_y, num_classes=2)

# Start construction of a DNN Sequential model.
model = Sequential()

# First input layer with a dropout and batch normalization layer following
model.add(Dense(256, input_dim=train_x.shape[1]))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(rate=0.2))

# Output layer
model.add(Dense(2))
model.add(Activation('softmax'))

# Use the Adam optimizer
optimizer = Adam(lr=0.001)

# Compile the model
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

# model.summary()

# Fit model
hist = model.fit(train_x, train_y, epochs=100, batch_size=128, shuffle=False, verbose=0)

# SHAP calculation
shap.explainers._deep.deep_tf.op_handlers["AddV2"] = shap.explainers._deep.deep_tf.passthrough
explainer = shap.DeepExplainer(model, train_x)

shap_values = explainer.shap_values(train_x[:500].values)

shap.summary_plot(shap_values[1])

0.38.2 
 2.2.0

在此处输入图像描述

Note couple of things:

  1. Package versions (tf should be below 2.4 I believe)
  2. Addition of "AddV2" (see discussion here )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM