-
Notifications
You must be signed in to change notification settings - Fork 127
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
Add gradient calculation for the covariance between points in GPyModelWrapper #347
Open
BrunoKM
wants to merge
19
commits into
EmuKit:main
Choose a base branch
from
BrunoKM:gradients-for-covariance-gpy
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
19 commits
Select commit
Hold shift + click to select a range
49f5389
Add covariance between points gradient and vectorize covariance gradi…
BrunoKM d0e25c8
Add tests for the gradients
BrunoKM bda97ad
Fix shapes in gradient tests
BrunoKM 6738faa
Merge branch 'master' into gradients-for-covariance-gpy
apaleyes 6ed9094
Rewrite the covariance gradient calculation code
BrunoKM e7b436b
Add covariance between points gradient and vectorize covariance gradi…
BrunoKM 8e16990
Add tests for the gradients
BrunoKM 38a0691
Fix shapes in gradient tests
BrunoKM d6d6b0f
Rewrite the covariance gradient calculation code
BrunoKM 5bd6383
Merge branch 'gradients-for-covariance-gpy' of github.com:BrunoKM/emu…
BrunoKM d34de42
Merge branch 'main' into gradients-for-covariance-gpy
apaleyes fe9b0d7
Merge branch 'main' into gradients-for-covariance-gpy
BrunoKM bcf4416
Fix typos and remove redundant args in doc-strings
BrunoKM 26960ee
Fix typo in emukit/model_wrappers/gpy_model_wrappers.py
BrunoKM 2bb36d2
Rename variable names to be more informative and verbose in dSigma()
BrunoKM 9d52426
Add futher documentation to gradients of covariance calculations
BrunoKM c8b9f83
Add an interface for differentiable cross-covariance models
BrunoKM f345342
Incorporate interface into GPyModel
BrunoKM c280f6d
Merge branch 'gradients-for-covariance-gpy' of github.com:BrunoKM/emu…
BrunoKM 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 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 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 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 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,53 @@ | ||
import GPy | ||
import numpy as np | ||
import pytest | ||
|
||
from emukit.model_wrappers.gpy_model_wrappers import GPyModelWrapper | ||
|
||
|
||
@pytest.fixture | ||
def test_data(gpy_model): | ||
np.random.seed(42) | ||
return np.random.randn(5, gpy_model.X.shape[1]) | ||
|
||
|
||
@pytest.fixture | ||
def test_data2(gpy_model): | ||
np.random.seed(42) | ||
return np.random.randn(4, gpy_model.X.shape[1]) | ||
|
||
|
||
def test_joint_prediction_gradients(gpy_model, test_data): | ||
epsilon = 1e-5 | ||
mean, cov = gpy_model.predict_with_full_covariance(test_data) | ||
# Get the gradients | ||
mean_dx, cov_dx = gpy_model.get_joint_prediction_gradients(test_data) | ||
|
||
for i in range(test_data.shape[0]): # Iterate over each test point | ||
for j in range(test_data.shape[1]): # Iterate over each dimension | ||
# Approximate the gradient numerically | ||
perturbed_input = test_data.copy() | ||
perturbed_input[i, j] += epsilon | ||
mean_perturbed, cov_perturbed = gpy_model.predict_with_full_covariance(perturbed_input) | ||
mean_dx_numerical = (mean_perturbed - mean) / epsilon | ||
cov_dx_numerical = (cov_perturbed - cov) / epsilon | ||
# Check that numerical approx. similar to true gradient | ||
assert pytest.approx(mean_dx_numerical.ravel(), abs=1e-8, rel=1e-2) == mean_dx[:, i, j] | ||
assert pytest.approx(cov_dx_numerical, abs=1e-8, rel=1e-2) == cov_dx[:, :, i, j] | ||
|
||
|
||
def test_get_covariance_between_points_gradients(gpy_model, test_data, test_data2): | ||
epsilon = 1e-5 | ||
cov = gpy_model.get_covariance_between_points(test_data, test_data2) | ||
# Get the gradients | ||
cov_dx = gpy_model.get_covariance_between_points_gradients(test_data, test_data2) | ||
|
||
for i in range(test_data.shape[0]): # Iterate over each test point | ||
for j in range(test_data.shape[1]): # Iterate over each dimension | ||
# Approximate the gradient numerically | ||
perturbed_input = test_data.copy() | ||
perturbed_input[i, j] += epsilon | ||
cov_perturbed = gpy_model.get_covariance_between_points(perturbed_input, test_data2) | ||
cov_dx_numerical = (cov_perturbed[i] - cov[i]) / epsilon | ||
# Check that numerical approx. similar to true gradient | ||
assert pytest.approx(cov_dx_numerical, abs=1e-8, rel=1e-2) == cov_dx[i, :, j] |
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.
We already have an interface that defines nearly the same method: https://github.com/EmuKit/emukit/blob/main/emukit/bayesian_optimization/interfaces/models.py
While this isn't that big of an issue, would be nice if we could separate them somehow. Few ideas:
Pick whichever you prefer!