Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Differentiating through my custom struct its restructuring throws an error #1796

Closed
JTaets opened this issue Dec 2, 2021 · 4 comments
Closed

Comments

@JTaets
Copy link

JTaets commented Dec 2, 2021

Title says all.

I could boil down my errors to this mwe.

using Flux

struct Model
    model
    offset
end
@Flux.functor Model (model,)
(m::Model)(x) =m.model(x+m.offset)

model = Model(Dense(1,1),[4])
p,re = Flux.destructure(model)
println(length(p)) #2 parameters
Flux.gradient((p)->sum(re(p)([1])),p) #doesn't work

The last lines throws an error DimensionMismatch("variable with size(x) == (2,) cannot have a gradient with size(dx) == (3,)")

@DhairyaLGandhi
Copy link
Member

Could you try with @functor Model

@JTaets
Copy link
Author

JTaets commented Dec 2, 2021

Then it works, but I don't want this offset to be differentiable

@ToucheSir
Copy link
Member

ToucheSir commented Dec 2, 2021

Try (m::Model)(x) = m.model(x + Zygote.dropgrad(m.offset))1 with your original @functor Model (model,). Just tested and it works for me locally.

Footnotes

  1. https://fluxml.ai/Zygote.jl/latest/utils/#Zygote.dropgrad

@JTaets
Copy link
Author

JTaets commented Dec 2, 2021

This also works for my actual case. Thank you!

@JTaets JTaets closed this as completed Dec 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants