Flux.jl icon indicating copy to clipboard operation
Flux.jl copied to clipboard

display fieldnames in show of a container

Open CarloLucibello opened this issue 11 months ago • 3 comments
trafficstars

I think it would be helpfuld to display the fieldnames in our bigshow. For example, we currently have

julia> rnn = RNN(4 => 6)
RNN(
  RNNCell(4 => 6, tanh),                # 66 parameters
)     

that could become

julia> rnn = RNN(4 => 6)
RNN(
  cell = RNNCell(4 => 6, tanh),                # 66 parameters
)     

CarloLucibello avatar Dec 07 '24 07:12 CarloLucibello

The present recursion roughly assumes the existence of the default constructor. This instead assumes the existence of a keyword constructor, which is much less common:

julia> RNN(
         cell = RNNCell(4 => 6, tanh),                # 66 parameters
       )
ERROR: MethodError: no method matching RNN(; cell::RNNCell{typeof(tanh), Matrix{Float32}, Matrix{Float32}, Vector{Float32}})
The type `RNN` exists, but no method is defined for this combination of argument types when trying to construct it.

Closest candidates are:
...
  RNN(::M) where M got unsupported keyword argument "cell"
   @ Flux ~/.julia/packages/Flux/5vIRy/src/layers/recurrent.jl:171

You could put field names in the comment without breaking anything, # .cell, 66 parameters.

It does show names for some where a keyword constructor is known to exist. That mechanism could surely be made more generalisable it someone wants it enough:

julia> Chain(one=RNN(4 => 6, tanh), two=identity)
Chain(
  one = RNN(
    RNNCell(4 => 6, tanh),              # 66 parameters
  ),
  two = identity,
)                   # Total: 3 arrays, 66 parameters, 424 bytes.

mcabbott avatar Dec 07 '24 15:12 mcabbott

This is because you want the output to be a valid constructor when copy-pasted, right?

CarloLucibello avatar Dec 09 '24 09:12 CarloLucibello

We can have an option for Flux.@layer :named MyModel to display the field names. Even if copy-pasting cannot construct I think it would be useful.

CarloLucibello avatar Jan 01 '25 09:01 CarloLucibello