It turns out that the tensorflow model requires a custom predict_function to create an Explainer object for it. This is because of two reasons:
It would be great to have dalex handling this natively, like xgboost models. To that moment I believe creating a short instruction is needed.
# tensorflow==2.0.0
import dalex as dx
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from sklearn.preprocessing import StandardScaler
data = dx.datasets.load_fifa()
X = data.drop(columns=['nationality', 'value_eur'])
y = data['value_eur']
X[X.columns] = StandardScaler().fit_transform(X[X.columns])
model = keras.models.Sequential([
layers.Dense(128, activation='relu', input_shape=[X.values.shape[1]]),
layers.Dense(64, activation='relu'),
layers.Dense(1)
])
optimizer = tf.keras.optimizers.RMSprop(0.001)
model.compile(loss='mse',
optimizer=optimizer,
metrics=['mse'])
def predict_function_without_reshaping(model, data):
X = data.values
return model.predict(X)
def predict_function_with_reshaping(model, data):
X = data.values
return model.predict(X).reshape((data.shape[0],))
exp_default = dx.Explainer(model, X, y, verbose=True, model_type='regression')
exp_no_reshape = dx.Explainer(model, X, y, verbose=True, model_type='regression', predict_function=predict_function_without_reshaping)
print(exp_no_reshape.y_hat.shape)
print(exp_no_reshape.residuals.shape)
exp_reshape = dx.Explainer(model, X, y, verbose=True, model_type='regression', predict_function=predict_function_with_reshaping)
print(exp_reshape.y_hat.shape)
print(exp_reshape.residuals.shape)
It turns out that the tensorflow model requires a custom
predict_functionto create anExplainerobject for it. This is because of two reasons:DataFrameso an additional step of changingDataFrameinto numpy's array is requiredndarrayof shape (input data n_rows, 1) which, without reshaping to (input data n_rows, ) shape, leads to incorrect results described in Python: lack of validation of the predict_function's output shape may cause errors #325It would be great to have dalex handling this natively, like xgboost models. To that moment I believe creating a short instruction is needed.