-
Notifications
You must be signed in to change notification settings - Fork 423
NP Regression Model w/ LIG Acquisition #2683
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
Open
eibarolle
wants to merge
33
commits into
pytorch:main
Choose a base branch
from
eibarolle:np_regression
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
9304881
Latent Information Gain
eibarolle 7a8e4ab
NP Regression
eibarolle 5f0dba0
Test NP Regression
eibarolle c55d7a9
Test Latent Information Gain
eibarolle 657151f
NP Regression Documentation
eibarolle 4f35e0f
1/25 Updates
eibarolle 280776d
1/25 Updates
eibarolle a811429
1/25 Updates
eibarolle 50fe7a1
1/25 Updates
eibarolle 9684e1d
Merge branch 'main' into np_regression
eibarolle 4aeeeeb
Update Acquisition Dimensions
eibarolle be34f60
Updated Test Files
eibarolle 204ba31
Updated LIG Parameters/Generalizability
eibarolle 8e33fc4
Updated NPR Compatability
eibarolle 7232725
Updated NPR Parameters
eibarolle d78d262
LIG WIP
eibarolle 32916b9
Updated Tests
eibarolle e13f38c
Test LIG WIP
eibarolle bf95d41
LIG Updated Parameters
eibarolle 046f609
NPR Updated Parameters
eibarolle 4c037c5
LIG Updated Tests
eibarolle e7c964d
NPR Updated Tests
eibarolle 96d2bb4
Update Parameters
eibarolle c4f5f87
Update Parameters
eibarolle 529e36c
Update Parameters
eibarolle 0e28077
April Updates
eibarolle 0ceb9ca
April Updates
eibarolle 9949ff9
April Tests
eibarolle 2c9c958
April Tests
eibarolle 918a4b4
5/16 Updates
eibarolle fe75f43
Recent Fixes
eibarolle badba20
Fixing merge conflicts for acquisition/models docs
eibarolle 43d8c32
Merge branch 'main' into np_regression
eibarolle 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
117 changes: 117 additions & 0 deletions
117
botorch_community/acquisition/latent_information_gain.py
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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
r""" | ||
Latent Information Gain Acquisition Function for Neural Process Models. | ||
|
||
References: | ||
|
||
.. [Wu2023arxiv] | ||
Wu, D., Niu, R., Chinazzi, M., Vespignani, A., Ma, Y.-A., & Yu, R. (2023). | ||
Deep Bayesian Active Learning for Accelerating Stochastic Simulation. | ||
arXiv preprint arXiv:2106.02770. Retrieved from https://arxiv.org/abs/2106.02770 | ||
|
||
Contributor: eibarolle | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Any, Type | ||
|
||
import torch | ||
from botorch.acquisition import AcquisitionFunction | ||
from botorch_community.models.np_regression import NeuralProcessModel | ||
from torch import Tensor | ||
# reference: https://arxiv.org/abs/2106.02770 | ||
|
||
|
||
class LatentInformationGain(AcquisitionFunction): | ||
def __init__( | ||
self, | ||
model: Type[Any], | ||
num_samples: int = 10, | ||
min_std: float = 0.01, | ||
scaler: float = 0.5, | ||
) -> None: | ||
""" | ||
Latent Information Gain (LIG) Acquisition Function. | ||
Uses the model's built-in posterior function to generalize KL computation. | ||
|
||
Args: | ||
model: The model class to be used, defaults to NeuralProcessModel. | ||
num_samples: Int showing the # of samples for calculation, defaults to 10. | ||
min_std: Float representing the minimum possible standardized std, | ||
defaults to 0.01. | ||
scaler: Float scaling the std, defaults to 0.5. | ||
""" | ||
super().__init__(model) | ||
self.model = model | ||
self.num_samples = num_samples | ||
self.min_std = min_std | ||
self.scaler = scaler | ||
|
||
def forward(self, candidate_x: Tensor) -> Tensor: | ||
""" | ||
Conduct the Latent Information Gain acquisition function for the inputs. | ||
|
||
Args: | ||
candidate_x: Candidate input points, as a Tensor. Ideally in the shape | ||
(N, q, D). | ||
|
||
Returns: | ||
torch.Tensor: The LIG scores of computed KLDs, in the shape (N, q). | ||
""" | ||
device = candidate_x.device | ||
candidate_x = candidate_x.to(device) | ||
N, q, D = candidate_x.shape | ||
kl = torch.zeros(N, device=device, dtype=torch.float32) | ||
|
||
if isinstance(self.model, NeuralProcessModel): | ||
x_c, y_c, _, _ = self.model.random_split_context_target( | ||
self.model.train_X, self.model.train_Y, self.model.n_context | ||
) | ||
self.model.z_mu_context, self.model.z_logvar_context = ( | ||
self.model.data_to_z_params(x_c, y_c) | ||
) | ||
|
||
for i in range(N): | ||
x_i = candidate_x[i] | ||
kl_i = 0.0 | ||
|
||
for _ in range(self.num_samples): | ||
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. Same with this for loop |
||
sample_z = self.model.sample_z( | ||
self.model.z_mu_context, self.model.z_logvar_context | ||
) | ||
if sample_z.dim() == 1: | ||
sample_z = sample_z.unsqueeze(0) | ||
|
||
y_pred = self.model.decoder(x_i, sample_z) | ||
|
||
combined_x = torch.cat([x_c, x_i], dim=0) | ||
combined_y = torch.cat([y_c, y_pred], dim=0) | ||
|
||
self.model.z_mu_all, self.model.z_logvar_all = ( | ||
self.model.data_to_z_params(combined_x, combined_y) | ||
) | ||
kl_sample = self.model.KLD_gaussian(self.min_std, self.scaler) | ||
kl_i += kl_sample | ||
|
||
kl[i] = kl_i / self.num_samples | ||
|
||
else: | ||
for i in range(N): | ||
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. Could we replace these loops with batched computation too? |
||
x_i = candidate_x[i] | ||
kl_i = 0.0 | ||
for _ in range(self.num_samples): | ||
posterior_prior = self.model.posterior(self.model.train_X) | ||
posterior_candidate = self.model.posterior(x_i) | ||
|
||
kl_i += torch.distributions.kl_divergence( | ||
posterior_candidate.mvn, posterior_prior.mvn | ||
).sum() | ||
|
||
kl[i] = kl_i / self.num_samples | ||
return kl |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this for loop be eliminated using batched computation?