Skip to content

Commit 0d83f60

Browse files
authored
Merge pull request #2210 from mcabbott/show4
Fix a bug in show
2 parents 0155f61 + 35f018a commit 0d83f60

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/layers/show.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ function _layer_show(io::IO, layer, indent::Int=0, name=nothing)
7777
print(io, " "^indent, str, indent==0 ? "" : ",")
7878
if !isempty(params(layer))
7979
print(io, " "^max(2, (indent==0 ? 20 : 39) - indent - length(str)))
80-
printstyled(io, "# ", underscorise(sum(length, params(layer))), " parameters"; color=:light_black)
81-
nonparam = _childarray_sum(length, layer) - sum(length, params(layer))
80+
printstyled(io, "# ", underscorise(sum(length, params(layer); init=0)), " parameters";
81+
color=:light_black)
82+
nonparam = _childarray_sum(length, layer) - sum(length, params(layer), init=0)
8283
if nonparam > 0
8384
printstyled(io, ", plus ", underscorise(nonparam), indent==0 ? " non-trainable" : ""; color=:light_black)
8485
end
@@ -90,11 +91,11 @@ end
9091
function _big_finale(io::IO, m)
9192
ps = params(m)
9293
if length(ps) > 2
93-
pars = underscorise(sum(length, ps))
94+
pars = underscorise(sum(length, ps; init=0))
9495
bytes = Base.format_bytes(Base.summarysize(m))
9596
noncnt = _childarray_sum(_->1, m) - length(ps)
9697
if noncnt > 0
97-
nonparam = underscorise(_childarray_sum(length, m) - sum(length, ps))
98+
nonparam = underscorise(_childarray_sum(length, m) - sum(length, ps; init=0))
9899
printstyled(io, " "^08, "# Total: ", length(ps), " trainable arrays, "; color=:light_black)
99100
println(io, pars, " parameters,")
100101
printstyled(io, " "^10, "# plus ", noncnt, " non-trainable, ", nonparam, " parameters, summarysize "; color=:light_black)
@@ -107,7 +108,8 @@ function _big_finale(io::IO, m)
107108
end
108109

109110
_childarray_sum(f, x::AbstractArray{<:Number}) = f(x)
110-
_childarray_sum(f, x) = isleaf(x) ? 0 : sum(y -> _childarray_sum(f, y), Functors.children(x))
111+
_childarray_sum(f, x) = isleaf(x) ? 0 : sum(y -> _childarray_sum(f, y), Functors.children(x),
112+
init=0)
111113

112114
# utility functions
113115

test/layers/show.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,12 @@ end
7373
@test occursin("Dense(2 => 2)", adjoint_chain)
7474
@test occursin("Chain([", adjoint_chain)
7575
end
76+
77+
# Bug when no children, https://github.com/FluxML/Flux.jl/issues/2208
78+
struct NoFields end
79+
Flux.@functor NoFields
80+
81+
@testset "show with no fields" begin
82+
str = repr("text/plain", Chain(Dense(1=>1), Dense(1=>1), NoFields()))
83+
@test occursin("4 arrays, 4 parameters", str)
84+
end

0 commit comments

Comments
 (0)