-
Notifications
You must be signed in to change notification settings - Fork 458
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
37 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 ef43cc1
Cleaned up code
eibarolle acc35e8
Codecov Test Cases
eibarolle 294a107
LIG Test Correct Placement
eibarolle 6d0de3a
Deleting Broken File
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
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,95 @@ | ||
| #!/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 | ||
|
|
||
| import warnings | ||
| from typing import Optional | ||
|
|
||
| import torch | ||
| from botorch import settings | ||
| from botorch_community.models.np_regression import NeuralProcessModel | ||
| from torch import Tensor | ||
|
|
||
| import torch | ||
| #reference: https://arxiv.org/abs/2106.02770 | ||
|
|
||
| class LatentInformationGain: | ||
| def __init__( | ||
| self, | ||
| model: NeuralProcessModel, | ||
| num_samples: int = 10, | ||
| min_std: float = 0.1, | ||
| scaler: float = 0.9 | ||
| ) -> None: | ||
| """ | ||
| Latent Information Gain (LIG) Acquisition Function, designed for the | ||
| NeuralProcessModel. | ||
|
|
||
| Args: | ||
| model: Trained NeuralProcessModel. | ||
| num_samples (int): Number of samples for calculation, defaults to 10. | ||
| min_std: Float representing the minimum possible standardized std, defaults to 0.1. | ||
| scaler: Float scaling the std, defaults to 0.9. | ||
| """ | ||
| self.model = model | ||
| self.num_samples = num_samples | ||
| self.min_std = min_std | ||
| self.scaler = scaler | ||
|
|
||
| def acquisition(self, candidate_x, context_x, context_y): | ||
| """ | ||
| Conduct the Latent Information Gain acquisition function for the inputs. | ||
|
|
||
| Args: | ||
| candidate_x: Candidate input points, as a Tensor. | ||
| context_x: Context input points, as a Tensor. | ||
| context_y: Context target points, as a Tensor. | ||
|
|
||
| Returns: | ||
| torch.Tensor: The LIG score of computed KLDs. | ||
| """ | ||
|
|
||
| # Encoding and Scaling the context data | ||
| z_mu_context, z_logvar_context = self.model.data_to_z_params(context_x, context_y) | ||
| kl = 0.0 | ||
| for _ in range(self.num_samples): | ||
| # Taking reparameterized samples | ||
| samples = self.model.sample_z(z_mu_context, z_logvar_context) | ||
|
|
||
| # Using the Decoder to take predicted values | ||
| y_pred = self.model.decoder(candidate_x, samples) | ||
|
|
||
| # Combining context and candidate data | ||
| combined_x = torch.cat([context_x, candidate_x], dim=0) | ||
| combined_y = torch.cat([context_y, y_pred], dim=0) | ||
|
|
||
| # Computing posterior variables | ||
| z_mu_posterior, z_logvar_posterior = self.model.data_to_z_params(combined_x, combined_y) | ||
| std_prior = self.min_std + self.scaler * torch.sigmoid(z_logvar_context) | ||
| std_posterior = self.min_std + self.scaler * torch.sigmoid(z_logvar_posterior) | ||
|
|
||
| p = torch.distributions.Normal(z_mu_posterior, std_posterior) | ||
| q = torch.distributions.Normal(z_mu_context, std_prior) | ||
|
|
||
| kl_divergence = torch.distributions.kl_divergence(p, q).sum() | ||
| kl += kl_divergence | ||
|
|
||
| # Average KLD | ||
| return kl / self.num_samples | ||
Oops, something went wrong.
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 we implement this as a subclass of
Acquisition, so that we can use it more organically in botorch? Likely the context would be needed to be provided inLatentInformationGain.__init__