Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lime predicts other label than CNN #184

Open
giuliaass opened this issue Feb 12, 2021 · 0 comments
Open

lime predicts other label than CNN #184

giuliaass opened this issue Feb 12, 2021 · 0 comments

Comments

@giuliaass
Copy link

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant