Skip to content

Commit bb24791

Browse files
committed
Tweak printing of composites
1 parent 4f87e40 commit bb24791

File tree

8 files changed

+39
-25
lines changed

8 files changed

+39
-25
lines changed

src/lrp/show.jl

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,52 @@ _range_string(::LastLayerTypeRule) = "last layer"
1111
_range_string(r::FirstNTypeRule) = "layers $(1:r.n)"
1212
_range_string(r::LastNTypeRule) = "last $(r.n) layers"
1313

14+
const COLOR_COMMENT = :light_black
15+
const COLOR_RULE = :yellow
16+
const COLOR_TYPE = :blue
17+
const COLOR_RANGE = :green
18+
19+
typename(x) = string(nameof(typeof(x)))
20+
1421
function Base.show(io::IO, m::MIME"text/plain", c::Composite)
15-
println(io, "Composite(")
22+
println(io, "Composite", "(")
1623
for p in c.primitives
1724
_show_primitive(io, p, 2)
1825
end
19-
return println(io, ")")
26+
println(io, ")")
27+
return nothing
2028
end
2129

2230
function _show_primitive(io::IO, r::AbstractRulePrimitive, indent::Int=0)
23-
print(io, " "^indent, nameof(typeof(r)), ": ")
24-
printstyled(io, _range_string(r); color=:blue)
31+
print(io, " "^indent, typename(r), ": ")
32+
printstyled(io, _range_string(r); color=COLOR_RANGE)
2533
print(io, " => ")
26-
printstyled(io, r.rule; color=:yellow)
34+
printstyled(io, r.rule; color=COLOR_RULE)
2735
println(io, ",")
2836
return nothing
2937
end
3038

31-
function _show_primitive(io::IO, r::AbstractCompositePrimitive, indent::Int=0)
32-
print(io, " "^indent, nameof(typeof(r)), "(")
33-
printstyled(io, " # on ", _range_string(r); color=:light_black)
39+
function _show_primitive(io::IO, r::AbstractTypeRulePrimitive, indent::Int=0)
40+
print(io, " "^indent, rpad(typename(r) * "(", 20))
41+
printstyled(io, "# on ", _range_string(r); color=COLOR_COMMENT)
3442
println(io)
3543
_print_rules(io, r.map, indent)
44+
println(io, " "^(indent), "),")
3645
return nothing
3746
end
3847

3948
function _print_rules(io::IO, rs, indent::Int=0)
4049
for r in rs
41-
printstyled(io, " "^(indent + 2), r.first; color=:blue)
42-
print(io, " => ")
43-
printstyled(io, r.second; color=:yellow)
50+
print(io, " "^(indent + 2))
51+
_print_type_rule(io, r)
4452
println(io, ",")
4553
end
46-
println(io, " "^(indent), "),")
54+
return nothing
55+
end
56+
57+
function _print_type_rule(io::IO, r::TypeRulePair)
58+
printstyled(io, r.first; color=COLOR_TYPE)
59+
print(io, " => ")
60+
printstyled(io, r.second; color=COLOR_RULE)
4761
return nothing
4862
end

