This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from model.data import mnist_train_test_sets | |
from model.network import LeNet5 | |
from saliency.vanilla_gradient import save_vanilla_gradient | |
# Get MNIST dataset, preprocessed | |
train_images, train_labels, test_images, test_labels = mnist_train_test_sets() | |
# Load net with 98% acc weights | |
net = LeNet5(weights_path="15epoch_weights.pkl") | |
# Generate saliency maps for the first 10 images |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a saliency map for each data point | |
for i, image in enumerate(data): | |
# Run a forward pass with an image | |
output = image | |
for l in range(len(network.layers)): | |
output = network.layers[l].forward(output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a saliency map for each data point | |
for i, image in enumerate(data): | |
# Forward pass on image | |
# Note: the activations 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] |
OlderNewer