@@ -120,16 +120,15 @@ class ToyModel(nn.Module):
120
120
self.lin1 = nn.Linear(3, 3)
121
121
self.relu = nn.ReLU()
122
122
self.lin2 = nn.Linear(3, 2)
123
- self.sigmoid = nn.Sigmoid()
124
123
125
124
# 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))
127
126
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))
129
128
self.lin2.bias = nn.Parameter(torch.ones(1,2))
130
129
131
130
def forward(self, input):
132
- return self.sigmoid(self. lin2(self.relu(self.lin1(input) )))
131
+ return self.lin2(self.relu(self.lin1(input)))
133
132
```
134
133
135
134
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)
176
175
```
177
176
Output:
178
177
```
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 ])
182
181
```
183
182
The algorithm outputs an attribution score for each input element and a
184
183
convergence delta. The lower the absolute value of the convergence delta the better
@@ -217,9 +216,9 @@ print('GradientShap Attributions: ', attributions, ' Convergence Delta: ', delta
217
216
```
218
217
Output
219
218
```
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 ])
223
222
224
223
```
225
224
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)
243
242
```
244
243
Output
245
244
```
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 ])
248
247
Convergence Delta: tensor([0., 0.])
249
248
```
250
249
DeepLift assigns similar attribution scores as Integrated Gradients to inputs,
@@ -269,12 +268,12 @@ print('DeepLiftSHAP Attributions: ', attributions, ' Convergence Delta: ', delta
269
268
```
270
269
Output
271
270
```
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 ])
278
277
```
279
278
` DeepLiftShap ` uses ` DeepLift ` to compute attribution score for each
280
279
input-baseline pair and averages it for each input across all baselines.
@@ -303,10 +302,10 @@ print('IG + SmoothGrad Attributions: ', attributions, ' Convergence Delta: ', de
303
302
```
304
303
Output
305
304
```
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])
310
309
311
310
```
312
311
The number of elements in the ` delta ` tensor is equal to: ` n_samples * input.shape[0] `
@@ -334,8 +333,8 @@ print('Neuron Attributions: ', attributions)
334
333
```
335
334
Output
336
335
```
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 ]])
339
338
```
340
339
341
340
Layer conductance shows the importance of neurons for a layer and given input.
@@ -351,9 +350,9 @@ print('Layer Attributions: ', attributions, ' Convergence Delta: ', delta)
351
350
```
352
351
Outputs
353
352
```
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 ])
357
356
```
358
357
359
358
Similar to other attribution algorithms that return convergence delta, LayerConductance
0 commit comments