|
| 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 os |
| 9 | + |
| 10 | +import torch |
| 11 | + |
| 12 | +# run on single threads to keep us from deadlocking weirdly in CI |
| 13 | +if "CI" in os.environ or "SANDCASTLE" in os.environ: |
| 14 | + torch.set_num_threads(1) |
| 15 | + |
| 16 | +import numpy as np |
| 17 | +from aepsych import Config |
| 18 | +from aepsych.acquisition.monotonic_rejection import MonotonicMCLSE |
| 19 | +from aepsych.acquisition.objective import ProbitObjective |
| 20 | +from aepsych.generators import MonotonicRejectionGenerator |
| 21 | +from aepsych.models import MonotonicRejectionGP |
| 22 | +from aepsych.strategy import SequentialStrategy, Strategy |
| 23 | +from botorch.acquisition.objective import IdentityMCObjective |
| 24 | +from botorch.utils.testing import BotorchTestCase |
| 25 | +from gpytorch.likelihoods import BernoulliLikelihood, GaussianLikelihood |
| 26 | +from scipy.stats import norm |
| 27 | + |
| 28 | + |
| 29 | +class MonotonicRejectionGPLSETest(BotorchTestCase): |
| 30 | + def test_regression_gpu(self): |
| 31 | + # Init |
| 32 | + target = 1.5 |
| 33 | + model_gen_options = {"num_restarts": 1, "raw_samples": 3, "epochs": 5} |
| 34 | + lb = torch.tensor([0, 0]) |
| 35 | + ub = torch.tensor([4, 4]) |
| 36 | + m = MonotonicRejectionGP( |
| 37 | + lb=lb, |
| 38 | + ub=ub, |
| 39 | + likelihood=GaussianLikelihood(), |
| 40 | + fixed_prior_mean=target, |
| 41 | + monotonic_idxs=[1], |
| 42 | + num_induc=2, |
| 43 | + num_samples=3, |
| 44 | + num_rejection_samples=4, |
| 45 | + ).cuda() |
| 46 | + strat = Strategy( |
| 47 | + lb=lb, |
| 48 | + ub=ub, |
| 49 | + model=m, |
| 50 | + generator=MonotonicRejectionGenerator( |
| 51 | + MonotonicMCLSE, |
| 52 | + acqf_kwargs={"target": target}, |
| 53 | + model_gen_options=model_gen_options, |
| 54 | + ), |
| 55 | + min_asks=1, |
| 56 | + stimuli_per_trial=1, |
| 57 | + outcome_types=["binary"], |
| 58 | + use_gpu_modeling=True, |
| 59 | + ) |
| 60 | + # Fit |
| 61 | + train_x = torch.tensor([[0.0, 0.0], [1.0, 1.0], [2.0, 2.0]]) |
| 62 | + train_y = torch.tensor([[1.0], [2.0], [3.0]]) |
| 63 | + m.fit(train_x=train_x, train_y=train_y) |
| 64 | + self.assertEqual(m.inducing_points.shape, torch.Size([2, 2])) |
| 65 | + self.assertEqual(m.mean_module.constant.item(), 1.5) |
| 66 | + # Predict |
| 67 | + f, var = m.predict(train_x) |
| 68 | + self.assertEqual(f.shape, torch.Size([3])) |
| 69 | + self.assertEqual(var.shape, torch.Size([3])) |
| 70 | + # Gen |
| 71 | + strat.add_data(train_x, train_y) |
| 72 | + Xopt = strat.gen() |
| 73 | + self.assertEqual(Xopt.shape, torch.Size([1, 2])) |
| 74 | + # Acquisition function |
| 75 | + acq = strat.generator._instantiate_acquisition_fn(m) |
| 76 | + self.assertEqual(acq.deriv_constraint_points.shape, torch.Size([2, 3])) |
| 77 | + self.assertTrue( |
| 78 | + torch.equal(acq.deriv_constraint_points[:, -1].cpu(), 2 * torch.ones(2)) |
| 79 | + ) |
| 80 | + self.assertEqual(acq.target, 1.5) |
| 81 | + self.assertTrue(isinstance(acq.objective, IdentityMCObjective)) |
| 82 | + |
| 83 | + def test_classification_gpu(self): |
| 84 | + # Init |
| 85 | + target = 0.75 |
| 86 | + model_gen_options = {"num_restarts": 1, "raw_samples": 3, "epochs": 5} |
| 87 | + lb = torch.tensor([0, 0]) |
| 88 | + ub = torch.tensor([4, 4]) |
| 89 | + m = MonotonicRejectionGP( |
| 90 | + lb=lb, |
| 91 | + ub=ub, |
| 92 | + likelihood=BernoulliLikelihood(), |
| 93 | + fixed_prior_mean=target, |
| 94 | + monotonic_idxs=[1], |
| 95 | + num_induc=2, |
| 96 | + num_samples=3, |
| 97 | + num_rejection_samples=4, |
| 98 | + ).cuda() |
| 99 | + strat = Strategy( |
| 100 | + lb=lb, |
| 101 | + ub=ub, |
| 102 | + model=m, |
| 103 | + generator=MonotonicRejectionGenerator( |
| 104 | + MonotonicMCLSE, |
| 105 | + acqf_kwargs={"target": target, "objective": ProbitObjective()}, |
| 106 | + model_gen_options=model_gen_options, |
| 107 | + ), |
| 108 | + min_asks=1, |
| 109 | + stimuli_per_trial=1, |
| 110 | + outcome_types=["binary"], |
| 111 | + use_gpu_modeling=True, |
| 112 | + ) |
| 113 | + # Fit |
| 114 | + train_x = torch.tensor([[0.0, 0.0], [1.0, 1.0], [2.0, 2.0]]) |
| 115 | + train_y = torch.tensor([1.0, 1.0, 0.0]) |
| 116 | + m.fit(train_x=train_x, train_y=train_y) |
| 117 | + self.assertEqual(m.inducing_points.shape, torch.Size([2, 2])) |
| 118 | + self.assertAlmostEqual(m.mean_module.constant.item(), norm.ppf(0.75)) |
| 119 | + # Predict |
| 120 | + f, var = m.predict(train_x) |
| 121 | + self.assertEqual(f.shape, torch.Size([3])) |
| 122 | + self.assertEqual(var.shape, torch.Size([3])) |
| 123 | + # Gen |
| 124 | + strat.add_data(train_x, train_y) |
| 125 | + Xopt = strat.gen() |
| 126 | + self.assertEqual(Xopt.shape, torch.Size([1, 2])) |
| 127 | + # Acquisition function |
| 128 | + acq = strat.generator._instantiate_acquisition_fn(m) |
| 129 | + self.assertEqual(acq.deriv_constraint_points.shape, torch.Size([2, 3])) |
| 130 | + self.assertTrue( |
| 131 | + torch.equal(acq.deriv_constraint_points[:, -1].cpu(), 2 * torch.ones(2)) |
| 132 | + ) |
| 133 | + self.assertEqual(acq.target, 0.75) |
| 134 | + self.assertTrue(isinstance(acq.objective, ProbitObjective)) |
| 135 | + # Update |
| 136 | + m.update(train_x=train_x[:2, :2], train_y=train_y[:2], warmstart=True) |
| 137 | + self.assertEqual(m.train_inputs[0].shape, torch.Size([2, 3])) |
| 138 | + |
| 139 | + def test_classification_from_config_gpu(self): |
| 140 | + seed = 1 |
| 141 | + torch.manual_seed(seed) |
| 142 | + np.random.seed(seed) |
| 143 | + |
| 144 | + n_init = 15 |
| 145 | + n_opt = 1 |
| 146 | + |
| 147 | + config_str = f""" |
| 148 | + [common] |
| 149 | + parnames = [par1] |
| 150 | + outcome_types = [binary] |
| 151 | + stimuli_per_trial = 1 |
| 152 | + strategy_names = [init_strat, opt_strat] |
| 153 | +
|
| 154 | + [par1] |
| 155 | + par_type = continuous |
| 156 | + lower_bound = 0 |
| 157 | + upper_bound = 1 |
| 158 | +
|
| 159 | + [init_strat] |
| 160 | + generator = SobolGenerator |
| 161 | + min_asks = {n_init} |
| 162 | +
|
| 163 | + [opt_strat] |
| 164 | + generator = MonotonicRejectionGenerator |
| 165 | + model = MonotonicRejectionGP |
| 166 | + acqf = MonotonicMCLSE |
| 167 | + min_asks = {n_opt} |
| 168 | +
|
| 169 | + [MonotonicRejectionGenerator] |
| 170 | + use_gpu = True |
| 171 | +
|
| 172 | + [MonotonicRejectionGP] |
| 173 | + num_induc = 2 |
| 174 | + num_samples = 3 |
| 175 | + num_rejection_samples = 4 |
| 176 | + monotonic_idxs = [0] |
| 177 | + use_gpu = True |
| 178 | +
|
| 179 | + [MonotonicMCLSE] |
| 180 | + target = 0.75 |
| 181 | + objective = ProbitObjective |
| 182 | + """ |
| 183 | + config = Config(config_str=config_str) |
| 184 | + strat = SequentialStrategy.from_config(config) |
| 185 | + |
| 186 | + for _i in range(n_init + n_opt): |
| 187 | + next_x = strat.gen() |
| 188 | + strat.add_data(next_x, int(np.random.rand() > next_x)) |
0 commit comments