Skip to content

Commit

Permalink
add NoShow as an (orthogonalised) alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott committed Aug 28, 2023
1 parent 8a1832a commit 6734e89
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Fluxperimental.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ include("chain.jl")

include("compact.jl")

include("noshow.jl")
export NoShow

include("new_recur.jl")

end # module Fluxperimental
2 changes: 1 addition & 1 deletion src/compact.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function Flux._big_show(io::IO, obj::CompactLayer, indent::Int=0, name=nothing)
setup_strings = obj.setup_strings
layer, input, block = obj.strings
pre, post = ("(", ")")
println(io, " "^indent, "@compact", pre)
println(io, " "^indent, isnothing(name) ? "" : "$name = ", layer, pre)
for k in keys(obj.variables)
v = obj.variables[k]
if Flux._show_leaflike(v)
Expand Down
60 changes: 60 additions & 0 deletions src/noshow.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

"""
NoShow(layer)
NoShow(string, layer)
This alters printing (for instance at the REPL prompt) to let you hide the complexity
of some part of a Flux model. It has no effect on the actual running of the model.
By default it prints `NoShow(...)` instead of the given layer.
If you provide a string, it prints that instead -- it can be anything,
but it may make sense to print the name of a function which will
re-create the same structure.
# Examples
```jldoctest
julia> Chain(Dense(2 => 3), NoShow(Parallel(vcat, Dense(3 => 4), Dense(3 => 5))), Dense(9 => 10))
Chain(
Dense(2 => 3), # 9 parameters
NoShow(...), # 36 parameters
Dense(9 => 10), # 100 parameters
) # Total: 8 arrays, 145 parameters, 1.191 KiB.
julia> PseudoLayer((i,o)::Pair) = Parallel(+, Dense(i => o, relu), Dense(i => o, tanh));
julia> mid = PseudoLayer(3 => 10);
julia> Chain(Dense(2 => 3), NoShow("PseudoLayer(3 => 10)", mid), Dense(9 => 10))
Chain(
Dense(2 => 3), # 9 parameters
PseudoLayer(3 => 10), # 80 parameters
Dense(9 => 10), # 100 parameters
) # Total: 8 arrays, 189 parameters, 1.379 KiB.
```
"""
struct NoShow{T}
str::String
layer::T
end

NoShow(layer) = NoShow("", layer)

Flux.@functor NoShow

(no::NoShow)(x...) = no.layer(x...)

Base.show(io::IO, no::NoShow) = print(io, isempty(no.str) ? "NoShow(...)" : no.str)

Flux._show_leaflike(::NoShow) = true # I think this is right
Flux._show_children(::NoShow) = (;) # Seems to be needed?

function Base.show(io::IO, ::MIME"text/plain", m::NoShow)
if get(io, :typeinfo, nothing) === nothing # e.g., top level of REPL
Flux._big_show(io, m)
elseif !get(io, :compact, false) # e.g., printed inside a Vector, but not a matrix
Flux._layer_show(io, m)
else
show(io, m)
end
end

0 comments on commit 6734e89

Please sign in to comment.