bgreenwell / fastshap

Fast approximate Shapley values in R
https://bgreenwell.github.io/fastshap/
112 stars 18 forks source link

Keras prediction issue #42

Closed dax44 closed 1 year ago

dax44 commented 2 years ago

I've learned Keras sequential model with the following code:

library(keras)
library(fastshap)

x_train <- iris |> 
  slice(1:100) |> 
  select(-Species) |> 
  as.matrix()
y_train <- iris |> 
  slice(1:100) |> 
  select(Species) |> 
  mutate(Species = as.numeric(Species)-1) |> 
  as.matrix()

mod <- keras_model_sequential()
mod |> 
  layer_dense(units = 16, input_shape = 4) |> 
  layer_activation(activation = "relu") |> 
  layer_dropout(rate = 0.3) |> 
  layer_dense(units = 1, activation = "sigmoid")
mod |> 
  compile(optimizer = "nadam",
          loss = loss_binary_crossentropy())

history <- fit(
  object = mod, 
  x = x_train, 
  y = y_train,
  batch_size = 10, 
  epochs = 30,
  validation_split = 0.1
)

Then I tried to build explainer with fastshap explainer

p_fun <- function(object, newdata) predict(object, x = newdata)
shp_exp <- fastshap::explain(mod, 
                             X = x_train, 
                             pred_wrapper = p_fun)
shp_exp

but my shp_exp has only one row... why?

dax44 commented 2 years ago

Is this project died?

mayer79 commented 1 year ago

The predict function should return only one numeric value: https://github.com/bgreenwell/fastshap/issues/14