question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem using LIME with Keras

See original GitHub issue

I train a model like so

model = Sequential() model.add(Dense(200, input_dim=11, kernel_initializer=‘normal’, activation=‘relu’)) model.add(Dropout(0.3)) model.add(Dense(200, activation=‘relu’)) model.add(Dropout(0.3)) model.add(Dense(200, activation=‘relu’)) model.add(Dropout(0.3)) model.add(Dense(1, activation=‘relu’)) model.compile(loss=‘mean_squared_error’, optimizer=‘adam’) # Fit the model model.fit(X_train, y_train, epochs=200, batch_size=512)`

Then I try to make a LIME prediction…

`import lime import lime.lime_tabular import pandas as pd

explainer = lime.lime_tabular.LimeTabularExplainer(df.as_matrix(), feature_names=df.columns, class_names=[‘Price’], verbose=True, mode=‘regression’)

exp = explainer.explain_instance(qc_reshape[0], model.predict, num_features=len(df.columns))

exp.show_in_notebook(show_table=True)

exp.as_list()`

After that I get the error…

`AssertionError Traceback (most recent call last) /anaconda3/envs/LIME/lib/python3.6/site-packages/lime/lime_tabular.py in explain_instance(self, data_row, predict_fn, labels, top_labels, num_features, num_samples, distance_metric, model_regressor) 300 try: –> 301 assert isinstance(yss, np.ndarray) and len(yss.shape) == 1 302 except AssertionError:

AssertionError:

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last) <ipython-input-153-d3c739459480> in <module>() 8 verbose=True, mode=‘regression’) 9 —> 10 exp = explainer.explain_instance(qc_reshape[0], model.predict, num_features=len(df.columns)) 11 12 exp.show_in_notebook(show_table=True)

/anaconda3/envs/LIME/lib/python3.6/site-packages/lime/lime_tabular.py in explain_instance(self, data_row, predict_fn, labels, top_labels, num_features, num_samples, distance_metric, model_regressor) 302 except AssertionError: 303 raise ValueError(“Your model needs to output single-dimensional
–> 304 numpyarrays, not arrays of {} dimensions”.format(yss.shape)) 305 306 predicted_value = yss[0]

ValueError: Your model needs to output single-dimensional numpyarrays, not arrays of (5000, 1) dimensions `

Is there any way to make Keras output single dimensional arrays? I’ve looked around for a while and no luck 😦

Thanks in advance!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11

github_iconTop GitHub Comments

5reactions
EoinKennycommented, May 29, 2018

Sure thing!

You might have to adjust one or two things though, I haven’t tested this pipeline specifically.

model = load_model('keras_model.h5')

# Previously loaded pandas df
qc = df.as_matrix()[1]
qc_reshape = qc.reshape(1,-1)

def predict(qc):
    global model
    qc = model.predict(qc)
    return qc.reshape(qc.shape[0])

import lime
import lime.lime_tabular
import pandas as pd

explainer = lime.lime_tabular.LimeTabularExplainer(df.as_matrix(), 
                                                   feature_names=df.columns, 
                                                   class_names=['Price'], 
                                                   verbose = True,
                                                   mode='regression')

exp = explainer.explain_instance(qc, predict, num_features=len(df.columns))
0reactions
hanzigscommented, Aug 22, 2019

Hi @EoinKenny, I am linking to Lime issue https://github.com/marcotcr/lime/issues/376 I tried your solution

Keras_model.predict_proba(testData_for_model)
Out[73]: array([[0.6559619]], dtype=float32)

def predictKeras(testData_for_model):
    prediction_Class_1 = Keras_model.predict_proba(testData_for_model) 
    x = numpy.zeros((prediction_Class_1.shape[0], 1))
    probability = (x + 1) - prediction_Class_1
    final = numpy.append(probability,prediction_Class_1, axis=1)
    return final

The output of final is

final
Out[71]: array([[0.3440381‬, 0.6559619]])

Then I call

keras_explainer = lime.lime_tabular.LimeTabularExplainer(input_x, 
                                                 mode='classification',
                                                 feature_names=feature_names,
                                                 kernel_width=5,
                                                 random_state=42,
                                                 discretize_continuous=False) 
test_for_explainer = testData_for_model.reshape(testData_for_model.shape[1],)
exp = keras_explainer.explain_instance(test_for_explainer, predictKeras, num_features = 10)

Its Working Good

One question is, keras returns probability for Class 1, how should I mention class names in explainer, as exp.class_names return 0

exp.class_names
Out[85]: ['0']


Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Lime on a binary classification neural network
I would like to use Lime to interpret a neural network model. For the sake of this question, I made a simple Dense...
Read more >
Interpreting an LSTM through LIME - Towards Data Science
We saw how to interpret Keras models using LIME through a custom predict_proba function. However, note that LIME isn't without its flaws and...
Read more >
Deep Learning With Keras & LIME in R - AWS
I am SUPER EXCITED about two recent packages available in R for Deep Learning that everyone is preaching about: keras for Neural Network(NN)...
Read more >
How to Interpret Keras Models with LIME ... - YouTube
This is the missing tutorial on how to interpret Keras word embeddings with LIME. If our data is already an array of embeddings...
Read more >
LIME: Interpret Predictions Of Keras Text Classification Networks
LIME (Local Interpretable Model-Agnostic Explanations) is an algorithm that helps us solve this problem. It can help us understand the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found