-
-
Couldn't load subscription status.
- Fork 615
show(::Chain) #1467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
show(::Chain) #1467
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
e5eeb57
fancy show for Chain & layers
2a9a011
pirate show for Params & Grads
6d051ba
tweaks & bug fixes
60a8f6b
more recursive big_show
f84e0e5
close brackets without wasting lines
12bad6d
tweaks
d3e2ee8
don't print BatchNorm as if it takes a keyword, ditto GroupNorm
d76da82
do print Conv keywords, including bias
3316366
move to file show.jl
a442b71
lots of fixes
1731fea
restore closing brackets to their own lines
d5a9051
check IOContext to disable within arrays etc.
e071efd
rm Zygote's types
5f3586d
conv doctests
92ac152
fix after rebase, tidy up & simplify
mcabbott 7f54144
add some tests
mcabbott 8f11ab8
change to use Base.summarysize
mcabbott dc5eca2
count non-trainable parameters
mcabbott 9690dfb
reduce indent
mcabbott 3e9c481
note non-trainables on every layer which has such
mcabbott 409c11e
print some totals not in light_black
mcabbott f65d514
extra when not indented
mcabbott 2423c1e
fix all doctests
mcabbott a5e39df
rm comment
mcabbott c91e731
tweaks
mcabbott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
|
|
||
| for T in [ | ||
| :Chain, :Parallel, :SkipConnection, :Recur # container types | ||
| ] | ||
| @eval function Base.show(io::IO, m::MIME"text/plain", x::$T) | ||
| if get(io, :typeinfo, nothing) === nothing # e.g. top level in REPL | ||
| _big_show(io, x) | ||
| elseif !get(io, :compact, false) # e.g. printed inside a Vector, but not a Matrix | ||
| _layer_show(io, x) | ||
| else | ||
| show(io, x) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| function _big_show(io::IO, obj, indent::Int=0) | ||
| children = trainable(obj) | ||
| if all(_show_leaflike, children) | ||
| _layer_show(io, obj, indent) | ||
| else | ||
| println(io, " "^indent, nameof(typeof(obj)), "(") | ||
| for c in children | ||
| _big_show(io, c, indent+2) | ||
| end | ||
| if indent == 0 | ||
| print(io, ")") | ||
| _big_finale(io, obj) | ||
| else | ||
| println(io, " "^indent, "),") | ||
| end | ||
| end | ||
| end | ||
|
|
||
| _show_leaflike(x) = isleaf(x) # mostly follow Functors, except for: | ||
| _show_leaflike(::Tuple{Vararg{<:Number}}) = true # e.g. stride of Conv | ||
| _show_leaflike(::Tuple{Vararg{<:AbstractArray}}) = true # e.g. parameters of LSTMcell | ||
| _show_leaflike(::Diagonal) = true # appears inside LayerNorm | ||
|
|
||
| for T in [ | ||
| :Conv, :ConvTranspose, :CrossCor, :DepthwiseConv, :Dense, | ||
| :BatchNorm, :LayerNorm, :InstanceNorm, :GroupNorm, | ||
| ] | ||
| @eval function Base.show(io::IO, m::MIME"text/plain", x::$T) | ||
| if !get(io, :compact, false) | ||
| _layer_show(io, x) | ||
| else | ||
| show(io, x) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| function _layer_show(io::IO, layer, indent::Int=0) | ||
| str = sprint(show, layer, context=io) | ||
| print(io, " "^indent, str, indent==0 ? "" : ",") | ||
| if !isempty(params(layer)) | ||
| print(io, " "^max(2, (indent==0 ? 20 : 39) - indent - length(str))) | ||
| printstyled(io, "# ", underscorise(sum(length, params(layer))), " parameters"; color=:light_black) | ||
| nonparam = _childarray_sum(length, layer) - sum(length, params(layer)) | ||
| if nonparam > 0 | ||
| printstyled(io, ", plus ", underscorise(nonparam); color=:light_black) | ||
| end | ||
| _nan_show(io, params(layer)) | ||
| end | ||
| indent==0 || println(io) | ||
| end | ||
|
|
||
| function _big_finale(io::IO, m) | ||
| ps = params(m) | ||
| if length(ps) > 2 | ||
| pars = underscorise(sum(length, ps)) | ||
| bytes = Base.format_bytes(Base.summarysize(m)) | ||
| noncnt = _childarray_sum(_->1, m) - length(ps) | ||
| if noncnt > 0 | ||
| nonparam = underscorise(_childarray_sum(length, m) - sum(length, ps)) | ||
| printstyled(io, " "^09, "# Total: ", length(ps), " trainable arrays, with "; color=:light_black) | ||
| println(io, pars, " parameters") | ||
| printstyled(io, " "^10, "# plus ", noncnt, " non-trainable, ", nonparam, " parameters, summarysize "; color=:light_black) | ||
| print(io, bytes) | ||
| else | ||
| printstyled(io, " "^19, "# Total: ", length(ps), " arrays, "; color=:light_black) | ||
| print(io, pars, " parameters, ", bytes) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| _childarray_sum(f, x::AbstractArray) = f(x) | ||
| _childarray_sum(f, x) = isleaf(x) ? 0 : sum(y -> _childarray_sum(f, y), Functors.children(x)) | ||
|
|
||
| # utility functions | ||
|
|
||
| underscorise(n::Integer) = | ||
| join(reverse(join.(reverse.(Iterators.partition(digits(n), 3)))), '_') | ||
|
|
||
| function _nan_show(io::IO, x) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed? Layers typically don't show their arrays, and custom layers should define their own, I don't want to take control of how they show their params. |
||
| if !isempty(x) && _all(iszero, x) | ||
| printstyled(io, " (all zero)", color=:cyan) | ||
| elseif _any(isnan, x) | ||
| printstyled(io, " (some NaN)", color=:red) | ||
| elseif _any(isinf, x) | ||
| printstyled(io, " (some Inf)", color=:red) | ||
| end | ||
| end | ||
|
|
||
| _any(f, xs::AbstractArray{<:Number}) = any(f, xs) | ||
| # _any(f, xs::Union{Tuple,NamedTuple,Zygote.Params}) = any(x -> _any(f, x), xs) | ||
| _any(f, xs) = any(x -> _any(f, x), xs) | ||
| _any(f, x::Number) = f(x) | ||
| # _any(f, x) = false | ||
|
|
||
| _all(f, xs) = !_any(!f, xs) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.