Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion alibi/explainers/integrated_gradients.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@ def _gradients_input(model: Union[tf.keras.models.Model],

grads = tape.gradient(preds, x)

# If certain inputs don't impact the target, the gradient is None, but we need to return a tensor
if isinstance(x, list):
for idx, grad in enumerate(grads):
if grad is None:
grads[idx] = tf.convert_to_tensor(np.zeros(x[idx].shape), dtype=x[idx].dtype)
return grads


Expand Down Expand Up @@ -497,7 +502,11 @@ def wrapper(*args, **kwargs):
grads = tape.gradient(preds, layer.inp)
else:
grads = tape.gradient(preds, layer.result)

# If certain inputs don't impact the target, the gradient is None, but we need to return a tensor
if isinstance(x, list):
for idx, grad in enumerate(grads):
if grad is None:
grads[idx] = tf.convert_to_tensor(np.zeros(x[idx].shape), dtype=x[idx].dtype)
delattr(layer, 'inp')
delattr(layer, 'result')
layer.call = orig_call
Expand Down