将石灰与带有顺序模型的 keras 一起使用 - 为数据框提供的 'dimnames' 无效

Using lime with keras with a sequential model - invalid 'dimnames' given for data frame

我有两个数据集,一个用于训练,另一个用于外部验证。

我正在尝试在通过 Keras 构建的简单顺序模型上使用 Lime:

# Building
build_model <- function() {                                
  model <- keras_model_sequential() %>%
    layer_dense(units = 25,
                activation = "relu",
                input_shape = dim(X_pca_scores_scaled)[[2]]) %>%
    layer_dropout(rate = 0.1) %>%
    layer_dense(units = 25, activation = "relu") %>%
    layer_dropout(rate = 0.1) %>%
    layer_dense(units = 1)
  model %>% compile(
    optimizer = "adam",
    loss = "mse",
    metrics = c("mae")
  )
}

# Training
model <- build_model()
model %>% fit(as.matrix(X_pca_scores_scaled), train_targets,                    
              epochs = num_epochs, verbose = 1, 
              callbacks = callback_early_stopping(monitor = "mae",
                                                  patience = 5,
                                                  min_delta = 0.25,
                                                  mode = "min"))

然后我先尝试在训练数据上使用 Lime:

explainer <- lime(X_pca_scores_scaled, model,  bin_continuous = TRUE, n_bins = 4)
explanation <- lime::explain(X_pca_scores_scaled, explainer, n_features = 10)
plot_features (explanation)

然而我 运行 在调用 lime::explain

时出现以下错误
Error in `dimnames<-.data.frame`(`*tmp*`, value = list(n)) :   
invalid 'dimnames' given for data frame

训练数据集和测试数据集都设置为data.frames,我以为可以解决问题,但没有。我也尝试关注此线程,但无法弄清楚如何将他们的建议实施到我的代码中。

所以我发布的 link 中的答案确实有效,我只是在函数描述中犯了一个愚蠢的错误。这对我有用:

######################
# LIME
######################
model_type.keras.engine.sequential.Sequential <- function(x, ...) {
  "regression"
}

# Setup lime::predict_model()
predict_model.keras.engine.sequential.Sequential <- function (x, newdata, type, ...) {
  
  ## here you have to write function, that takes a data.frame
  ## and transform it to shape that keras understands
  
  ## for example if u flatten your array before training CNN, you just use
  ## as-matrix()
  
  ## if keras model expect 3d dataset, you write something like
  ## as.array(newdata, dims(n,12,12))
  
  your_function <- function(data){
    as.matrix(newdata)
  }
  
  pred <- predict(object = x, x = your_function(newdata))
  data.frame (pred) }


x <- as.data.frame(your_train_data)  
x2 <- as.data.frame(your_test_data)  

explainer <- lime(x = x, model= model)



explanation <- lime::explain (
  x=  x2[1:10,], 
  explainer, n_features = 4) 


plot_features (explanation) +
  labs (title = "LIME: Feature Importance Visualization")