Problem using LIME with Keras
See original GitHub issueI 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:
- Created 5 years ago
- Comments:11
Sure thing!
You might have to adjust one or two things though, I haven’t tested this pipeline specifically.
Hi @EoinKenny, I am linking to Lime issue https://github.com/marcotcr/lime/issues/376 I tried your solution
The output of final is
Then I call
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