From b09e44daa5d238a9d5e87dd9a6e78434009e7f8d Mon Sep 17 00:00:00 2001 From: Niko Date: Fri, 7 Apr 2023 12:22:46 +0200 Subject: [PATCH] added BO torch tryout --- .idea/ActiveBOToytask.iml | 2 +- .idea/misc.xml | 2 +- BoTorchTest/botorchtest1.py | 26 ++++++++++++++++++++++++++ runner/BOGymRunner.py | 4 ++-- 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 BoTorchTest/botorchtest1.py diff --git a/.idea/ActiveBOToytask.iml b/.idea/ActiveBOToytask.iml index fa79220..09bfacf 100644 --- a/.idea/ActiveBOToytask.iml +++ b/.idea/ActiveBOToytask.iml @@ -4,7 +4,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 7e39aa8..dc9ea49 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/BoTorchTest/botorchtest1.py b/BoTorchTest/botorchtest1.py new file mode 100644 index 0000000..4e10e9d --- /dev/null +++ b/BoTorchTest/botorchtest1.py @@ -0,0 +1,26 @@ +import torch +from botorch.models import SingleTaskGP +from botorch.fit import fit_gpytorch_mll +from botorch.utils import standardize +from gpytorch.mlls import ExactMarginalLogLikelihood + +from botorch.acquisition import UpperConfidenceBound +from botorch.optim import optimize_acqf + + +train_X = torch.rand(10, 2, dtype=torch.double) +Y = 1 - torch.norm(train_X - 0.5, dim=-1, keepdim=True) +Y = Y + 0.1 * torch.randn_like(Y) # add some noise +train_Y = standardize(Y) + +gp = SingleTaskGP(train_X, train_Y) +mll = ExactMarginalLogLikelihood(gp.likelihood, gp) +fit_gpytorch_mll(mll) + +UCB = UpperConfidenceBound(gp, beta=0.1) + +bounds = torch.stack([torch.zeros(2), torch.ones(2)]) +candidate, acq_value = optimize_acqf( + UCB, bounds=bounds, q=1, num_restarts=5, raw_samples=20, +) +print(candidate) \ No newline at end of file diff --git a/runner/BOGymRunner.py b/runner/BOGymRunner.py index 94ff760..13733f3 100644 --- a/runner/BOGymRunner.py +++ b/runner/BOGymRunner.py @@ -5,10 +5,10 @@ import numpy as np import matplotlib.pyplot as plt # BO parameters -env = Continuous_MountainCarEnv() +env = Continuous_MountainCarEnv() nr_steps = 100 acquisition_fun = 'ei' -iteration_steps = 200 +iteration_steps = 100 nr_runs = 100