thomasp85 / lime

Local Interpretable Model-Agnostic Explanations (R port of original Python package)
https://lime.data-imaginist.com/
Other
481 stars 109 forks source link

lime predicts other label than CNN #184

Open giuliaass opened 3 years ago

giuliaass commented 3 years ago

Hi,

I'm working on a Text classification Problem with Keras and lime. My code talks about credit risk which is a classification problem, Yes or No, When I predict the outcome for the first four Test Data I get: Yes No No Yes. But when I explain my model with lime I get for the first four cases: No Yes Yes Yes. Why is this the case and how can I fix it?

Thank you in advance!!

My code is the following:

y_pred <- model_CNN_K_2T %>% predict_classes(x_test) y_pred_prob <- model_CNN_K_2T %>% predict(x_test)

########################

Explanation of the model

########################

install.packages("lime")

library(lime)

class(model_CNN_K_2T)

Setup lime::model_type() function for keras

model_type.keras.engine.sequential.Sequential <- function(x, ...) { return("classification") }

Setup lime::predict_model() function for keras

predict_model.keras.engine.sequential.Sequential <- function(x, newdata, type, ...) { pred <- predict_proba(object = x, x = as.matrix(newdata)) return(data.frame(Yes = pred, No = 1 - pred)) }

predict_model.keras.engine.sequential.Sequential(x = model_CNN_K_2T, newdata = x_test, type = 'raw') %>% tibble::as_tibble()

get_embedding_explanation <- function(text) { tokenizer %>% fit_text_tokenizer(text) text_to_seq <- texts_to_sequences(tokenizer, text) sentences <- text_to_seq %>% pad_sequences(maxlen = maxlen) }

sentence_to_explain <- as.character(Refinitiv_data_test$Text[1:4])

explainer <- lime(Refinitiv_data_train$Text, model = model_CNN_K_2T, preprocess = get_embedding_explanation, bin_continuous = FALSE)

Get explanation for the first 20 words

explanation <- lime::explain(sentence_to_explain, explainer, n_features = 20, n_labels = 1, kernel_width = 0.5)

Different graphical ways to show the same information

plot_text_explanations(explanation) plot_features(explanation)

Show Prediction

y_pred[1:4] y_pred_prob[1:4]