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

Make fit_pathfinder more similar to fit_laplace and pm.sample #447

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

velochy
Copy link

@velochy velochy commented Apr 1, 2025

  • Add initvals to parameters
  • Add observations and constants to return value (idata)
  • Add both fit_laplace and fit_pathfinder to docs and make fit link to both
  • Clean up init.py include structure

This arose out of https://discourse.pymc.io/t/pymc-extras-fit-compatibility-with-pm-sample/16763/5

@zaxtax
Copy link
Contributor

zaxtax commented Apr 8, 2025

This looks alright to me. Thoughts @ricardoV94 ?

@zaxtax zaxtax requested a review from jessegrabowski April 8, 2025 09:15
@fonnesbeck
Copy link
Member

CC @aphc14

from pymc_extras.inference.find_map import find_MAP
from pymc_extras.inference.fit import fit
from pymc_extras.inference.laplace import fit_laplace
from pymc_extras.inference import *
Copy link
Member

Choose a reason for hiding this comment

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

Can we use explicit imports?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, of course. I'll fix that when I have a few minutes

Copy link
Author

Choose a reason for hiding this comment

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

done

The inference data containing the results of the Pathfinder algorithm.

References
----------
Zhang, L., Carpenter, B., Gelman, A., & Vehtari, A. (2022). Pathfinder: Parallel quasi-Newton variational inference. Journal of Machine Learning Research, 23(306), 1-49.
"""

if initvals is not None:
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't mutate the model, make a copy perhaps if there's no better way to just forward the initvals

Copy link
Author

Choose a reason for hiding this comment

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

I would normally agree. However, I tried it, but model.copy() does not produce a working model sometimes - most notably when any transformations are used.

Should I use some other copy function?

@velochy
Copy link
Author

velochy commented Apr 9, 2025

@ricardoV94 it currently also fails a test on including blackjax, which is currently included in pathfinder.py
How would you like to handle it? Do I bring the include lower down so it is only included when used, or do I add it to the requirements of the package?

@aphc14
Copy link
Contributor

aphc14 commented Apr 9, 2025

@ricardoV94 it currently also fails a test on including blackjax, which is currently included in pathfinder.py How would you like to handle it? Do I bring the include lower down so it is only included when used, or do I add it to the requirements of the package?

Yup, that is a good idea. closes #443

The inference data containing the results of the Pathfinder algorithm.

References
----------
Zhang, L., Carpenter, B., Gelman, A., & Vehtari, A. (2022). Pathfinder: Parallel quasi-Newton variational inference. Journal of Machine Learning Research, 23(306), 1-49.
"""

if initvals is not None:
for rv_name, ivals in initvals.items():
model.set_initval(model.named_vars[rv_name], ivals)
Copy link
Contributor

Choose a reason for hiding this comment

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

Might need to ensure that ivals is a support point for the RV. For example, x ~ Uniform(-1, 1) would have nan initial values with model.set_initval(model.named_vars["x"], 2)

Copy link
Author

Choose a reason for hiding this comment

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

While in the ideal world, I would agree, in practice
a) It is very nontrivial to do as I understand, as the limits are not specified anywhere where they are easy to take
b) pm.sample does no such checks, and the goal of this PR is to be compatible with that

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup, seems fair enough. Thanks for this submission @velochy

@zaxtax
Copy link
Contributor

zaxtax commented Apr 9, 2025 via email

@velochy
Copy link
Author

velochy commented Apr 9, 2025

Yup, that is a good idea. closes #443

Considering you have a standing PR on this, does it not make more sense to just have your quickfix merged separately?
I don't think it conflicts with anything I've done here

@zaxtax
Copy link
Contributor

zaxtax commented Apr 9, 2025 via email

…ue for pathfinder and cleaned relevant docs a bit
@velochy velochy force-pushed the FitCompatibility branch from 1ee9bcf to 370ffe0 Compare April 9, 2025 16:13
@aphc14
Copy link
Contributor

aphc14 commented Apr 10, 2025

The latest test failure is due to pathfinder using pt.batched_dot operations. PR #453 should solve this.

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.

5 participants