SeldonIO / alibi

Algorithms for explaining machine learning models
https://docs.seldon.io/projects/alibi/en/stable/
Other
2.42k stars 252 forks source link

Tensorflow RuntimeError Eager Execution? #594

Open cdeterman opened 2 years ago

cdeterman commented 2 years ago

I have an xgboost model I am trying to run couterfactuals on using the CounterfactualProto explainer. Given the RuntimeError I see below, I tired to disable the eager execution using the method found here but the problem did not go away. Is this something that has been encountered before? Please let me know if more information is required?

Packages: alibi - 0.6.4 tensorflow - 2.7.1 xgboost - 1.2.1

RuntimeError                              Traceback (most recent call last)
Input In [9], in <module>
----> 1 sm.cf_setup_alibi(df1)

File /my/path/myfile.py:123, in Model.cf_setup_alibi(self, train_df)
    120     return self.mdl.predict_proba(x)
    121 # predict_fn = lambda x: mdl.predict_proba(x)
--> 123 self.exp = CounterfactualProto(predict_fn, shape, use_kdtree=use_kdtree,
    124                                theta=theta, max_iterations=max_iter,
    125                                feature_range=feature_range,
    126                                c_init=c_init, c_steps=c_steps)
    127 self.exp.fit(train_df.loc[:, self.vars_for_cfs + ['acc']])

File /my/path/alibi/lib/python3.8/site-packages/alibi/explainers/cfproto.py:496, in CounterfactualProto.__init__(self, predict, shape, kappa, beta, feature_range, gamma, ae_model, enc_model, theta, cat_vars, ohe, use_kdtree, learning_rate_init, max_iterations, c_init, c_steps, eps, clip, update_num_grad, write_dir, sess)
    493 self.global_step = tf.Variable(0.0, trainable=False, name='global_step')
    495 # define placeholders that will be assigned to relevant variables
--> 496 self.assign_orig = tf.placeholder(tf.float32, shape, name='assign_orig')
    497 self.assign_adv = tf.placeholder(tf.float32, shape, name='assign_adv')
    498 self.assign_adv_s = tf.placeholder(tf.float32, shape, name='assign_adv_s')

File /my/path/alibi/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py:3286, in placeholder(dtype, shape, name)
   3239 """Inserts a placeholder for a tensor that will be always fed.
   3240
   3241 **Important**: This tensor will produce an error if evaluated. Its value must
   (...)
   3283 @end_compatibility
   3284 """
   3285 if context.executing_eagerly():
-> 3286   raise RuntimeError("tf.placeholder() is not compatible with "
   3287                      "eager execution.")
   3289 return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)

RuntimeError: tf.placeholder() is not compatible with eager execution.
mauicv commented 2 years ago

Hey @cdeterman, Thanks for opening the issue. I think the above error relates to this.:

To summarize, eager mode needs to be disabled to run CF/CEM algorithms because we use TF1.x constructs in the code.

Although I think the above should be fixed by disabling eager execution as you do? Perhaps also try:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

I've tried/guessed at recreating your error with the same lib versions:

from sklearn.model_selection import train_test_split
from sklearn import datasets
import xgboost as xgb

iris = datasets.load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)

param = {'max_depth': 3, 'eta': 0.3, 'silent': 1, 'objective': 'multi:softprob', 'num_class': 3}
num_round = 20
bst = xgb.train(param, dtrain, num_round)

# import tensorflow as tf
# tf.compat.v1.disable_eager_execution()

from alibi.explainers import CounterfactualProto

def predict_fn(x):
    return bst.inplace_predict(x)

shape = (1,) + X_train.shape[1:]

proto_explainer = CounterfactualProto(predict_fn, shape, use_kdtree=True, theta=10., feature_range=(-.5, .5))

The above reproduces the issue and uncommenting the disable_eager_execution solves it for me:

import tensorflow as tf
tf.compat.v1.disable_eager_execution()

Can you give more information about your model and/or a minimal working example?