I’m having a hard time understanding the behavior of the
@functor macro. See the snippets below: For the first one, I follow the tutorial on custom layers step by step and it works. For the second one, I alter the code slightly for my own custom layer and
@functor is no longer able to peek into the struct, leading to empty
Can anyone share any insights on why the two codes work differently? Thanks in advance.
using Flux #Custom Layer from the Tutorial struct Affine W b end # Some constructor Affine(in::Integer, out::Integer) = Affine(randn(out, in), randn(out)) # Overload call, so the object can be used as a function (m::Affine)(x) = m.W * x .+ m.b a = Affine(10, 5) a(rand(10)) # Works: 5-element vector Flux.@functor Affine Flux.params(a) #Works
#My Custom Layer struct MyLayer a b c end # Some constructor MyLayer(a::Real, b::Real) = MyLayer(a,b,-b) # Overload call, so the object can be used as a function (m::MyLayer)(x) = [x,m.a*x + m.b*x] b = MyLayer(1.,2.) b(rand(2)) # Works: 2-element vector Flux.@functor MyLayer Flux.params(b) # Doesn't work: Params()
Got these results on a fresh VSCode session, using Flux v.0.13.13.