shap / shap

A game theoretic approach to explain the output of any machine learning model.
https://shap.readthedocs.io
MIT License
22.59k stars 3.26k forks source link

error running mnist deepexplainer #1129

Open MMostavi opened 4 years ago

MMostavi commented 4 years ago

Hi Scott,

Thanks for the amazing package. I am having trouble running the mnist example that you have given for deepexplainer.

https://github.com/slundberg/shap/blob/master/notebooks/deep_explainer/Front%20Page%20DeepExplainer%20MNIST%20Example.ipynb

Here is the error: ValueError: Dimension 1 in both shapes must be equal, but are 10 and 1. Shapes are [?,10] and [?,1]. for 'gradients/dense_2_1/Softmax_grad/gradients/gradients/dense_2_1/Softmax_grad/truediv_grad/Select_1' (op: 'Select') with input shapes: [?,1], [?,10], [?,10].

I tried to debug the code and ended up to deep-tf file at line 408 where we have for non_reg_ops in ops_not_in_registry: del reg[non_reg_ops] I am using Tesnoforflow-gpu 1.14.0 and Keras version of 2.3.1. Also degraded the version of Keras but again it didn't work out!

Thanks in advance for the help Milad

raed19 commented 4 years ago

I have same issue with my own custom model

hs271095 commented 4 years ago

Any Solution to this issue?

TheEdoardo93 commented 4 years ago

I've got the same problem when using a custom tf.keras model (with Conv1D layers if it can be a useful information to know) and by using SHAP v. 0.35.0. I'm using TensorFlow v. 1.15.2

Have you ever tried SHAP v. 0.31.0? With this version, I'm able to plot feature importances with SHAP.

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in get_attr(self, name)
   2379       with c_api_util.tf_buffer() as buf:
-> 2380         c_api.TF_OperationGetAttrValueProto(self._c_op, name, buf)
   2381         data = c_api.TF_GetBuffer(buf)

InvalidArgumentError: Operation 'dense_1_2/Softmax' has no attr named '_XlaCompile'.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in _MaybeCompile(scope, op, func, grad_fn)
    344     try:
