Prevent none value in gradients when some of the inputs have not impact to the target #987
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is to fix the gradients which could have none values. There are scenarios that some of the inputs have not impact to the target value, IG generates None type grads, something like [ [gradient 1], [gradient 2], None, None], and then when calculating the sum in the _calculate_sum_int method, "grads = tf.concat(batches[j], 0)" line throws errors like "None type can't convert to tensor".
Here is the full stack of errors:
An error was encountered:
Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.
Traceback (most recent call last):
File "/tmp/8638054080360197755", line 229, in execute
exec(code, global_dict)
File "", line 8, in
baselines=None)
File "/usr/local/lib/python3.7/dist-packages/alibi/explainers/integrated_gradients.py", line 828, in explain
attribute_to_layer_inputs)
File "/usr/local/lib/python3.7/dist-packages/alibi/explainers/integrated_gradients.py", line 1069, in _compute_attributions_list_input
step_sizes, j)
File "/usr/local/lib/python3.7/dist-packages/alibi/explainers/integrated_gradients.py", line 614, in _calculate_sum_int
grads = tf.concat(batches[j], 0)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py", line 206, in wrapper
return target(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/array_ops.py", line 1769, in concat
return gen_array_ops.concat_v2(values=values, axis=axis, name=name)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1218, in concat_v2
values, axis, name=name, ctx=_ctx)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1248, in concat_v2_eager_fallback
_attr_T, values = _execute.args_to_matching_eager(list(values), ctx, [])
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py", line 274, in args_to_matching_eager
t, dtype, preferred_dtype=default_dtype, ctx=ctx)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/profiler/trace.py", line 163, in wrapped
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py", line 1566, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py", line 346, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py", line 272, in constant
allow_broadcast=True)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py", line 283, in _constant_impl
return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py", line 308, in _constant_eager_impl
t = convert_to_eager_tensor(value, ctx, dtype)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py", line 106, in convert_to_eager_tensor
return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.