@@ -20,9 +20,10 @@ x, _ = MNIST(Float32, :test)[10]
20
20
input = reshape (x, 28 , 28 , 1 , :);
21
21
22
22
# ## LRP composites
23
- # ### Custom composites
23
+ # ### Assigning individual rules
24
24
# When creating an LRP-analyzer, we can assign individual rules to each layer.
25
- # The array of rules has to match the length of the Flux chain:
25
+ # The array of rules has to match the length of the Flux chain.
26
+ # The `LRP` analyzer will show a summary of how layers and rules got matched:
26
27
rules = [
27
28
ZBoxRule (0.0f0 , 1.0f0 ),
28
29
EpsilonRule (),
@@ -35,6 +36,8 @@ rules = [
35
36
]
36
37
37
38
analyzer = LRP (model, rules)
39
+
40
+ #
38
41
heatmap (input, analyzer)
39
42
40
43
# Since some Flux Chains contain other Flux Chains, ExplainableAI provides
@@ -44,8 +47,10 @@ heatmap(input, analyzer)
44
47
# md # Not all models can be flattened, e.g. those using
45
48
# md # `Parallel` and `SkipConnection` layers.
46
49
50
+ # ### Custom composites
47
51
# Instead of manually defining a list of rules, we can also use a [`Composite`](@ref).
48
- # A composite contructs a list of LRP-rules by sequentially applying composite primitives.
52
+ # A composite contructs a list of LRP-rules by sequentially applying
53
+ # [Composite primitives](@ref composite_primitive_api) it contains.
49
54
#
50
55
# To obtain the same set of rules as in the previous example, we can define
51
56
composite = Composite (
@@ -57,11 +62,12 @@ composite = Composite(
57
62
FirstLayerRule (ZBoxRule (0.0f0 , 1.0f0 )), # apply ZBoxRule on the first layer
58
63
)
59
64
60
- analyzer = LRP (model, composite) # construct LRP analyzer from composite
61
- heatmap (input, analyzer )
65
+ # We now construct an LRP analyzer from ` composite`
66
+ analyzer = LRP (model, composite )
62
67
63
- # This analyzer contains the same rules as our previous one:
64
- analyzer. rules # show rules
68
+ # As you can see, this analyzer contains the same rules as our previous one
69
+ # and therefore also produces the same heatmaps:
70
+ heatmap (input, analyzer)
65
71
66
72
# ### Composite primitives
67
73
# The following [Composite primitives](@ref composite_primitive_api) can used to construct a [`Composite`](@ref).
@@ -87,7 +93,9 @@ analyzer.rules # show rules
87
93
# ### Default composites
88
94
# A list of implemented default composites can be found under
89
95
# [Default composites](@ref default_composite_api) in the API reference, e.g. [`EpsilonPlusFlat`](@ref):
90
- EpsilonPlusFlat ()
96
+ composite = EpsilonPlusFlat ()
97
+ #
98
+ analyzer = LRP (model, composite)
91
99
92
100
# ## Custom LRP rules
93
101
# Let's define a rule that modifies the weights and biases of our layer on the forward pass.
@@ -101,7 +109,7 @@ struct MyGammaRule <: AbstractLRPRule end
101
109
import ExplainableAI: modify_param!
102
110
103
111
function modify_param! (:: MyGammaRule , param)
104
- param .+ = 0.25 * relu .(param)
112
+ param .+ = 0.25f0 * relu .(param)
105
113
return nothing
106
114
end
107
115
0 commit comments