--> 345       xla_compile = op.get_attr("_XlaCompile")
    346       xla_separate_compiled_gradients = op.get_attr(

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in get_attr(self, name)
   2383       # Convert to ValueError for backwards compatibility.
-> 2384       raise ValueError(str(e))
   2385     x = attr_value_pb2.AttrValue()

ValueError: Operation 'dense_1_2/Softmax' has no attr named '_XlaCompile'.

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in get_attr(self, name)
   2379       with c_api_util.tf_buffer() as buf:
-> 2380         c_api.TF_OperationGetAttrValueProto(self._c_op, name, buf)
   2381         data = c_api.TF_GetBuffer(buf)

InvalidArgumentError: Operation 'gradients_4/dense_1_2/Softmax_grad/truediv' has no attr named '_XlaCompile'.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in _MaybeCompile(scope, op, func, grad_fn)
    344     try:
--> 345       xla_compile = op.get_attr("_XlaCompile")
    346       xla_separate_compiled_gradients = op.get_attr(

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in get_attr(self, name)
   2383       # Convert to ValueError for backwards compatibility.
-> 2384       raise ValueError(str(e))
   2385     x = attr_value_pb2.AttrValue()

ValueError: Operation 'gradients_4/dense_1_2/Softmax_grad/truediv' has no attr named '_XlaCompile'.

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1606   try:
-> 1607     c_op = c_api.TF_FinishOperation(op_desc)
   1608   except errors.InvalidArgumentError as e:

InvalidArgumentError: Dimension 1 in both shapes must be equal, but are 4 and 1. Shapes are [?,4] and [?,1]. for 'gradients_4/dense_1_2/Softmax_grad/gradients/gradients_4/dense_1_2/Softmax_grad/truediv_grad/Select_1' (op: 'Select') with input shapes: [?,1], [?,4], [?,4].

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-121-7117a71875fc> in <module>()
----> 1 shap_values = e.shap_values(test_set)

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/__init__.py in shap_values(self, X, ranked_outputs, output_rank_order, check_additivity)
    117         were chosen as "top".
    118         """
--> 119         return self.explainer.shap_values(X, ranked_outputs, output_rank_order, check_additivity=check_additivity)

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in shap_values(self, X, ranked_outputs, output_rank_order, check_additivity)
    302                 # run attribution computation graph
    303                 feature_ind = model_output_ranks[j,i]
--> 304                 sample_phis = self.run(self.phi_symbolic(feature_ind), self.model_inputs, joint_input)
    305 
    306                 # assign the attributions to the right part of the output arrays

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in phi_symbolic(self, i)
    228                     return tf.gradients(out, self.model_inputs)
    229 
--> 230                 self.phi_symbolics[i] = self.execute_with_overridden_gradients(anon)
    231             else:
    232                 @tf.function

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in execute_with_overridden_gradients(self, f)
    395         # define the computation graph for the attribution values using a custom gradient-like computation
    396         try:
--> 397             out = f()
    398         finally:
    399             # reinstate the backpropagatable check

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in anon()
    226                 def anon():
    227                     out = self.model_output[:,i] if self.multi_output else self.model_output
--> 228                     return tf.gradients(out, self.model_inputs)
    229 
    230                 self.phi_symbolics[i] = self.execute_with_overridden_gradients(anon)

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_impl.py in gradients(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients)
    156         ys, xs, grad_ys, name, colocate_gradients_with_ops,
    157         gate_gradients, aggregation_method, stop_gradients,
--> 158         unconnected_gradients)
    159   # pylint: enable=protected-access
    160 

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients, src_graph)
    677                 # functions.
    678                 in_grads = _MaybeCompile(grad_scope, op, func_call,
--> 679                                          lambda: grad_fn(op, *out_grads))
    680               else:
    681                 # For function call ops, we add a 'SymbolicGradient'

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in _MaybeCompile(scope, op, func, grad_fn)
    348       xla_scope = op.get_attr("_XlaScope").decode()
    349     except ValueError:
--> 350       return grad_fn()  # Exit early
    351 
    352   if not xla_compile:

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in <lambda>()
    677                 # functions.
    678                 in_grads = _MaybeCompile(grad_scope, op, func_call,
--> 679                                          lambda: grad_fn(op, *out_grads))
    680               else:
    681                 # For function call ops, we add a 'SymbolicGradient'

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in custom_grad(self, op, *grads)
    365         """
    366         type_name = op.type[5:] if op.type.startswith("shap_") else op.type
--> 367         out = op_handlers[type_name](self, op, *grads) # we cut off the shap_ prefex before the lookup
    368         return out
    369 

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in softmax(explainer, op, *grads)
    480                 explainer.between_tensors[t.name] = False
    481 
--> 482     out = tf.gradients(div, in0_centered, grad_ys=grads[0])[0]
    483 
    484     # remove the names we just added

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_impl.py in gradients(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients)
    156         ys, xs, grad_ys, name, colocate_gradients_with_ops,
    157         gate_gradients, aggregation_method, stop_gradients,
--> 158         unconnected_gradients)
    159   # pylint: enable=protected-access
    160 

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients, src_graph)
    677                 # functions.
    678                 in_grads = _MaybeCompile(grad_scope, op, func_call,
--> 679                                          lambda: grad_fn(op, *out_grads))
    680               else:
    681                 # For function call ops, we add a 'SymbolicGradient'

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in _MaybeCompile(scope, op, func, grad_fn)
    348       xla_scope = op.get_attr("_XlaScope").decode()
    349     except ValueError:
