Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Passing field names to @functor for typed struct gives constructor error #6

Closed
andrschl opened this issue Dec 4, 2020 · 2 comments
Closed

Comments

@andrschl
Copy link

andrschl commented Dec 4, 2020

I am not sure whether this is intended or not, but when trying to specify the trainable parameters in the following code a "no method matching error" for the constructor is returned.

struct MyLayer{R, S, T}
    a::R
    b::S
    c::T
end
Flux.@functor MyLayer (a,b)
m = MyLayer(Dense(1,10,tanh), Dense(1,10,tanh), zeros(5))
Flux.destructure(m) # returns no method matching error for the constructor

In the last sentence of the corresponding section in the documentation it is mentioned that a corresponding constructor has to exist. However, it is not clear to me how this constructor should look like.

@devmotion
Copy link

This issue seems to be the same as the one discussed in #3. You can check the comments therein for a solution to your problem: Instead of Flux.@functor MyLayer (a,b) you can define

function Functors.functor(::Type{<:MyLayer}, x)
    function reconstruct_MyLayer(xs)
        return MyLayer(xs.a, xs.b, x.c)
    end
    return (a = x.a, b = x.b), reconstruct_MyLayer
end

@CarloLucibello
Copy link
Member

addressed in #7

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants