-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGBP.py
94 lines (75 loc) · 3.13 KB
/
GBP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import torch
import torch.nn as nn
from torch.autograd import Function
import torch.nn.functional as F
# custom autograd function for doing guided backprop through LeakyReLU
class GuidedBackpropTanh(Function):
@staticmethod
def forward(ctx, input):
# saves input tensor into ctx
ctx.save_for_backward(input)
# returns output of activation
return torch.tanh(input)
@staticmethod
def backward(ctx, grad_output):
# retrieve input saved during forward pass
input, = ctx.saved_tensors
tanh_grad = 1 - torch.tanh(input) ** 2
grad_input = grad_output.clone()
grad_input *= tanh_grad
return grad_input
class GuidedBackpropLeakyReLU(Function):
@staticmethod
def forward(ctx, input, negative_slope=0.01):
ctx.save_for_backward(input)
# sets slope of LeakyReLU
ctx.negative_slope = negative_slope
return F.leaky_relu(input, negative_slope=negative_slope)
@staticmethod
def backward(ctx, grad_output):
input, = ctx.saved_tensors
negative_slope = ctx.negative_slope
# clone gradient so as to not modify original tensor
grad_input = grad_output.clone()
grad_input[input < 0] *= negative_slope
return grad_input
class GuidedBackpropModel:
def __init__(self, model):
self.model = model
self.model.eval()
# Replace activation functions with guided backprop versions
for name, module in self.model.named_modules():
if isinstance(module, nn.Tanh):
module.register_backward_hook(self.backward_hook_tanh)
elif isinstance(module, nn.LeakyReLU):
module.register_backward_hook(self.backward_hook_leaky_relu)
def backward_hook_tanh(self, module, grad_input, grad_output):
return (GuidedBackpropTanh.apply(grad_input[0]),)
def backward_hook_leaky_relu(self, module, grad_input, grad_output):
return (GuidedBackpropLeakyReLU.apply(grad_input[0]),)
def forward(self, device, input):
#print('input in forward: ', input.shape)
return self.model(device, input)
def __call__(self, device, input, index=None):
#print('input in __call__: ', input.shape)
input.requires_grad = True
output = self.forward(device, input)
#print('output shape: ', output.shape)
if index is None:
# Find the index of the maximum value in the flattened tensor
index = torch.argmax(output)
index = index.item()
# Zero out all previous gradients
self.model.zero_grad()
#print('index: ', index)
# Backpropagate
grad_output = torch.zeros_like(output.view(-1)) # Flatten grad_output
#print('grad output shape: ', grad_output.shape)
# Set the corresponding index to 1 in the unflattened tensor
grad_output[index] = 1
grad_output = grad_output.view(output.shape)
#print('grad output shape post reshaping: ', grad_output.shape)
output.backward(gradient=grad_output)
gbp = input.grad
#print('gbp type: ', type(gbp))
return gbp