--> 350       return grad_fn()  # Exit early
    351 
    352   if not xla_compile:

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in <lambda>()
    677                 # functions.
    678                 in_grads = _MaybeCompile(grad_scope, op, func_call,
--> 679                                          lambda: grad_fn(op, *out_grads))
    680               else:
    681                 # For function call ops, we add a 'SymbolicGradient'

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in custom_grad(self, op, *grads)
    365         """
    366         type_name = op.type[5:] if op.type.startswith("shap_") else op.type
--> 367         out = op_handlers[type_name](self, op, *grads) # we cut off the shap_ prefex before the lookup
    368         return out
    369 

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in handler(explainer, op, *grads)
    555             return linearity_1d_handler(input_ind1, explainer, op, *grads)
    556         elif var[input_ind0] and var[input_ind1]:
--> 557             return nonlinearity_2d_handler(input_ind0, input_ind1, op_func, explainer, op, *grads)
    558         else:
    559             return [None for _ in op.inputs] # no inputs vary, we must be hidden by a switch function

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in nonlinearity_2d_handler(input_ind0, input_ind1, op_func, explainer, op, *grads)
    626     # Avoid divide by zero nans
    627     out0 = tf.where(tf.abs(tf.tile(delta_in0, dup0)) < 1e-7, tf.zeros_like(out0), out0)
--> 628     out1 = tf.where(tf.abs(tf.tile(delta_in1, dup0)) < 1e-7, tf.zeros_like(out1), out1)
    629 
    630     # see if due to broadcasting our gradient shapes don't match our input shapes

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/util/deprecation.py in new_func(*args, **kwargs)
    322               'in a future version' if date is None else ('after %s' % date),
    323               instructions)
--> 324       return func(*args, **kwargs)
    325     return tf_decorator.make_decorator(
    326         func, new_func, 'deprecated',

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/util/dispatch.py in wrapper(*args, **kwargs)
    178     """Call target, and fall back on dispatchers if there is a TypeError."""
    179     try:
--> 180       return target(*args, **kwargs)
    181     except (TypeError, ValueError):
    182       # Note: convert_to_eager_tensor currently raises a ValueError, not a

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/array_ops.py in where(condition, x, y, name)
   3757       return gen_array_ops.where(condition=condition, name=name)
   3758   elif x is not None and y is not None:
-> 3759     return gen_math_ops.select(condition=condition, x=x, y=y, name=name)
   3760   else:
   3761     raise ValueError("x and y must both be non-None or both be None.")

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gen_math_ops.py in select(condition, x, y, name)
   9437   # Add nodes to the TensorFlow graph.
   9438   _, _, _op = _op_def_lib._apply_op_helper(
-> 9439         "Select", condition=condition, t=x, e=y, name=name)
   9440   _result = _op.outputs[:]
   9441   _inputs_flat = _op.inputs

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
    792         op = g.create_op(op_type_name, inputs, dtypes=None, name=scope,
    793                          input_types=input_types, attrs=attr_protos,
--> 794                          op_def=op_def)
    795 
    796       # Conditionally invoke tfdbg v2's op callback(s).

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/util/deprecation.py in new_func(*args, **kwargs)
    505                 'in a future version' if date is None else ('after %s' % date),
    506                 instructions)
--> 507       return func(*args, **kwargs)
    508 
    509     doc = _add_deprecated_arg_notice_to_docstring(

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in create_op(***failed resolving arguments***)
   3355         raise TypeError("Input #%d is not a tensor: %s" % (idx, a))
   3356     return self._create_op_internal(op_type, inputs, dtypes, input_types, name,
-> 3357                                     attrs, op_def, compute_device)
   3358 
   3359   def _create_op_internal(

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
   3424           input_types=input_types,
   3425           original_op=self._default_original_op,
-> 3426           op_def=op_def)
   3427       self._create_op_helper(ret, compute_device=compute_device)
   3428     return ret

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
   1768           op_def, inputs, node_def.attr)
   1769       self._c_op = _create_c_op(self._graph, node_def, grouped_inputs,
-> 1770                                 control_input_ops)
   1771     # pylint: enable=protected-access
   1772 

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1608   except errors.InvalidArgumentError as e:
   1609     # Convert to ValueError for backwards compatibility.
-> 1610     raise ValueError(str(e))
   1611 
   1612   return c_op

ValueError: Dimension 1 in both shapes must be equal, but are 4 and 1. Shapes are [?,4] and [?,1]. for 'gradients_4/dense_1_2/Softmax_grad/gradients/gradients_4/dense_1_2/Softmax_grad/truediv_grad/Select_1' (op: 'Select') with input shapes: [?,1], [?,4], [?,4].
TheEdoardo93 commented 4 years ago

In my case, I've resolved my bug with a reshape method offered by NumPy. The problem was a mismatch input shape between the CONV-1D model (which expects 3D input) and the input (it was 2D).

I hope that this comment could help someone!

I've got the same problem when using a custom tf.keras model (with Conv1D layers if it can be a useful information to know) and by using SHAP v. 0.35.0. I'm using TensorFlow v. 1.15.2

Have you ever tried SHAP v. 0.31.0? With this version, I'm able to plot feature importances with SHAP.

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in get_attr(self, name)
   2379       with c_api_util.tf_buffer() as buf:
-> 2380         c_api.TF_OperationGetAttrValueProto(self._c_op, name, buf)
   2381         data = c_api.TF_GetBuffer(buf)

InvalidArgumentError: Operation 'dense_1_2/Softmax' has no attr named '_XlaCompile'.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in _MaybeCompile(scope, op, func, grad_fn)
    344     try:
--> 345       xla_compile = op.get_attr("_XlaCompile")
    346       xla_separate_compiled_gradients = op.get_attr(

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in get_attr(self, name)
   2383       # Convert to ValueError for backwards compatibility.
-> 2384       raise ValueError(str(e))
   2385     x = attr_value_pb2.AttrValue()

ValueError: Operation 'dense_1_2/Softmax' has no attr named '_XlaCompile'.

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in get_attr(self, name)
   2379       with c_api_util.tf_buffer() as buf:
-> 2380         c_api.TF_OperationGetAttrValueProto(self._c_op, name, buf)
   2381         data = c_api.TF_GetBuffer(buf)

InvalidArgumentError: Operation 'gradients_4/dense_1_2/Softmax_grad/truediv' has no attr named '_XlaCompile'.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in _MaybeCompile(scope, op, func, grad_fn)
    344     try:
--> 345       xla_compile = op.get_attr("_XlaCompile")
    346       xla_separate_compiled_gradients = op.get_attr(

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in get_attr(self, name)
   2383       # Convert to ValueError for backwards compatibility.
-> 2384       raise ValueError(str(e))
   2385     x = attr_value_pb2.AttrValue()

ValueError: Operation 'gradients_4/dense_1_2/Softmax_grad/truediv' has no attr named '_XlaCompile'.

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1606   try:
-> 1607     c_op = c_api.TF_FinishOperation(op_desc)
   1608   except errors.InvalidArgumentError as e:

InvalidArgumentError: Dimension 1 in both shapes must be equal, but are 4 and 1. Shapes are [?,4] and [?,1]. for 'gradients_4/dense_1_2/Softmax_grad/gradients/gradients_4/dense_1_2/Softmax_grad/truediv_grad/Select_1' (op: 'Select') with input shapes: [?,1], [?,4], [?,4].

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-121-7117a71875fc> in <module>()
----> 1 shap_values = e.shap_values(test_set)

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/__init__.py in shap_values(self, X, ranked_outputs, output_rank_order, check_additivity)
    117         were chosen as "top".
    118         """
