|
1 |
| -function gradient_wrt_input(model, input, ns::AbstractOutputSelector) |
2 |
| - output, back = Zygote.pullback(model, input) |
3 |
| - output_indices = ns(output) |
4 |
| - |
5 |
| - # Compute VJP w.r.t. full model output, selecting vector s.t. it masks output neurons |
6 |
| - v = zero(output) |
7 |
| - v[output_indices] .= 1 |
8 |
| - grad = only(back(v)) |
9 |
| - return grad, output, output_indices |
| 1 | +function forward_with_output_selection(model, input, selector::AbstractOutputSelector) |
| 2 | + output = model(input) |
| 3 | + sel = selector(output) |
| 4 | + return output[sel] |
| 5 | +end |
| 6 | + |
| 7 | +function gradient_wrt_input( |
| 8 | + model, input, output_selector::AbstractOutputSelector, backend::AbstractADType |
| 9 | +) |
| 10 | + output = model(input) |
| 11 | + return gradient_wrt_input(model, input, output, output_selector, backend) |
| 12 | +end |
| 13 | + |
| 14 | +function gradient_wrt_input( |
| 15 | + model, input, output, output_selector::AbstractOutputSelector, backend::AbstractADType |
| 16 | +) |
| 17 | + output_selection = output_selector(output) |
| 18 | + dy = zero(output) |
| 19 | + dy[output_selection] .= 1 |
| 20 | + |
| 21 | + output, grad = value_and_pullback(model, backend, input, dy) |
| 22 | + return grad, output, output_selection |
10 | 23 | end
|
11 | 24 |
|
12 | 25 | """
|
13 | 26 | Gradient(model)
|
14 | 27 |
|
15 | 28 | Analyze model by calculating the gradient of a neuron activation with respect to the input.
|
16 | 29 | """
|
17 |
| -struct Gradient{M} <: AbstractXAIMethod |
| 30 | +struct Gradient{M,B<:AbstractADType} <: AbstractXAIMethod |
18 | 31 | model::M
|
19 |
| - Gradient(model) = new{typeof(model)}(model) |
| 32 | + backend::B |
| 33 | + |
| 34 | + function Gradient(model::M, backend::B=DEFAULT_AD_BACKEND) where {M,B<:AbstractADType} |
| 35 | + new{M,B}(model, backend) |
| 36 | + end |
20 | 37 | end
|
21 | 38 |
|
22 | 39 | function call_analyzer(input, analyzer::Gradient, ns::AbstractOutputSelector; kwargs...)
|
23 |
| - grad, output, output_indices = gradient_wrt_input(analyzer.model, input, ns) |
| 40 | + grad, output, output_indices = gradient_wrt_input( |
| 41 | + analyzer.model, input, ns, analyzer.backend |
| 42 | + ) |
24 | 43 | return Explanation(
|
25 | 44 | grad, input, output, output_indices, :Gradient, :sensitivity, nothing
|
26 | 45 | )
|
|
32 | 51 | Analyze model by calculating the gradient of a neuron activation with respect to the input.
|
33 | 52 | This gradient is then multiplied element-wise with the input.
|
34 | 53 | """
|
35 |
| -struct InputTimesGradient{M} <: AbstractXAIMethod |
| 54 | +struct InputTimesGradient{M,B<:AbstractADType} <: AbstractXAIMethod |
36 | 55 | model::M
|
37 |
| - InputTimesGradient(model) = new{typeof(model)}(model) |
| 56 | + backend::B |
| 57 | + |
| 58 | + function InputTimesGradient( |
| 59 | + model::M, backend::B=DEFAULT_AD_BACKEND |
| 60 | + ) where {M,B<:AbstractADType} |
| 61 | + new{M,B}(model, backend) |
| 62 | + end |
38 | 63 | end
|
39 | 64 |
|
40 | 65 | function call_analyzer(
|
41 | 66 | input, analyzer::InputTimesGradient, ns::AbstractOutputSelector; kwargs...
|
42 | 67 | )
|
43 |
| - grad, output, output_indices = gradient_wrt_input(analyzer.model, input, ns) |
| 68 | + grad, output, output_indices = gradient_wrt_input( |
| 69 | + analyzer.model, input, ns, analyzer.backend |
| 70 | + ) |
44 | 71 | attr = input .* grad
|
45 | 72 | return Explanation(
|
46 | 73 | attr, input, output, output_indices, :InputTimesGradient, :attribution, nothing
|
|
0 commit comments