test/references/show/EpsilonAlpha2Beta1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Composite(
2-
GlobalTypeRule( # on all layers
2+
GlobalTypeRule( # on all layers
33
Conv => AlphaBetaRule{Float32}(2.0f0, 1.0f0),
44
Dense => EpsilonRule{Float32}(1.0f-6),
55
Union{typeof(Flux.dropout), AlphaDropout, Dropout} => PassRule(),

test/references/show/EpsilonAlpha2Beta1Flat.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Composite(
2-
GlobalTypeRule( # on all layers
2+
GlobalTypeRule( # on all layers
33
Conv => AlphaBetaRule{Float32}(2.0f0, 1.0f0),
44
Dense => EpsilonRule{Float32}(1.0f-6),
55
Union{typeof(Flux.dropout), AlphaDropout, Dropout} => PassRule(),
66
Union{typeof(flatten), typeof(MLUtils.flatten)} => PassRule(),
77
),
8-
FirstLayerTypeRule( # on first layer
8+
FirstLayerTypeRule( # on first layer
99
Conv => FlatRule(),
1010
Dense => FlatRule(),
1111
),
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Composite(
2-
GlobalTypeRule( # on all layers
2+
GlobalTypeRule( # on all layers
33
Conv => GammaRule{Float32}(0.25f0),
44
Dense => EpsilonRule{Float32}(1.0f-6),
55
Union{typeof(Flux.dropout), AlphaDropout, Dropout} => PassRule(),
66
Union{typeof(flatten), typeof(MLUtils.flatten)} => PassRule(),
77
),
8-
FirstLayerTypeRule( # on first layer
8+
FirstLayerTypeRule( # on first layer
99
Conv => ZBoxRule{Float32}(-3.0f0, 3.0f0),
1010
),
1111
)

test/references/show/EpsilonPlus.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Composite(
2-
GlobalTypeRule( # on all layers
2+
GlobalTypeRule( # on all layers
33
Conv => ZPlusRule(),
44
Dense => EpsilonRule{Float32}(1.0f-6),
55
Union{typeof(Flux.dropout), AlphaDropout, Dropout} => PassRule(),

test/references/show/EpsilonPlusFlat.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Composite(
2-
GlobalTypeRule( # on all layers
2+
GlobalTypeRule( # on all layers
33
Conv => ZPlusRule(),
44
Dense => EpsilonRule{Float32}(1.0f-6),
55
Union{typeof(Flux.dropout), AlphaDropout, Dropout} => PassRule(),
66
Union{typeof(flatten), typeof(MLUtils.flatten)} => PassRule(),
77
),
8-
FirstLayerTypeRule( # on first layer
8+
FirstLayerTypeRule( # on first layer
99
Conv => FlatRule(),
1010
Dense => FlatRule(),
1111
),

test/references/show/composite1.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
Composite(
22
GlobalRule: all layers => ZeroRule(),
33
GlobalRule: all layers => PassRule(),
4-
GlobalTypeRule( # on all layers
4+
GlobalTypeRule( # on all layers
55
Conv => AlphaBetaRule{Float32}(2.0f0, 1.0f0),
66
Dense => EpsilonRule{Float32}(1.0f-6),
77
Union{GlobalMaxPool, GlobalMeanPool, AdaptiveMaxPool, AdaptiveMeanPool, MaxPool, MeanPool} => EpsilonRule{Float32}(1.0f-6),
88
),
9-
FirstNTypeRule( # on layers 1:7
9+
FirstNTypeRule( # on layers 1:7
1010
Conv => FlatRule(),
1111
),
12-
LastNTypeRule( # on last 3 layers
12+
LastNTypeRule( # on last 3 layers
1313
Dense => EpsilonRule{Float32}(1.0f-7),
1414
),
15-
RangeTypeRule( # on layers 4:10
15+
RangeTypeRule( # on layers 4:10
1616
Union{GlobalMaxPool, GlobalMeanPool, AdaptiveMaxPool, AdaptiveMeanPool, MaxPool, MeanPool} => EpsilonRule{Float32}(1.0f-5),
1717
),
1818
LayerRule: layer 9 => AlphaBetaRule{Float32}(1.0f0, 0.0f0),

test/references/show/composite2.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Composite(
33
Dense => EpsilonRule{Float32}(2.0f-5),
44
Conv => EpsilonRule{Float32}(0.0002f0),
55
),
6-
FirstLayerTypeRule( # on first layer
6+
FirstLayerTypeRule( # on first layer
77
Dense => AlphaBetaRule{Float32}(1.0f0, 0.0f0),
88
Conv => AlphaBetaRule{Float32}(2.0f0, 1.0f0),
99
),

0 commit comments

Comments
 (0)