--> 119         return self.explainer.shap_values(X, ranked_outputs, output_rank_order, check_additivity=check_additivity)

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in shap_values(self, X, ranked_outputs, output_rank_order, check_additivity)
    302                 # run attribution computation graph
    303                 feature_ind = model_output_ranks[j,i]
--> 304                 sample_phis = self.run(self.phi_symbolic(feature_ind), self.model_inputs, joint_input)
    305 
    306                 # assign the attributions to the right part of the output arrays

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in phi_symbolic(self, i)
    228                     return tf.gradients(out, self.model_inputs)
    229 
--> 230                 self.phi_symbolics[i] = self.execute_with_overridden_gradients(anon)
    231             else:
    232                 @tf.function

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in execute_with_overridden_gradients(self, f)
    395         # define the computation graph for the attribution values using a custom gradient-like computation
    396         try:
--> 397             out = f()
    398         finally:
    399             # reinstate the backpropagatable check

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in anon()
    226                 def anon():
    227                     out = self.model_output[:,i] if self.multi_output else self.model_output
--> 228                     return tf.gradients(out, self.model_inputs)
    229 
    230                 self.phi_symbolics[i] = self.execute_with_overridden_gradients(anon)

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_impl.py in gradients(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients)
    156         ys, xs, grad_ys, name, colocate_gradients_with_ops,
    157         gate_gradients, aggregation_method, stop_gradients,
