-
Notifications
You must be signed in to change notification settings - Fork 247
Add .bind() method to potentials #1943
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 all commits
89bc9a2
3e52f56
30459d9
8fdc10c
968b58b
84c4731
ee2adfa
b98bd84
a6284bc
d345ffb
a0cebbf
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 |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |
| # under the Apache License Version 2.0, see <https://www.apache.org/licenses/> | ||
| import inspect | ||
| import warnings | ||
| from copy import deepcopy | ||
| from functools import partial | ||
| from math import ceil | ||
| from typing import Any, Callable, Dict, Literal, Optional, Union | ||
|
|
@@ -237,7 +236,7 @@ def log_prob( | |
| warn("The log-probability is unnormalized!", stacklevel=2) | ||
|
|
||
| x = self._x_else_default_x(x) | ||
| self.potential_fn.set_x(x, x_is_iid=True) | ||
| self.potential_fn = self.potential_fn.bind(x, x_is_iid=True) | ||
|
|
||
| theta = ensure_theta_batched(torch.as_tensor(theta)) | ||
| return self.potential_fn( | ||
|
|
@@ -291,7 +290,7 @@ def sample( | |
| """ | ||
|
|
||
| x = self._x_else_default_x(x) | ||
| self.potential_fn.set_x(x, x_is_iid=True) | ||
| self.potential_fn = self.potential_fn.bind(x, x_is_iid=True) | ||
|
|
||
| # Replace arguments that were not passed with their default. | ||
| method = self.method if method is None else method | ||
|
|
@@ -451,7 +450,7 @@ def sample_batched( | |
| # in the order of the observations. | ||
| x_ = x.repeat_interleave(num_chains, dim=0) | ||
|
|
||
| self.potential_fn.set_x(x_, x_is_iid=False) | ||
| self.potential_fn = self.potential_fn.bind(x_, x_is_iid=False) | ||
| self.potential_ = self._prepare_potential(method) # type: ignore | ||
|
|
||
| # For each observation in the batch, we have num_chains independent chains. | ||
|
|
@@ -662,25 +661,25 @@ def _get_initial_params_batched( | |
| Tensor: initial parameters, one for each chain | ||
| """ | ||
|
|
||
| potential_ = deepcopy(self.potential_fn) | ||
|
Collaborator
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 understand why a deepcopy was necessary here and why we can replace it it with an assignment to self?
Contributor
Author
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. Jan told me this on discord:
|
||
| potential_ = self.potential_fn | ||
| initial_params = [] | ||
| init_fn = self._build_mcmc_init_fn( | ||
| self.proposal, | ||
| potential_fn=potential_, | ||
| transform=self.theta_transform, | ||
| init_strategy=init_strategy, # type: ignore | ||
| **kwargs, | ||
| ) | ||
| for xi in x: | ||
| # Build init function | ||
| potential_.set_x(xi) | ||
| # Build init function with bound potential for this specific xi | ||
| potential_bound = potential_.bind(xi) | ||
| init_fn = self._build_mcmc_init_fn( | ||
| self.proposal, | ||
| potential_fn=potential_bound, | ||
| transform=self.theta_transform, | ||
| init_strategy=init_strategy, # type: ignore | ||
| **kwargs, | ||
| ) | ||
|
Comment on lines
+669
to
+675
Collaborator
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 understand the implications of moving that inside the loop? What is happening there and is it fine to move it inside the loop?
Contributor
Author
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. Here is how I understand it:
|
||
|
|
||
| # Parallelize inits for resampling or sir. | ||
| if num_workers > 1 and ( | ||
| init_strategy == "resample" or init_strategy == "sir" | ||
| ): | ||
|
|
||
| def seeded_init_fn(seed): | ||
| def seeded_init_fn(seed, init_fn=init_fn): | ||
| torch.manual_seed(seed) | ||
| return init_fn() | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.