Skip to content

Instantly share code, notes, and snippets.

@andrewschreiber
Created August 16, 2019 06:29
Show Gist options
  • Save andrewschreiber/fea5d3b35b07f92dff58b76fef2ac67b to your computer and use it in GitHub Desktop.
Save andrewschreiber/fea5d3b35b07f92dff58b76fef2ac67b to your computer and use it in GitHub Desktop.
def save_vanilla_gradient(network, data, labels), see https://github.com/andrewschreiber/numpy-saliency
# Create a saliency map for each data point
for i, image in enumerate(data):
# Forward pass on image
# Note: the activations from this are saved on each layer
output = image
for l in range(len(network.layers)):
output = network.layers[l].forward(output)
# Backprop to get gradient
label_one_hot = labels[i]
dy = np.array(label_one_hot)
for l in range(len(network.layers)-1, -1, -1):
dout = network.layers[l].backward(dy)
dy = dout
raw_saliency_map = dout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment