-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from utkarshp1161/main
- Loading branch information
Showing
7 changed files
with
660 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,3 +127,5 @@ dmypy.json | |
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
*tar.gz |
Large diffs are not rendered by default.
Oops, something went wrong.
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,28 @@ | ||
import pyro | ||
import pyro.contrib.gp as gp | ||
import torch | ||
from tqdm import tqdm | ||
def gp_model(input_dim: int = None, encoded_X: torch.Tensor = None, y: torch.Tensor = None, gp_iterations: int = 1): | ||
""" | ||
Returns a GP model trained on the encoded data. | ||
Args: | ||
input_dim: Dimensionality of the input data. | ||
encoded_X: Encoded data. | ||
y: Target data. | ||
Returns: | ||
gpr: GP regression model. | ||
""" | ||
# Define and train the GP model | ||
print("Training GP model...") | ||
kernel = gp.kernels.RBF(input_dim=encoded_X.shape[1]) | ||
gpr = gp.models.GPRegression(encoded_X, y, kernel) | ||
optimizer = torch.optim.Adam(gpr.parameters(), lr=0.005) | ||
loss_fn = pyro.infer.Trace_ELBO().differentiable_loss | ||
loss = loss_fn(gpr.model, gpr.guide) | ||
for _ in tqdm(range(gp_iterations)): | ||
optimizer.zero_grad() | ||
loss.backward() | ||
optimizer.step() | ||
print("GP model trained.") | ||
|
||
return gpr |
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,16 @@ | ||
import pytest | ||
import torch | ||
import pyro | ||
import pyro.contrib.gp as gp | ||
from pyroved.utils import gp_model | ||
from pyroved import models | ||
|
||
def test_gp_model_output_shape(): | ||
input_dim = 3 | ||
num_samples = 5 | ||
encoded_X = torch.randn(num_samples, input_dim) # Random tensor for encoded_X | ||
y = torch.randn(num_samples) # Random tensor for y | ||
gpr = gp_model(input_dim, encoded_X, y) | ||
with torch.no_grad(): | ||
predictions, _ = gpr(encoded_X) | ||
assert predictions.shape == y.shape, "Output tensor shape mismatch" |