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

Provide AD gradient for MLE/MAP #1369

Merged
merged 9 commits into from
Aug 20, 2020
Merged

Provide AD gradient for MLE/MAP #1369

merged 9 commits into from
Aug 20, 2020

Conversation

cpfiffer
Copy link
Member

@cpfiffer cpfiffer commented Aug 4, 2020

Currently, MLE/MAP is not using the AD gradients calculated with gradient_logp, as noted in #1365. This PR modifies the MLE/MAP code to use the AD-generated gradient.

I incremented the version number to 0.13.1, but this might have to be 0.14.0 since we are technically dropping support for second-order methods with this PR. I intend to follow up in a separate PR with code that adds a hessian_logp method so people can use Optim.Newton or whatever -- it'll also make the information matrix calculations much better to use the AD-Hessian.

src/modes/ModeEstimation.jl Show resolved Hide resolved
@@ -369,6 +395,11 @@ function _optimize(
args...;
kwargs...
)
# Throw an error if we received a second-order optimizer.
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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 only_fgh!(f) and implementing f(F, G, H, x) that contains the check

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).

Copy link

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link

Choose a reason for hiding this comment

The 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 😬

Copy link

Choose a reason for hiding this comment

The 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.

Copy link

@pkofod pkofod Aug 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Project.toml Outdated
@@ -1,6 +1,6 @@
name = "Turing"
uuid = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
version = "0.13.0"
version = "0.13.1"
Copy link
Member

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).

Copy link
Member Author

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.

Copy link
Contributor

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 and using Turing will fail. I think maybe you guys want to bump up the version really soon...

Copy link
Member

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?

Copy link
Contributor

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 and using Turing somehow fails.

Copy link
Member

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.

@@ -147,6 +147,50 @@ function (f::OptimLogDensity)(z)
return -DynamicPPL.getlogp(varinfo)
end

function (f::OptimLogDensity)(F, G, z)
Copy link
Member

Choose a reason for hiding this comment

The 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 f(F, G, H, z), so the implementation could just be included there directly.

@wupeifan
Copy link
Contributor

It seems that there is nothing blocking this PR, I think?

@devmotion devmotion closed this Aug 19, 2020
@devmotion devmotion reopened this Aug 19, 2020
@devmotion
Copy link
Member

Tests fail currently.

@cpfiffer
Copy link
Member Author

I'll take a look when I'm done teaching today, looks like I've done something strange to the methods.

@codecov
Copy link

codecov bot commented Aug 20, 2020

Codecov Report

Merging #1369 into master will increase coverage by 0.16%.
The diff coverage is 88.88%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1369      +/-   ##
==========================================
+ Coverage   66.79%   66.95%   +0.16%     
==========================================
  Files          25       25              
  Lines        1605     1619      +14     
==========================================
+ Hits         1072     1084      +12     
- Misses        533      535       +2     
Impacted Files Coverage Δ
src/core/compat/reversediff.jl 90.47% <ø> (ø)
src/core/compat/zygote.jl 100.00% <ø> (ø)
src/modes/ModeEstimation.jl 66.95% <85.71%> (+2.25%) ⬆️
src/core/ad.jl 74.24% <100.00%> (+0.39%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 43e2f20...fa7c59f. Read the comment docs.

Copy link
Member

@devmotion devmotion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@cpfiffer cpfiffer merged commit f2f6665 into master Aug 20, 2020
@devmotion devmotion deleted the csp/explicit-gradient branch August 20, 2020 14:02
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

Successfully merging this pull request may close these issues.

4 participants