|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | +# All rights reserved. |
| 4 | + |
| 5 | +# This source code is licensed under the license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +import torch |
| 9 | +from aepsych import Config, SequentialStrategy |
| 10 | +from aepsych.models.derivative_gp import MixedDerivativeVariationalGP |
| 11 | +from botorch.fit import fit_gpytorch_mll |
| 12 | +from botorch.utils.testing import BotorchTestCase |
| 13 | +from gpytorch.likelihoods import BernoulliLikelihood |
| 14 | +from gpytorch.mlls.variational_elbo import VariationalELBO |
| 15 | + |
| 16 | + |
| 17 | +class TestDerivativeGP(BotorchTestCase): |
| 18 | + def test_MixedDerivativeVariationalGP_gpu(self): |
| 19 | + train_x = torch.cat( |
| 20 | + (torch.tensor([1.0, 2.0, 3.0, 4.0]).unsqueeze(1), torch.zeros(4, 1)), dim=1 |
| 21 | + ) |
| 22 | + train_y = torch.tensor([1.0, 2.0, 3.0, 4.0]) |
| 23 | + m = MixedDerivativeVariationalGP( |
| 24 | + train_x=train_x, |
| 25 | + train_y=train_y, |
| 26 | + inducing_points=train_x, |
| 27 | + fixed_prior_mean=0.5, |
| 28 | + ).cuda() |
| 29 | + |
| 30 | + self.assertEqual(m.mean_module.constant.item(), 0.5) |
| 31 | + self.assertEqual( |
| 32 | + m.covar_module.base_kernel.raw_lengthscale.shape, torch.Size([1, 1]) |
| 33 | + ) |
| 34 | + mll = VariationalELBO( |
| 35 | + likelihood=BernoulliLikelihood(), model=m, num_data=train_y.numel() |
| 36 | + ).cuda() |
| 37 | + mll = fit_gpytorch_mll(mll) |
| 38 | + test_x = torch.tensor([[1.0, 0], [3.0, 1.0]]).cuda() |
| 39 | + m(test_x) |
0 commit comments