|
| 1 | +""" |
| 2 | + @big_show MyContainer |
1 | 3 |
|
2 |
| -for T in [ |
3 |
| - :Chain, :Parallel, :SkipConnection, :Recur, :Maxout, :PairwiseFusion # container types |
4 |
| - ] |
5 |
| - @eval function Base.show(io::IO, m::MIME"text/plain", x::$T) |
6 |
| - if get(io, :typeinfo, nothing) === nothing # e.g. top level in REPL |
7 |
| - _big_show(io, x) |
8 |
| - elseif !get(io, :compact, false) # e.g. printed inside a Vector, but not a Matrix |
9 |
| - _layer_show(io, x) |
10 |
| - else |
11 |
| - show(io, x) |
| 4 | +This macro lets you opt-in to Flux's fancy printing. |
| 5 | +
|
| 6 | +When `model::MyContainer` is returned at the REPL it will be treated like `Chain`, |
| 7 | +and the printing routine will recursively unfold its children. |
| 8 | +This is triggered by adding a method to 3-arg `Base.show(io::IO, ::MIME"text/plain", l::MyContainer)`. |
| 9 | +
|
| 10 | +Custom layers which do not contain other layers (more like `Dense` than like `Chain`) |
| 11 | +need not call this, and should simply define 2-arg `Base.show(io::IO, l::MyLayer)`. |
| 12 | +
|
| 13 | +# Example |
| 14 | +```jldoctest |
| 15 | +julia> struct Trio{A,B,C}; a::A; b::B; c::C end |
| 16 | +
|
| 17 | +julia> Flux.@functor Trio |
| 18 | +
|
| 19 | +julia> Flux.@big_show Trio |
| 20 | +
|
| 21 | +julia> tri = Trio(Dense(10=>5,tanh), Dense(5=>2), softmax) |
| 22 | +Trio( |
| 23 | + Dense(10 => 5, tanh), # 55 parameters |
| 24 | + Dense(5 => 2), # 12 parameters |
| 25 | + NNlib.softmax, |
| 26 | +) # Total: 4 arrays, 67 parameters, 492 bytes. |
| 27 | +``` |
| 28 | +
|
| 29 | +Note that there is no automatic method for 2-arg `show`, and thus |
| 30 | +something like `(tri, tri)` will print all the type parameters. |
| 31 | +
|
| 32 | +However, `Chain(tri, tri)` will always use Flux's recursive printing, |
| 33 | +even without using this macro: `Chain` is the entry point. |
| 34 | +""" |
| 35 | +macro big_show(ex) |
| 36 | + ex isa Symbol || error("usage is `Flux.@big_show Chain`") |
| 37 | + eex = esc(ex) |
| 38 | + quote |
| 39 | + function Base.show(io::IO, m::MIME"text/plain", x::$eex) |
| 40 | + if get(io, :typeinfo, nothing) === nothing # e.g. top level in REPL |
| 41 | + _big_show(io, x) |
| 42 | + elseif !get(io, :compact, false) # e.g. printed inside a Vector, but not a Matrix |
| 43 | + _layer_show(io, x) |
| 44 | + else |
| 45 | + show(io, x) |
| 46 | + end |
| 47 | + end |
12 | 48 | end
|
13 |
| - end |
14 | 49 | end
|
15 | 50 |
|
| 51 | +@big_show Chain |
| 52 | +@big_show Parallel |
| 53 | +@big_show SkipConnection |
| 54 | +@big_show Recur |
| 55 | +@big_show Maxout |
| 56 | + |
16 | 57 | function _big_show(io::IO, obj, indent::Int=0, name=nothing)
|
17 | 58 | pre, post = obj isa Chain{<:AbstractVector} ? ("([", "])") : ("(", ")")
|
18 | 59 | children = _show_children(obj)
|
|
0 commit comments