Skip to content

Python: lack of out-of-the-box support for tensorflow models #326

Description

@pfraczek

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions