def test_gen_batch_initial_conditions_warning(self): for dtype in (torch.float, torch.double): bounds = torch.tensor([[0, 0], [1, 1]], device=self.device, dtype=dtype) samples = torch.zeros(10, 1, 2, device=self.device, dtype=dtype) with ExitStack() as es: ws = es.enter_context(warnings.catch_warnings(record=True)) es.enter_context(settings.debug(True)) es.enter_context( mock.patch( "botorch.optim.initializers.draw_sobol_samples", return_value=samples, )) batch_initial_conditions = gen_batch_initial_conditions( acq_function=MockAcquisitionFunction(), bounds=bounds, q=1, num_restarts=2, raw_samples=10, options={"seed": 1234}, ) self.assertEqual(len(ws), 1) self.assertTrue( any( issubclass(w.category, BadInitialCandidatesWarning) for w in ws)) self.assertTrue( torch.equal( batch_initial_conditions, torch.zeros(2, 1, 2, device=self.device, dtype=dtype), ))
def test_gen_batch_initial_conditions_highdim(self): d = 120 bounds = torch.stack([torch.zeros(d), torch.ones(d)]) for dtype in (torch.float, torch.double): bounds = bounds.to(device=self.device, dtype=dtype) for nonnegative in (True, False): for seed in (None, 1234): with warnings.catch_warnings( record=True) as ws, settings.debug(True): batch_initial_conditions = gen_batch_initial_conditions( acq_function=MockAcquisitionFunction(), bounds=bounds, q=10, num_restarts=1, raw_samples=2, options={ "nonnegative": nonnegative, "eta": 0.01, "alpha": 0.1, "seed": seed, }, ) self.assertTrue( any( issubclass(w.category, SamplingWarning) for w in ws)) expected_shape = torch.Size([1, 10, d]) self.assertEqual(batch_initial_conditions.shape, expected_shape) self.assertEqual(batch_initial_conditions.device, bounds.device) self.assertEqual(batch_initial_conditions.dtype, bounds.dtype)
def test_gen_batch_initial_conditions(self): for dtype in (torch.float, torch.double): bounds = torch.tensor([[0, 0], [1, 1]], device=self.device, dtype=dtype) for nonnegative in (True, False): for seed in (None, 1234): batch_initial_conditions = gen_batch_initial_conditions( acq_function=MockAcquisitionFunction(), bounds=bounds, q=1, num_restarts=2, raw_samples=10, options={ "nonnegative": nonnegative, "eta": 0.01, "alpha": 0.1, "seed": seed, }, ) expected_shape = torch.Size([2, 1, 2]) self.assertEqual(batch_initial_conditions.shape, expected_shape) self.assertEqual(batch_initial_conditions.device, bounds.device) self.assertEqual(batch_initial_conditions.dtype, bounds.dtype)
def test_gen_batch_initial_conditions_simple_warning(self, cuda=False): device = torch.device("cuda") if cuda else torch.device("cpu") for dtype in (torch.float, torch.double): bounds = torch.tensor([[0, 0], [1, 1]], device=device, dtype=dtype) with warnings.catch_warnings(record=True) as ws: with mock.patch( "botorch.optim.optimize.draw_sobol_samples", return_value=torch.zeros(10, 1, 2, device=device, dtype=dtype), ): batch_initial_conditions = gen_batch_initial_conditions( acq_function=MockAcquisitionFunction(), bounds=bounds, q=1, num_restarts=2, raw_samples=10, ) self.assertEqual(len(ws), 1) self.assertTrue( issubclass(ws[-1].category, BadInitialCandidatesWarning)) self.assertTrue( torch.equal( batch_initial_conditions, torch.zeros(2, 1, 2, device=device, dtype=dtype), ))
def test_gen_batch_initial_conditions_simple_warning(self, cuda=False): device = torch.device("cuda") if cuda else torch.device("cpu") for dtype in (torch.float, torch.double): bounds = torch.tensor([[0, 0], [1, 1]], device=device, dtype=dtype) with warnings.catch_warnings(record=True) as ws: with mock.patch( "botorch.optim.optimize.draw_sobol_samples", return_value=torch.zeros(10, 1, 2, device=device, dtype=dtype), ): batch_initial_conditions = gen_batch_initial_conditions( acq_function=MockAcquisitionFunction(), bounds=bounds, q=1, num_restarts=2, raw_samples=10, ) self.assertEqual(len(ws), 1) self.assertTrue( issubclass(ws[-1].category, BadInitialCandidatesWarning) ) self.assertTrue( torch.equal( batch_initial_conditions, torch.zeros(2, 1, 2, device=device, dtype=dtype), ) )
def test_gen_batch_initial_conditions(self, cuda=False): device = torch.device("cuda") if cuda else torch.device("cpu") for dtype in (torch.float, torch.double): bounds = torch.tensor([[0, 0], [1, 1]], device=device, dtype=dtype) for nonnegative in (True, False): batch_initial_conditions = gen_batch_initial_conditions( acq_function=MockAcquisitionFunction(), bounds=bounds, q=1, num_restarts=2, raw_samples=10, options={"nonnegative": nonnegative, "eta": 0.01, "alpha": 0.1}, ) expected_shape = torch.Size([2, 1, 2]) self.assertEqual(batch_initial_conditions.shape, expected_shape) self.assertEqual(batch_initial_conditions.device, bounds.device) self.assertEqual(batch_initial_conditions.dtype, bounds.dtype)