Skip to content

Commit e1d7a4c

Browse files
committed
Adds some docs to tutorials and to README
1 parent 44187a0 commit e1d7a4c

6 files changed

+249
-193
lines changed

README.md

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,15 @@ class ToyModel(nn.Module):
120120
self.lin1 = nn.Linear(3, 3)
121121
self.relu = nn.ReLU()
122122
self.lin2 = nn.Linear(3, 2)
123-
self.sigmoid = nn.Sigmoid()
124123
125124
# initialize weights and biases
126-
self.lin1.weight = nn.Parameter(torch.arange(0.0, 9.0).view(3, 3))
125+
self.lin1.weight = nn.Parameter(torch.arange(-4.0, 5.0).view(3, 3))
127126
self.lin1.bias = nn.Parameter(torch.zeros(1,3))
128-
self.lin2.weight = nn.Parameter(torch.arange(0.0, 6.0).view(2, 3))
127+
self.lin2.weight = nn.Parameter(torch.arange(-3.0, 3.0).view(2, 3))
129128
self.lin2.bias = nn.Parameter(torch.ones(1,2))
130129
131130
def forward(self, input):
132-
return self.sigmoid(self.lin2(self.relu(self.lin1(input))))
131+
return self.lin2(self.relu(self.lin1(input)))
133132
```
134133

135134
Let's create an instance of our model and set it to eval mode.
@@ -176,9 +175,9 @@ print('IG Attributions: ', attributions, ' Convergence Delta: ', delta)
176175
```
177176
Output:
178177
```
179-
IG Attributions: tensor([[0.0628, 0.1314, 0.0747],
180-
[0.0930, 0.0120, 0.1639]])
181-
Convergence Delta: tensor([0., 0.])
178+
IG Attributions: tensor([[-0.5922, -1.5497, -1.0067],
179+
[ 0.0000, -0.2219, -5.1991]])
180+
Convergence Delta: tensor([2.3842e-07, -4.7684e-07])
182181
```
183182
The algorithm outputs an attribution score for each input element and a
184183
convergence delta. The lower the absolute value of the convergence delta the better
@@ -217,9 +216,9 @@ print('GradientShap Attributions: ', attributions, ' Convergence Delta: ', delta
217216
```
218217
Output
219218
```
220-
GradientShap Attributions: tensor([[ 0.0008, 0.0019, 0.0009],
221-
[ 0.1892, -0.0045, 0.2445]])
222-
Convergence Delta: tensor([-0.2681, -0.2633, -0.2607, -0.2655, -0.2689, -0.2689, 1.4493, -0.2688])
219+
GradientShap Attributions: tensor([[-0.1542, -1.6229, -1.5835],
220+
[-0.3916, -0.2836, -4.6851]])
221+
Convergence Delta: tensor([ 0.0000, -0.0005, -0.0029, -0.0084, -0.0087, -0.0405, 0.0000, -0.0084])
223222
224223
```
225224
Deltas are computed for each `n_samples * input.shape[0]` example. The user can,
@@ -243,8 +242,8 @@ print('DeepLift Attributions: ', attributions, ' Convergence Delta: ', delta)
243242
```
244243
Output
245244
```
246-
DeepLift Attributions: tensor([[0.0628, 0.1314, 0.0747],
247-
[0.0930, 0.0120, 0.1639]])
245+
DeepLift Attributions: tensor([[-0.5922, -1.5497, -1.0067],
246+
[ 0.0000, -0.2219, -5.1991])
248247
Convergence Delta: tensor([0., 0.])
249248
```
250249
DeepLift assigns similar attribution scores as Integrated Gradients to inputs,
@@ -269,12 +268,12 @@ print('DeepLiftSHAP Attributions: ', attributions, ' Convergence Delta: ', delta
269268
```
270269
Output
271270
```
272-
DeepLiftShap Attributions: tensor([0.0627, 0.1313, 0.0747],
273-
[0.0929, 0.0120, 0.1637], grad_fn=<MeanBackward1>)
274-
Convergence Delta: tensor([-2.9802e-08, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00,
275-
0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 2.9802e-08,
276-
0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00,
277-
0.0000e+00, 0.0000e+00, 2.9802e-08, 0.0000e+00, 2.9802e-08])
271+
DeepLiftShap Attributions: tensor([[-5.9169e-01, -1.5491e+00, -1.0076e+00],
272+
[-4.7101e-03, -2.2300e-01, -5.1926e+00]], grad_fn=<MeanBackward1>)
273+
Convergence Delta: tensor([-4.6120e-03, -1.6267e-03, -5.1045e-04, -1.4184e-03, -6.8886e-03,
274+
-2.2224e-02, 0.0000e+00, -2.8790e-02, -4.1285e-03, -2.7295e-02,
275+
-3.2349e-03, -1.6265e-03, -4.7684e-07, -1.4191e-03, -6.8889e-03,
276+
-2.2224e-02, 0.0000e+00, -2.4792e-02, -4.1289e-03, -2.7296e-02])
278277
```
279278
`DeepLiftShap` uses `DeepLift` to compute attribution score for each
280279
input-baseline pair and averages it for each input across all baselines.
@@ -303,10 +302,10 @@ print('IG + SmoothGrad Attributions: ', attributions, ' Convergence Delta: ', de
303302
```
304303
Output
305304
```
306-
IG + SmoothGrad Attributions: tensor([[0.0631, 0.1335, 0.0723],
307-
[0.0911, 0.0142, 0.1636]])
308-
Convergence Delta: tensor([ 1.4901e-07, -8.9407e-08, 1.1921e-07,
309-
1.4901e-07, 1.1921e-07, -1.7881e-07, -5.9605e-08, 5.9605e-08])
305+
IG + SmoothGrad Attributions: tensor([[-0.4574, -1.5493, -1.0893],
306+
[ 0.0000, -0.2647, -5.1619]])
307+
Convergence Delta: tensor([ 0.0000e+00, 2.3842e-07, 0.0000e+00, -2.3842e-07, 0.0000e+00,
308+
-4.7684e-07, 0.0000e+00, -4.7684e-07])
310309
311310
```
312311
The number of elements in the `delta` tensor is equal to: `n_samples * input.shape[0]`
@@ -334,8 +333,8 @@ print('Neuron Attributions: ', attributions)
334333
```
335334
Output
336335
```
337-
Neuron Attributions: tensor([[0.0106, 0.0247, 0.0150],
338-
[0.0144, 0.0021, 0.0301]])
336+
Neuron Attributions: tensor([[ 0.0000, 0.0000, 0.0000],
337+
[ 1.3358, 0.0000, -1.6811]])
339338
```
340339

341340
Layer conductance shows the importance of neurons for a layer and given input.
@@ -351,9 +350,9 @@ print('Layer Attributions: ', attributions, ' Convergence Delta: ', delta)
351350
```
352351
Outputs
353352
```
354-
Layer Attributions: tensor([[0.0000, 0.0515, 0.1811],
355-
[0.0000, 0.0477, 0.1652]], grad_fn=<SumBackward1>)
356-
Convergence Delta: tensor([-0.0363, -0.0560])
353+
Layer Attributions: tensor([[ 0.0000, 0.0000, -3.0856],
354+
[ 0.0000, -0.3488, -4.9638]], grad_fn=<SumBackward1>)
355+
Convergence Delta: tensor([0.0630, 0.1084])
357356
```
358357

359358
Similar to other attribution algorithms that return convergence delta, LayerConductance

tutorials/CIFAR_TorchVision_Interpret.ipynb

Lines changed: 73 additions & 35 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)