--> 158         unconnected_gradients)
    159   # pylint: enable=protected-access
    160 

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients, src_graph)
    677                 # functions.
    678                 in_grads = _MaybeCompile(grad_scope, op, func_call,
--> 679                                          lambda: grad_fn(op, *out_grads))
    680               else:
    681                 # For function call ops, we add a 'SymbolicGradient'

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in _MaybeCompile(scope, op, func, grad_fn)
    348       xla_scope = op.get_attr("_XlaScope").decode()
    349     except ValueError:
--> 350       return grad_fn()  # Exit early
    351 
    352   if not xla_compile:

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in <lambda>()
    677                 # functions.
    678                 in_grads = _MaybeCompile(grad_scope, op, func_call,
--> 679                                          lambda: grad_fn(op, *out_grads))
    680               else:
    681                 # For function call ops, we add a 'SymbolicGradient'

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in custom_grad(self, op, *grads)
    365         """
    366         type_name = op.type[5:] if op.type.startswith("shap_") else op.type
--> 367         out = op_handlers[type_name](self, op, *grads) # we cut off the shap_ prefex before the lookup
    368         return out
    369 

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in softmax(explainer, op, *grads)
    480                 explainer.between_tensors[t.name] = False
    481 
--> 482     out = tf.gradients(div, in0_centered, grad_ys=grads[0])[0]
    483 
    484     # remove the names we just added

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_impl.py in gradients(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients)
    156         ys, xs, grad_ys, name, colocate_gradients_with_ops,
    157         gate_gradients, aggregation_method, stop_gradients,
--> 158         unconnected_gradients)
    159   # pylint: enable=protected-access
    160 

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients, src_graph)
    677                 # functions.
    678                 in_grads = _MaybeCompile(grad_scope, op, func_call,
--> 679                                          lambda: grad_fn(op, *out_grads))
    680               else:
    681                 # For function call ops, we add a 'SymbolicGradient'

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in _MaybeCompile(scope, op, func, grad_fn)
    348       xla_scope = op.get_attr("_XlaScope").decode()
    349     except ValueError:
--> 350       return grad_fn()  # Exit early
    351 
    352   if not xla_compile:

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in <lambda>()
    677                 # functions.
    678                 in_grads = _MaybeCompile(grad_scope, op, func_call,
--> 679                                          lambda: grad_fn(op, *out_grads))
    680               else:
    681                 # For function call ops, we add a 'SymbolicGradient'

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in custom_grad(self, op, *grads)
    365         """
    366         type_name = op.type[5:] if op.type.startswith("shap_") else op.type
--> 367         out = op_handlers[type_name](self, op, *grads) # we cut off the shap_ prefex before the lookup
    368         return out
    369 

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in handler(explainer, op, *grads)
    555             return linearity_1d_handler(input_ind1, explainer, op, *grads)
    556         elif var[input_ind0] and var[input_ind1]:
--> 557             return nonlinearity_2d_handler(input_ind0, input_ind1, op_func, explainer, op, *grads)
    558         else:
    559             return [None for _ in op.inputs] # no inputs vary, we must be hidden by a switch function

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in nonlinearity_2d_handler(input_ind0, input_ind1, op_func, explainer, op, *grads)
    626     # Avoid divide by zero nans
    627     out0 = tf.where(tf.abs(tf.tile(delta_in0, dup0)) < 1e-7, tf.zeros_like(out0), out0)
--> 628     out1 = tf.where(tf.abs(tf.tile(delta_in1, dup0)) < 1e-7, tf.zeros_like(out1), out1)
    629 
    630     # see if due to broadcasting our gradient shapes don't match our input shapes

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/util/deprecation.py in new_func(*args, **kwargs)
    322               'in a future version' if date is None else ('after %s' % date),
    323               instructions)
