-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Improve tuning by skipping the first samples + add new experimental tuning method #5004
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
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
97033ab
Fix issue in hmc gradient storage
aseyboldt 71db94d
Skip first samples during NUTS adaptation
aseyboldt 0076325
Add test and doc for jitter+adapt_diag_grad
aseyboldt a4121bc
Improve tests of init methods
aseyboldt 2b39903
Add new tuning method to release notes
aseyboldt ad45bd8
Fix quadpotential for short tuning periods
aseyboldt 9933091
Fix release notes
aseyboldt 6263f99
Remove old gradient mass matrix adaptation
aseyboldt d2684c0
Change default for stop_adaptation
aseyboldt ad777b1
Add docstrings for quadpotential
aseyboldt 2f9e5d1
Fix tests for deleted init methods
aseyboldt b0b56ce
Remove weight argument in quadpotential add_sample
aseyboldt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -154,6 +154,10 @@ def __init__( | |||||
adaptation_window=101, | ||||||
adaptation_window_multiplier=1, | ||||||
dtype=None, | ||||||
discard_window=50, | ||||||
aseyboldt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
initial_weights=None, | ||||||
early_update=False, | ||||||
store_mass_matrix_trace=False, | ||||||
): | ||||||
"""Set up a diagonal mass matrix.""" | ||||||
if initial_diag is not None and initial_diag.ndim != 1: | ||||||
|
@@ -175,12 +179,20 @@ def __init__( | |||||
self.dtype = dtype | ||||||
self._n = n | ||||||
|
||||||
self._discard_window = discard_window | ||||||
self._early_update = early_update | ||||||
|
||||||
self._initial_mean = initial_mean | ||||||
self._initial_diag = initial_diag | ||||||
self._initial_weight = initial_weight | ||||||
self.adaptation_window = adaptation_window | ||||||
self.adaptation_window_multiplier = float(adaptation_window_multiplier) | ||||||
|
||||||
self._store_mass_matrix_trace = store_mass_matrix_trace | ||||||
self._mass_trace = [] | ||||||
|
||||||
self._initial_weights = initial_weights | ||||||
|
||||||
self.reset() | ||||||
|
||||||
def reset(self): | ||||||
|
@@ -222,12 +234,18 @@ def _update_from_weightvar(self, weightvar): | |||||
|
||||||
def update(self, sample, grad, tune): | ||||||
"""Inform the potential about a new sample during tuning.""" | ||||||
if self._store_mass_matrix_trace: | ||||||
self._mass_trace.append(self._stds.copy()) | ||||||
ricardoV94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
if not tune: | ||||||
return | ||||||
|
||||||
self._foreground_var.add_sample(sample, weight=1) | ||||||
self._background_var.add_sample(sample, weight=1) | ||||||
self._update_from_weightvar(self._foreground_var) | ||||||
if self._n_samples > self._discard_window: | ||||||
self._foreground_var.add_sample(sample, weight=1) | ||||||
self._background_var.add_sample(sample, weight=1) | ||||||
|
||||||
if self._early_update or self._n_samples > self.adaptation_window: | ||||||
self._update_from_weightvar(self._foreground_var) | ||||||
|
||||||
if self._n_samples > 0 and self._n_samples % self.adaptation_window == 0: | ||||||
self._foreground_var = self._background_var | ||||||
|
@@ -342,6 +360,8 @@ def __init__( | |||||
|
||||||
def add_sample(self, x, weight): | ||||||
x = np.asarray(x) | ||||||
if weight != 1: | ||||||
raise ValueError("weight is unused and broken") | ||||||
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.
Suggested change
Or maybe we should just remove it all-together. 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. done |
||||||
self.n_samples += 1 | ||||||
old_diff = x - self.mean | ||||||
self.mean[:] += old_diff / self.n_samples | ||||||
|
@@ -360,6 +380,83 @@ def current_mean(self): | |||||
return self.mean.copy(dtype=self._dtype) | ||||||
|
||||||
|
||||||
class _ExpWeightedVariance: | ||||||
def __init__(self, n_vars, *, init_mean, init_var, alpha): | ||||||
self._variance = init_var | ||||||
self._mean = init_mean | ||||||
self._alpha = alpha | ||||||
|
||||||
def add_sample(self, value): | ||||||
alpha = self._alpha | ||||||
delta = value - self._mean | ||||||
self._mean += alpha * delta | ||||||
self._variance = (1 - alpha) * (self._variance + alpha * delta ** 2) | ||||||
|
||||||
def current_variance(self, out=None): | ||||||
if out is None: | ||||||
out = np.empty_like(self._variance) | ||||||
np.copyto(out, self._variance) | ||||||
return out | ||||||
|
||||||
def current_mean(self, out=None): | ||||||
if out is None: | ||||||
out = np.empty_like(self._mean) | ||||||
np.copyto(out, self._mean) | ||||||
return out | ||||||
|
||||||
|
||||||
class QuadPotentialDiagAdaptExp(QuadPotentialDiagAdapt): | ||||||
def __init__(self, *args, alpha, use_grads=False, stop_adaptation=None, **kwargs): | ||||||
super().__init__(*args, **kwargs) | ||||||
self._alpha = alpha | ||||||
self._use_grads = use_grads | ||||||
self._stop_adaptation = stop_adaptation | ||||||
|
||||||
def update(self, sample, grad, tune): | ||||||
if tune and self._n_samples < self._stop_adaptation: | ||||||
if self._n_samples > self._discard_window: | ||||||
self._variance_estimator.add_sample(sample) | ||||||
if self._use_grads: | ||||||
self._variance_estimator_grad.add_sample(grad) | ||||||
elif self._n_samples == self._discard_window: | ||||||
self._variance_estimator = _ExpWeightedVariance( | ||||||
self._n, | ||||||
init_mean=sample.copy(), | ||||||
init_var=np.zeros_like(sample), | ||||||
alpha=self._alpha, | ||||||
) | ||||||
if self._use_grads: | ||||||
self._variance_estimator_grad = _ExpWeightedVariance( | ||||||
self._n, | ||||||
init_mean=grad.copy(), | ||||||
init_var=np.zeros_like(grad), | ||||||
alpha=self._alpha, | ||||||
) | ||||||
|
||||||
if self._n_samples > 2 * self._discard_window: | ||||||
if self._use_grads: | ||||||
self._update_from_variances( | ||||||
self._variance_estimator, self._variance_estimator_grad | ||||||
) | ||||||
else: | ||||||
self._update_from_weightvar(self._variance_estimator) | ||||||
|
||||||
self._n_samples += 1 | ||||||
|
||||||
if self._store_mass_matrix_trace: | ||||||
self._mass_trace.append(self._stds.copy()) | ||||||
|
||||||
def _update_from_variances(self, var_estimator, inv_var_estimator): | ||||||
var = var_estimator.current_variance() | ||||||
inv_var = inv_var_estimator.current_variance() | ||||||
# print(inv_var) | ||||||
updated = np.sqrt(var / inv_var) | ||||||
self._var[:] = updated | ||||||
# updated = np.exp((np.log(var) - np.log(inv_var)) / 2) | ||||||
np.sqrt(updated, out=self._stds) | ||||||
np.divide(1, self._stds, out=self._inv_stds) | ||||||
|
||||||
|
||||||
class QuadPotentialDiag(QuadPotential): | ||||||
"""Quad potential using a diagonal covariance matrix.""" | ||||||
|
||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.