5
5
#
6
6
# This example will show you how to implement custom LRP rules and register custom layers
7
7
# and activation functions.
8
- # For this purpose, we will quickly load our model from the previous section:
8
+ # For this purpose, we will quickly load our model from the previous section
9
9
using ExplainableAI
10
10
using Flux
11
11
using MLDatasets
@@ -14,14 +14,14 @@ using BSON
14
14
15
15
model = BSON. load (" ../model.bson" , @__MODULE__ )[:model ]
16
16
17
+ # and data from the MNIST dataset
17
18
index = 10
18
19
x, _ = MNIST (Float32, :test )[10 ]
19
20
input = reshape (x, 28 , 28 , 1 , :);
20
21
21
22
# ## Custom LRP composites
22
- # Instead of creating an LRP-analyzer from a single rule (e.g. `LRP(model, GammaRule())`),
23
- # we can also assign rules to each layer individually.
24
- # For this purpose, we create an array of rules that matches the length of the Flux chain:
23
+ # When creating an LRP-analyzer, we can assign individual rules to each layer.
24
+ # The array of rules has to match the length of the Flux chain:
25
25
rules = [
26
26
ZBoxRule (0.0f0 , 1.0f0 ),
27
27
EpsilonRule (),
@@ -43,6 +43,44 @@ heatmap(input, analyzer)
43
43
# md # Not all models can be flattened, e.g. those using
44
44
# md # `Parallel` and `SkipConnection` layers.
45
45
46
+ # Instead of manually defining a list of rules, we can also use a [`Composite`](@ref).
47
+ # A composite contructs a list of LRP-rules by sequentially applying composite primitives.
48
+ #
49
+ # To obtain the same set of rules as in the previous example, we can define
50
+ composite = Composite (
51
+ ZeroRule (), # default rule
52
+ GlobalRuleMap (
53
+ Conv => GammaRule (), # apply GammaRule on all convolutional layers
54
+ MaxPool => EpsilonRule (), # apply EpsilonRule on all pooling-layers
55
+ ),
56
+ FirstRule (ZBoxRule (0.0f0 , 1.0f0 )), # apply ZBoxRule on the first layer
57
+ )
58
+
59
+ analyzer = LRP (model, composite) # construct LRP analyzer from composite
60
+ heatmap (input, analyzer)
61
+
62
+ # This analyzer contains the same rules as our previous one:
63
+ analyzer. rules # show rules
64
+
65
+ # ### Composite primitives
66
+ # The following sets of primitives can used to construct a [`Composite`](@ref).
67
+ #
68
+ # To apply a single rule, use:
69
+ # * [`LayerRule`](@ref) to apply a rule to the `n`-th layer of a model
70
+ # * [`GlobalRule`](@ref) to apply a rule to all layers of a model
71
+ # * [`RangeRule`](@ref) to apply a rule to a positional range of layers of a model
72
+ # * [`FirstRule`](@ref) to apply a rule to the first layer of a model
73
+ # * [`LastRule`](@ref) to apply a rule to the last layer of a model
74
+ #
75
+ # To apply a set of rules to multiple layers, use:
76
+ # * [`GlobalRuleMap`](@ref) to apply a dictionary that maps layer types to LRP-rules
77
+ # * [`RangeRuleMap`](@ref) for a `RuleMap` on generalized ranges
78
+ # * [`FirstNRuleMap`](@ref) for a `RuleMap` on the first `n` layers of a model
79
+ # * [`LastNRuleMap`](@ref) for a `RuleMap` on the last `n` layers
80
+ #
81
+ # Primitives are called sequentially in the order the `Composite` was created with
82
+ # and overwrite rules specified by previous primitives.
83
+
46
84
# ## Custom LRP rules
47
85
# Let's define a rule that modifies the weights and biases of our layer on the forward pass.
48
86
# The rule has to be of type `AbstractLRPRule`.
0 commit comments