Skip to content

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

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

Merged
merged 1 commit into from
Apr 16, 2025

Conversation

velochy
Copy link
Contributor

@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
Contributor 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
Contributor 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
Contributor 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?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for that @zaxtax
Updated the code. Also added a test to check if initvals are used correctly.

@velochy
Copy link
Contributor 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
Contributor 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
Contributor 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

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

@velochy
Copy link
Contributor Author

velochy commented Apr 14, 2025

Is there anything I should still do?
Tests are failing for things unrelated to this PR to the best of my understanding, and I have addressed all of the comments

@zaxtax
Copy link
Contributor

zaxtax commented Apr 14, 2025 via email

@velochy velochy force-pushed the FitCompatibility branch 3 times, most recently from f53445a to b81684d Compare April 14, 2025 16:51
"identity" : Applies log importance weights directly without resampling.
None : No importance sampling weights. Returns raw samples of size (num_paths, num_draws_per_path, N) where N is number of model parameters. Other methods return samples of size (num_draws, N).

- **"psis"** : Pareto Smoothed Importance Sampling (default). Usually most stable.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you remove these ** characters? They aren't consistent the style used elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@zaxtax
Copy link
Contributor

zaxtax commented Apr 14, 2025

Can you also move the import of jax into the relevant functions in the pathfinder file?

@zaxtax
Copy link
Contributor

zaxtax commented Apr 15, 2025

I'm not sure why the build source distribution test is still unhappy. Can you investigate why importing jax still happens?

…ue for pathfinder and cleaned relevant docs a bit
@velochy
Copy link
Contributor Author

velochy commented Apr 15, 2025

I'm not sure why the build source distribution test is still unhappy. Can you investigate why importing jax still happens?

I think it was because of the import from pymc.sampling.jax. Moved that down as well. Hopefully tests will pass now

@zaxtax zaxtax self-requested a review April 16, 2025 03:43
@velochy
Copy link
Contributor Author

velochy commented Apr 16, 2025

What is still needed to get it merged? Review from @jessegrabowski ?

@zaxtax
Copy link
Contributor

zaxtax commented Apr 16, 2025 via email

@zaxtax zaxtax merged commit 53df690 into pymc-devs:main Apr 16, 2025
5 checks passed
@zaxtax
Copy link
Contributor

zaxtax commented Apr 16, 2025

@velochy now let's get #444 merged in too 😄

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