예제 #1
0
 def test_noisy_expected_improvement(self, cuda=False):
     for dtype in (torch.float, torch.double):
         model = self._get_model(cuda=cuda, dtype=dtype)
         X_observed = model.train_inputs[0]
         nEI = NoisyExpectedImprovement(model, X_observed, num_fantasies=5)
         X_test = torch.tensor(
             [[[0.25]], [[0.75]]],
             device=X_observed.device,
             dtype=dtype,
             requires_grad=True,
         )
         val = nEI(X_test)
         # test basics
         self.assertEqual(val.dtype, dtype)
         self.assertEqual(val.device.type, X_observed.device.type)
         self.assertEqual(val.shape, torch.Size([2]))
         # test values
         self.assertGreater(val[0].item(), 1e-4)
         self.assertLess(val[1].item(), 1e-6)
         # test gradient
         val.sum().backward()
         self.assertGreater(X_test.grad[0].abs().item(), 1e-4)
         # test without gradient
         with torch.no_grad():
             nEI(X_test)
         # test non-FixedNoiseGP model
         other_model = SingleTaskGP(X_observed, model.train_targets)
         with self.assertRaises(UnsupportedError):
             NoisyExpectedImprovement(other_model, X_observed, num_fantasies=5)
         # Test with minimize
         nEI = NoisyExpectedImprovement(
             model, X_observed, num_fantasies=5, maximize=False
         )
예제 #2
0
 def test_noisy_expected_improvement(self, cuda=False):
     for dtype in (torch.float, torch.double):
         model = self._get_model(cuda=cuda, dtype=dtype)
         X_observed = model.train_inputs[0]
         nEI = NoisyExpectedImprovement(model, X_observed, num_fantasies=5)
         X_test = torch.tensor(
             [[[0.25]], [[0.75]]],
             device=X_observed.device,
             dtype=dtype,
             requires_grad=True,
         )
         val = nEI(X_test)
         # test basics
         self.assertEqual(val.dtype, dtype)
         self.assertEqual(val.device.type, X_observed.device.type)
         self.assertEqual(val.shape, torch.Size([2]))
         # test values
         self.assertGreater(val[0].item(), 1e-4)
         self.assertLess(val[1].item(), 1e-6)
         # test gradient
         val.sum().backward()
         self.assertGreater(X_test.grad[0].abs().item(), 1e-4)
         # test without gradient
         with torch.no_grad():
             nEI(X_test)
예제 #3
0
def main(argv):
    dataset = 1

    try:
        opts, args = getopt.getopt(argv, "hd:", ["dataset="])
    except getopt.GetoptError:
        print ('random parallel with input dataset')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print ('random parallel with input dataset')
            sys.exit()
        elif opt in ("-d", "--dataset"):
            dataset = int(arg)
# average over multiple trials
    for trial in range(3, N_TRIALS + 1):

        print(f"\nTrial {trial:>2} of {N_TRIALS} ", end="")
        best_observed_ei, best_observed_nei = [], []

        # call helper functions to generate initial training data and initialize model
        train_x_ei, train_obj_ei, best_observed_value_ei, current_best_config = generate_initial_data(dataset)

        train_x_nei, train_obj_nei = train_x_ei, train_obj_ei
        best_observed_value_nei = best_observed_value_ei
        mll_nei, model_nei = initialize_model(train_x_nei, train_obj_nei)

        best_observed_nei.append(best_observed_value_nei)

        # run N_BATCH rounds of BayesOpt after the initial random batch
        for iteration in range(1, N_BATCH + 1):

            # fit the models
            fit_gpytorch_model(mll_nei)

            # define the qEI and qNEI acquisition modules using a QMC sampler
            qmc_sampler = SobolQMCNormalSampler(num_samples=MC_SAMPLES)

            # for best_f, we use the best observed noisy values as an approximation

            NEI = NoisyExpectedImprovement(
                model=model_nei,
                X_observed=train_x_nei,
            )

            # optimize and get new observation
            new_x_nei, new_obj_nei = optimize_acqf_and_get_observation(NEI, dataset)

            # update training points

            train_x_nei = torch.cat([train_x_nei, new_x_nei])
            train_obj_nei = torch.cat([train_obj_nei, new_obj_nei])

            # update progress

            best_value_nei = train_obj_nei.max().item()
            best_observed_nei.append(best_value_nei)

            # reinitialize the models so they are ready for fitting on next iteration
            # use the current state dict to speed up fitting
            mll_nei, model_nei = initialize_model(
                train_x_nei,
                train_obj_nei,
                model_nei.state_dict(),
            )

        # return the best configuration

        best_tensor_nei, indices_nei = torch.max(train_obj_nei, 0)
        train_best_x_nei = train_x_nei[indices_nei].cpu().numpy()


        from botorch.acquisition import PosteriorMean


        argmax_pmean_nei, max_pmean_nei = optimize_acqf(
            acq_function=PosteriorMean(model_nei),
            bounds=bounds,
            q=1,
            num_restarts=20,
            raw_samples=2048,
        )

        csv_file_name = '/home/junjie/modes/botorch/' + folder_name + '/modes-b/hp-ngp-nei-dataset-' + str(
            dataset) + '-trail' + str(trial) + '.csv'

        with open(csv_file_name, 'w') as csvFile:
            writer = csv.writer(csvFile)
            writer.writerow([str(argmax_pmean_nei.cpu().numpy()), str(max_pmean_nei.cpu().numpy())])  # nei prediction
            writer.writerow([str(train_best_x_nei), str(best_tensor_nei.cpu().numpy())])  # nei observation


        csvFile.close()