-
Notifications
You must be signed in to change notification settings - Fork 226
Provide AD gradient for MLE/MAP #1369
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
Changes from 3 commits
da777ba
fc2f8df
284bbdf
2ddd480
5ecc065
959ff51
f431e2b
53d9134
fa7c59f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,6 +147,32 @@ function (f::OptimLogDensity)(z) | |
return -DynamicPPL.getlogp(varinfo) | ||
end | ||
|
||
function (f::OptimLogDensity)(F, G, z) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if it's useful to keep this separate definition? It seems we only need |
||
spl = DynamicPPL.SampleFromPrior() | ||
|
||
# Calculate log joint and the gradient | ||
l, g = gradient_logp( | ||
z, | ||
DynamicPPL.VarInfo(f.vi, spl, z), | ||
f.model, | ||
DynamicPPL.SampleFromPrior(), | ||
f.context | ||
) | ||
|
||
if G !== nothing | ||
cpfiffer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Use the negative gradient because we are minimizing. | ||
G[:] = -g | ||
end | ||
|
||
if F !== nothing | ||
# Return the negative log joint because we are minimizing. | ||
F = -l | ||
return F | ||
end | ||
|
||
return nothing | ||
end | ||
|
||
""" | ||
ModeResult{ | ||
V<:NamedArrays.NamedArray, | ||
|
@@ -369,6 +395,11 @@ function _optimize( | |
args...; | ||
kwargs... | ||
) | ||
# Throw an error if we received a second-order optimizer. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have to do that? Doesn't Optim just use ForwardDiff (or FD?) to compute the Hessian in this case? If that's the case, then we shouldn't throw an error IMO. It might not be the most efficient approach and would not adhere to the user-provided AD settings but as long as it works we could only print a warning. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In FD yes; but (for example in the project that I am working on) it could be that users only define custom adjoints for the gradients but not the Hessian. Therefore even the user provides an AD backend, it might not be a great idea if it by default take that for Hessian function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean we shouldn't even print a warning? Would be fine with me as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I think throwing an error when some Hessian-required optimizer is received is a great idea, just like what Cameron did here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to throw an error if the Hessian is evaluated, I suggest using if H !== nothing
error("second order methods are not supported at the moment")
end In general, this approach is more flexible, avoids baking in a hardcoded check for a special type of a different package in our implementation, and avoids incorrect and unexpected behaviour for second-order optimization algorithms that don't subtype this specific type (since multiple inheritance is not possible in Julia, that's not an impossible scenario per se). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bug. I think there might be an issue for it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just found JuliaNLSolvers/Optim.jl#718, I guess that's the related issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I have a fix. Sorry to cossio for waiting a year and a half 😬 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean, I will tag a fix in an hour or so, so please don't special case with a branch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if optimizer isa Optim.SecondOrderOptimizer | ||
throw(ArgumentError("Second order optimizers for MLE/MAP are not yet supported.")) | ||
end | ||
|
||
# Do some initialization. | ||
spl = DynamicPPL.SampleFromPrior() | ||
|
||
|
@@ -378,9 +409,8 @@ function _optimize( | |
link!(f.vi, spl) | ||
init_vals = f.vi[spl] | ||
|
||
|
||
# Optimize! | ||
M = Optim.optimize(f, init_vals, optimizer, options, args...; kwargs...) | ||
M = Optim.optimize(Optim.only_fg!(f), init_vals, optimizer, options, args...; kwargs...) | ||
|
||
# Warn the user if the optimization did not converge. | ||
if !Optim.converged(M) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make sure that we haven't introduced any breaking changes since 0.13.0. (IMO we should adopt the ColPrac practice of making patch releases for every PR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll just bump it up to 0.14.0. Honestly at this point we should consider moving to 1.0 as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More importantly, current 0.13.0 is by default failing so
] add Turing
andusing Turing
will fail. I think maybe you guys want to bump up the version really soon...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We first have to fix the bug introduced by the changes in PDMats 0.10 on master before releasing 0.14.0. What was your package setup that failed, i.e. can you post the output of
] st
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I have mine on Turing#master, but there are more than one guys on slack that faces an issue:
] add Turing
installs an older version andusing Turing
somehow fails.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually these problems are caused by unbounded compatibilities of old Turing versions (there are many closed issues in the repo here). These issues should be fixed by running
] add [email protected]
and possibly adjusting conflicting packages (by users) and adding correct bounds in the registry (by us). I fixed some bounds a while ago, but it seems the old version are still missing some compatibilty bounds.