How to get the number of tracked parameters?

I’m sure there is a way to get the number of tracked parameters by Flux.Tracker, but so far I still haven’t found any way of actually getting this number.

Suppose I make the following:

forward = Chain(
    Conv((5,5), 3=>16, relu),
    x -> maxpool(x, PoolDims(x,(2,2))),
    Conv((5,5), 16=>8, relu),
    x -> maxpool(x, PoolDims(x,(2,2))),
    x -> reshape(x, :, size(x, 4)),
    Dense(200, 120),
    Dense(120, 84))

How would I then go about finding the actual parameters this tracks?

ps = params(forward) #An object referencing all the parameters
sum(length(p) for p ∈ ps) #The total number of parameters
3 Likes

Or even shorter

sum(length, ps)
3 Likes

Wait, why does that even work?
And how would I ever figure out that that could work?

It’s explained in the docstring for sum.

2 Likes