ModelOriented / shapper

An R wrapper of SHAP python library
https://modeloriented.github.io/shapper/
58 stars 16 forks source link

Issue in running SHAP with Keras model in R #24

Closed KhawlaSeddiki closed 4 years ago

KhawlaSeddiki commented 4 years ago

I am running SHAP from the library shapper in R for a classification model intrepetation on a Keras 1D CNN model:

library(keras)
library("shapper")
library("DALEX")

I made a simple reproductible example

mdat.train <- cbind(rep(1:2, each = 5), matrix(c(1:30), ncol = 3, byrow = TRUE))
train.conv <- array_reshape(mdat.train[,-1], c(nrow(mdat.train[,-1]), ncol(mdat.train[,-1]), 1))

mdat.test <- cbind(rep(1:2, each = 3), matrix(c(1:18), ncol = 3, byrow = TRUE))
test.conv <- array_reshape(mdat.test[,-1], c(nrow(mdat.test[,-1]), ncol(mdat.test[,-1]), 1))

My CNN model

model.CNN <- keras_model_sequential()
model.CNN %>% 
  layer_conv_1d(filters=16L, kernel_initializer=initializer_he_normal(seed=NULL), kernel_size=2L, input_shape = c(dim(train.conv)[[2]],1)) %>%
  layer_batch_normalization() %>%
  layer_activation_leaky_relu() %>%

  layer_flatten() %>%
  layer_dense(50, activation ="relu") %>%
  layer_dropout(rate=0.5) %>%
  layer_dense(units=2, activation ='sigmoid')

model.CNN %>% compile(
  loss = loss_binary_crossentropy,
  optimizer = optimizer_adam(lr = 0.001, beta_1 = 0.9, beta_2 = 0.999, epsilon = 1e-08),
  metrics = c("accuracy"))

model.CNN %>% fit(
  train.conv, mdat.train[,1], epochs = 5, verbose = 1)

My Shap command

p_function <- function(model, data) predict(model.CNN, test.conv, type = "prob")
exp_cnn <- explain(model.CNN, data = train.conv)
ive_cnn <- shap(exp_cnn, data = train.conv, new_observation = test.conv, predict_function = p_function)

I am getting this error :

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: operands could not be broadcast together with shapes (2,6) (10,) 
maksymiuks commented 4 years ago

Solved by me at: https://stackoverflow.com/questions/59724406/shap-with-keras-model-operands-could-not-be-broadcast-together-with-shapes-2/60443872#60443872