Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use batches for SmoothGrad #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
22 changes: 13 additions & 9 deletions saliency/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,22 @@ def GetSmoothedMask(self,
magnitude: If true, computes the sum of squares of gradients instead of
just the sum. Defaults to true.
"""
stdev = stdev_spread * (np.max(x_value) - np.min(x_value))

stdev = stdev_spread * (np.max(x_value) - np.min(x_value))
total_gradients = np.zeros_like(x_value, dtype=np.float32)
for _ in range(nsamples):
noise = np.random.normal(0, stdev, x_value.shape)
x_plus_noise = x_value + noise
grad = self.GetMask(x_plus_noise, call_model_function, call_model_args,
shape = (nsamples,) + x_value.shape
noisy_samples = np.zeros(shape)
for i in range(nsamples):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could get rid of the loop completely if you used np.repeat(...) and generating the noise mask for the batched shape.

noise = np.random.normal(0, stdev, x_value.shape)
x_plus_noise = x_value + noise
noisy_samples[i] = x_plus_noise

grads = self.GetMask(noisy_samples, call_model_function, call_model_args,
**kwargs)
if magnitude:
total_gradients += (grad * grad)
else:
total_gradients += grad
if magnitude:
total_gradients = np.sum((grads * grads), axis = 0)
else:
total_gradients = np.sum(grads, axis = 0)

return total_gradients / nsamples

Expand Down
10 changes: 8 additions & 2 deletions saliency/core/gradients.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def GetMask(self, x_value, call_model_function, call_model_args=None):
call_model_args: The arguments that will be passed to the call model
function, for every call of the model.
"""
x_value_batched = np.expand_dims(x_value, axis=0)
if(len(x_value.shape) == 3):
x_value_batched = np.expand_dims(x_value, axis=0)
else:
x_value_batched = x_value
call_model_output = call_model_function(
x_value_batched,
call_model_args=call_model_args,
Expand All @@ -57,4 +60,7 @@ def GetMask(self, x_value, call_model_function, call_model_args=None):
x_value_batched.shape,
self.expected_keys)

return call_model_output[INPUT_OUTPUT_GRADIENTS][0]
if(x_value_batched.shape[0] == 1):
return call_model_output[INPUT_OUTPUT_GRADIENTS][0]
else:
return call_model_output[INPUT_OUTPUT_GRADIENTS]