--> 324       return func(*args, **kwargs)
    325     return tf_decorator.make_decorator(
    326         func, new_func, 'deprecated',

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/util/dispatch.py in wrapper(*args, **kwargs)
    178     """Call target, and fall back on dispatchers if there is a TypeError."""
    179     try:
--> 180       return target(*args, **kwargs)
    181     except (TypeError, ValueError):
    182       # Note: convert_to_eager_tensor currently raises a ValueError, not a

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/array_ops.py in where(condition, x, y, name)
   3757       return gen_array_ops.where(condition=condition, name=name)
   3758   elif x is not None and y is not None:
-> 3759     return gen_math_ops.select(condition=condition, x=x, y=y, name=name)
   3760   else:
   3761     raise ValueError("x and y must both be non-None or both be None.")

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/ops/gen_math_ops.py in select(condition, x, y, name)
   9437   # Add nodes to the TensorFlow graph.
   9438   _, _, _op = _op_def_lib._apply_op_helper(
-> 9439         "Select", condition=condition, t=x, e=y, name=name)
   9440   _result = _op.outputs[:]
   9441   _inputs_flat = _op.inputs

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
    792         op = g.create_op(op_type_name, inputs, dtypes=None, name=scope,
    793                          input_types=input_types, attrs=attr_protos,
--> 794                          op_def=op_def)
    795 
    796       # Conditionally invoke tfdbg v2's op callback(s).

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/util/deprecation.py in new_func(*args, **kwargs)
    505                 'in a future version' if date is None else ('after %s' % date),
    506                 instructions)
--> 507       return func(*args, **kwargs)
    508 
    509     doc = _add_deprecated_arg_notice_to_docstring(

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in create_op(***failed resolving arguments***)
   3355         raise TypeError("Input #%d is not a tensor: %s" % (idx, a))
   3356     return self._create_op_internal(op_type, inputs, dtypes, input_types, name,
-> 3357                                     attrs, op_def, compute_device)
   3358 
   3359   def _create_op_internal(

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
   3424           input_types=input_types,
   3425           original_op=self._default_original_op,
-> 3426           op_def=op_def)
   3427       self._create_op_helper(ret, compute_device=compute_device)
   3428     return ret

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
   1768           op_def, inputs, node_def.attr)
   1769       self._c_op = _create_c_op(self._graph, node_def, grouped_inputs,
-> 1770                                 control_input_ops)
   1771     # pylint: enable=protected-access
   1772 

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1608   except errors.InvalidArgumentError as e:
   1609     # Convert to ValueError for backwards compatibility.
-> 1610     raise ValueError(str(e))
   1611 
   1612   return c_op

ValueError: Dimension 1 in both shapes must be equal, but are 4 and 1. Shapes are [?,4] and [?,1]. for 'gradients_4/dense_1_2/Softmax_grad/gradients/gradients_4/dense_1_2/Softmax_grad/truediv_grad/Select_1' (op: 'Select') with input shapes: [?,1], [?,4], [?,4].
krkaufma commented 4 years ago

@TheEdoardo93 Can you provide more information about the workaround you found? My numpy arrays are of shape (498, 246, 299, 1) and (6, 246, 299, 1). For the background examples and new examples respectively.

TheEdoardo93 commented 4 years ago

@TheEdoardo93 Can you provide more information about the workaround you found? My numpy arrays are of shape (498, 246, 299, 1) and (6, 246, 299, 1). For the background examples and new examples respectively.

In my case, I had a TensorFlow.Keras model CONV-like (which expects 3D input tensor) but I passed a 2D input tensor. In order to solve this problem, I've used a method offered by NumPy called reshape which reshape my input as expected by the model.

Hovewer, have you ever tried to use a same shape for the background examples and examples in the test set, i.e. in terms of number of examples?

bowtiejicode commented 4 years ago

@TheEdoardo93 are u able to get the demo MNIST notebook working? I am getting the same error as you.

Dimension 1 in both shapes must be equal, but are 10 and 1. Shapes are [?,10] and [?,1]. for 'gradients_1/dense_4/Softmax_grad/gradients/gradients_1/dense_4/Softmax_grad/truediv_grad/Select_1' (op: 'Select') with input shapes: [?,1], [?,10], [?,10].
mturk24 commented 3 years ago

@TheEdoardo93 Hi, can you please provide more details or show a minimally working code example on your NumPy workaround? How did you reformat the way you passed input to DeepExplainer and what did your call to explainer.shap_values look like?

Amarchuk commented 3 years ago

Confirm that downgrade to 0.31 helps in my case (and on MNIST example too, keras==2.3.1).

lxy417 commented 3 years ago

Confirm that downgrade to 0.31 helps in my case (and on MNIST example too, keras==2.3.1).

这个可以