Shap: Tensorflow: Session is empty

Created on 18 Aug 2019  路  4Comments  路  Source: slundberg/shap

Hey there,

I'm rather new to python resp. tensorflow 2.0 with keras.

I have a model which is trained and I want to interpret it with SHAP now.
But using the DeepExplainer doesn't work. I get the following error

RuntimeError                              Traceback (most recent call last)

<ipython-input-19-38e59444e33c> in <module>
      2 
      3 data = x_train[:200]
----> 4 explainer = shap.DeepExplainer(model, data)
      5 

~/python/lib/python3.7/site-packages/shap/explainers/deep/__init__.py in __init__(self, model, data, session, learning_phase_flags)
     78 
     79         if framework == 'tensorflow':
---> 80             self.explainer = TFDeepExplainer(model, data, session, learning_phase_flags)
     81         elif framework == 'pytorch':
     82             self.explainer = PyTorchDeepExplainer(model, data)

~/python/lib/python3.7/site-packages/shap/explainers/deep/deep_tf.py in __init__(self, model, data, session, learning_phase_flags)
    139             if self.data[0].shape[0] > 5000:
    140                 warnings.warn("You have provided over 5k background samples! For better performance consider using smaller random sample.")
--> 141             self.expected_value = self.run(self.model_output, self.model_inputs, self.data).mean(0)
    142 
    143         # find all the operations in the graph between our inputs and outputs

~/python/lib/python3.7/site-packages/shap/explainers/deep/deep_tf.py in run(self, out, model_inputs, X)
    282         for t in self.learning_phase_flags:
    283             feed_dict[t] = False
--> 284         return self.session.run(out, feed_dict)
    285 
    286     def custom_grad(self, op, *grads):

~/python/lib/python3.7/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    948     try:
    949       result = self._run(None, fetches, feed_dict, options_ptr,
--> 950                          run_metadata_ptr)
    951       if run_metadata:
    952         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~/python/lib/python3.7/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1096       raise RuntimeError('Attempted to use a closed Session.')
   1097     if self.graph.version == 0:
-> 1098       raise RuntimeError('The Session graph is empty.  Add operations to the '
   1099                          'graph before calling run().')
   1100 

RuntimeError: The Session graph is empty.  Add operations to the graph before calling run().

This is what my Model looks like:

model = keras.models.Sequential()


# Input size of VOCABULAR_SIZE with 50-neuron-layer
model.add(keras.layers.Dense(50, input_shape=(VOCABULAR_SIZE,), activation='relu'))

# Hidden layer
model.add(keras.layers.Dense(25, activation='relu'))

# Hidden layer in size of tags
# Sigmoid returns each output between 0 and 1

model.add(keras.layers.Dense(tags_count, activation='sigmoid'))
model.summary()

Am I missing something? Could you guys help me out?

Most helpful comment

@alexander-fischer Session/Graph execution still exists, it's just that TF2 changed to eager execution mode as the default, which doesn't use them. Conscious design choice, so not something that's likely to change.

A couple of possibilities would be A) update tutorials to reference the necessary change for tf2 (since it would need to be in the user's code, typically) and/or B) update SHAP to detect eager execution mode, and fail with instructions on how to run in non-eager mode.

@TdoubleG - basically, the following change was sufficient to get SHAP to run on tf2.0 for me:

 import tensorflow as tf

 tf.compat.v1.disable_eager_execution()

(Added before any other tensorflow calls, as the switch to non-eager mode must happen first)

All 4 comments

I think it has something to do with Tensorflow 2. As they dropped session graphs in Tensorflow, the SHAP library still depends on it. I think soon after the release they will fix it.

@alexander-fischer Session/Graph execution still exists, it's just that TF2 changed to eager execution mode as the default, which doesn't use them. Conscious design choice, so not something that's likely to change.

A couple of possibilities would be A) update tutorials to reference the necessary change for tf2 (since it would need to be in the user's code, typically) and/or B) update SHAP to detect eager execution mode, and fail with instructions on how to run in non-eager mode.

@TdoubleG - basically, the following change was sufficient to get SHAP to run on tf2.0 for me:

 import tensorflow as tf

 tf.compat.v1.disable_eager_execution()

(Added before any other tensorflow calls, as the switch to non-eager mode must happen first)

Thanks @jamlong ! As for SHAP support... sitting down to deal with TF 2.0 changes gracefully is on my list of to-dos next month.

@jamlong you saved my.. you know what I mean :D

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nickkimer picture nickkimer  路  4Comments

cbeauhilton picture cbeauhilton  路  3Comments

1vecera picture 1vecera  路  3Comments

Nithanaroy picture Nithanaroy  路  4Comments

artemmavrin picture artemmavrin  路  4Comments