mirror of
https://github.com/c-sooyoung/bo-ptycho.git
synced 2026-09-17 19:29:07 +09:00
added hotfix for batched bo
This commit is contained in:
+1
-1
@@ -7,7 +7,7 @@ class BOEngine(ABC):
|
|||||||
self.state = None
|
self.state = None
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def ask(self):
|
def ask(self, n: int = 1) -> list[dict]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
|||||||
+14
-11
@@ -37,20 +37,23 @@ class RandomBOEngine(BOEngine):
|
|||||||
self.train_y = train_y
|
self.train_y = train_y
|
||||||
|
|
||||||
|
|
||||||
def ask(self):
|
def ask(self, n = 1):
|
||||||
config = self.config
|
config = self.config
|
||||||
next_config = copy.deepcopy(config)
|
next_configs = []
|
||||||
|
|
||||||
for param in self.params:
|
for _ in range(n):
|
||||||
radius = config['bo']['params'][param]['radius']
|
next_config = copy.deepcopy(config)
|
||||||
center = config['ptycho']['params'][param]
|
for param in self.params:
|
||||||
modulation = radius * (np.random.rand() - 0.5) * 2
|
radius = config['bo']['params'][param]['radius']
|
||||||
next_value = center + modulation
|
center = config['ptycho']['params'][param]
|
||||||
if self.param_types[param] == 'int':
|
modulation = radius * (np.random.rand() - 0.5) * 2
|
||||||
next_value = round(next_value)
|
next_value = center + modulation
|
||||||
next_config['ptycho']['params'][param] = next_value
|
if self.param_types[param] == 'int':
|
||||||
|
next_value = round(next_value)
|
||||||
|
next_config['ptycho']['params'][param] = next_value
|
||||||
|
next_configs.append(next_config)
|
||||||
|
|
||||||
return next_config
|
return next_configs
|
||||||
|
|
||||||
|
|
||||||
def tell(self, job_config, y_value):
|
def tell(self, job_config, y_value):
|
||||||
|
|||||||
+14
-17
@@ -52,7 +52,7 @@ class SingleObjectiveBOEngine(BOEngine):
|
|||||||
self.acquisition = config['bo']['acquisition']
|
self.acquisition = config['bo']['acquisition']
|
||||||
|
|
||||||
|
|
||||||
def ask(self):
|
def ask(self, n = 1):
|
||||||
|
|
||||||
train_x = torch.from_numpy(self.train_x)
|
train_x = torch.from_numpy(self.train_x)
|
||||||
train_y = torch.from_numpy(self.train_y).unsqueeze(-1) # shape: (BOiter, 1)
|
train_y = torch.from_numpy(self.train_y).unsqueeze(-1) # shape: (BOiter, 1)
|
||||||
@@ -98,44 +98,41 @@ class SingleObjectiveBOEngine(BOEngine):
|
|||||||
sampler = SobolQMCNormalSampler(sample_shape=torch.Size([512]))
|
sampler = SobolQMCNormalSampler(sample_shape=torch.Size([512]))
|
||||||
|
|
||||||
if self.acquisition == 'ucb':
|
if self.acquisition == 'ucb':
|
||||||
beta = 0.2
|
acqf = qUpperConfidenceBound(gp, beta=self.config['bo']['beta'], sampler=sampler)
|
||||||
print("Acquisition: UCB | Beta: {} (fixed)".format(beta))
|
|
||||||
acqf = qUpperConfidenceBound(gp, beta=beta, sampler=sampler)
|
|
||||||
elif self.acquisition == 'ei':
|
elif self.acquisition == 'ei':
|
||||||
best_f = train_y.max()
|
acqf = qLogExpectedImprovement(gp, best_f=train_y.max(), sampler=sampler)
|
||||||
print("Acquisition: LogEI best_f: {:.6f}".format(best_f.item()))
|
|
||||||
acqf = qLogExpectedImprovement(gp, best_f=best_f, sampler=sampler)
|
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError(f"Acquisition function {self.acquisition} is not implemented. Current options: 'ucb', 'ei'")
|
raise NotImplementedError(f"Acquisition function {self.acquisition} is not implemented. Current options: 'ucb', 'ei'")
|
||||||
|
|
||||||
# Full [0,1]^d search (trust region disabled)
|
|
||||||
acqf_bounds = torch.stack([
|
acqf_bounds = torch.stack([
|
||||||
torch.zeros(train_x.shape[1], dtype=torch.double),
|
torch.zeros(train_x.shape[1], dtype=torch.double),
|
||||||
torch.ones(train_x.shape[1], dtype=torch.double),
|
torch.ones(train_x.shape[1], dtype=torch.double),
|
||||||
])
|
])
|
||||||
|
|
||||||
candidate, _ = optimize_acqf(
|
candidates, _ = optimize_acqf(
|
||||||
acq_function=acqf,
|
acq_function=acqf,
|
||||||
bounds=acqf_bounds,
|
bounds=acqf_bounds,
|
||||||
q=1,
|
q=n,
|
||||||
num_restarts=20,
|
num_restarts=20,
|
||||||
raw_samples=1024,
|
raw_samples=1024,
|
||||||
post_processing_func=self._pr_post_processing, # PR applied here
|
post_processing_func=self._pr_post_processing, # PR applied here
|
||||||
sequential=True,
|
sequential=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
new_x = candidate.detach() * (bounds[1] - bounds[0]) + bounds[0]
|
new_xs = candidates.detach() * (bounds[1] - bounds[0]) + bounds[0]
|
||||||
|
|
||||||
# Hard-round integer dims (final guarantee)
|
# Hard-round integer dims (final guarantee)
|
||||||
for i in self.integer_indices:
|
for i in self.integer_indices:
|
||||||
new_x[:, i] = torch.round(new_x[:, i])
|
new_xs[:, i] = torch.round(new_xs[:, i])
|
||||||
|
|
||||||
next_config = copy.deepcopy(self.config)
|
next_configs = []
|
||||||
|
for i in range(n):
|
||||||
|
next_config = copy.deepcopy(self.config)
|
||||||
|
for j, param in enumerate(self.params):
|
||||||
|
next_config['ptycho']['params'][param] = new_xs[i,j].item()
|
||||||
|
next_configs.append(next_config)
|
||||||
|
|
||||||
for i, param in enumerate(self.params):
|
return next_configs
|
||||||
next_config['ptycho']['params'][param] = new_x[0,i].item()
|
|
||||||
|
|
||||||
return next_config
|
|
||||||
|
|
||||||
|
|
||||||
def _pr_post_processing(self, X):
|
def _pr_post_processing(self, X):
|
||||||
|
|||||||
+16
-14
@@ -1,22 +1,22 @@
|
|||||||
job:
|
job:
|
||||||
type: "random+sobo"
|
type: "test"
|
||||||
random_iters: 3
|
random_iters: 5
|
||||||
sobo_iters: 30
|
sobo_iters: 10
|
||||||
|
|
||||||
io:
|
io:
|
||||||
input_data_path: '/home/swim/Si_project/data/Si2V1_1.mat'
|
input_data_path: '/home/swim/shared/Si_project/data/Si2V1_1.mat'
|
||||||
result_dir: '/home/swim/bo-ptycho/results/test'
|
result_dir: '/home/swim/bo-ptycho/results/test'
|
||||||
verbosity: 1
|
verbosity: 1
|
||||||
|
|
||||||
ptycho:
|
ptycho:
|
||||||
engine: 'fold_slice'
|
engine: 'fold_slice'
|
||||||
path: '/home/swim/fold_slice'
|
path: '/home/swim/fold_slice-stable'
|
||||||
params:
|
params:
|
||||||
voltage: 200
|
voltage: 200
|
||||||
alpha_max: 30
|
alpha_max: 30
|
||||||
defocus: -200
|
defocus: -200
|
||||||
rot_ang: 0.3
|
rot_ang: 0.3
|
||||||
Nlayers: 20
|
Nlayers: 5
|
||||||
thickness: 250
|
thickness: 250
|
||||||
rbf: 37
|
rbf: 37
|
||||||
Nprobe: 1
|
Nprobe: 1
|
||||||
@@ -25,8 +25,8 @@ ptycho:
|
|||||||
tilt_x: 2
|
tilt_x: 2
|
||||||
tilt_y: 0
|
tilt_y: 0
|
||||||
scan_step_size: 0.36
|
scan_step_size: 0.36
|
||||||
Niter: 100
|
Niter: 5
|
||||||
Niter_save_results: 100
|
Niter_save_results: 5
|
||||||
CBED_size: 192
|
CBED_size: 192
|
||||||
ADU: 1
|
ADU: 1
|
||||||
extra_print_info: ''
|
extra_print_info: ''
|
||||||
@@ -36,24 +36,26 @@ ptycho:
|
|||||||
diff_pattern_blur: 1
|
diff_pattern_blur: 1
|
||||||
probe_change_start: 1
|
probe_change_start: 1
|
||||||
object_change_start: 1
|
object_change_start: 1
|
||||||
grouping: 64
|
grouping: inf
|
||||||
probe_posiiton_search: 1
|
probe_position_search: 1
|
||||||
regularize_layers: 0.2
|
regularize_layers: 0.2
|
||||||
variable_probe: 'false'
|
variable_probe: 'false'
|
||||||
|
verbosity
|
||||||
|
|
||||||
bo:
|
bo:
|
||||||
mode: 'sobo'
|
batch: 4
|
||||||
acquisition: 'ucb'
|
acquisition: 'ucb'
|
||||||
|
beta: 0.1 # for ucb only
|
||||||
metric: 'log_fourier'
|
metric: 'log_fourier'
|
||||||
params:
|
params:
|
||||||
alpha_max:
|
alpha_max:
|
||||||
defocus:
|
defocus:
|
||||||
radius: 100
|
# radius: 100
|
||||||
rot_ang:
|
rot_ang:
|
||||||
Nlayers:
|
Nlayers:
|
||||||
radius: 5
|
radius: 2
|
||||||
type: int
|
type: int
|
||||||
thickness:
|
thickness:
|
||||||
radius: 100
|
# radius: 100
|
||||||
train_x:
|
train_x:
|
||||||
train_y:
|
train_y:
|
||||||
|
|||||||
@@ -8,11 +8,7 @@ def main(config_yaml):
|
|||||||
with open(config_yaml, 'r') as f:
|
with open(config_yaml, 'r') as f:
|
||||||
config = yaml.safe_load(f)
|
config = yaml.safe_load(f)
|
||||||
|
|
||||||
job_types = {
|
pipelines.job_types[config['job']['type']](config)
|
||||||
'random+sobo': pipelines.sobo_pipeline
|
|
||||||
}
|
|
||||||
|
|
||||||
job_types[config['job']['type']](config)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -1 +1,7 @@
|
|||||||
from .sobo import sobo_pipeline
|
from .sobo import sobo_pipeline
|
||||||
|
from .test import test_pipeline
|
||||||
|
|
||||||
|
job_types = {
|
||||||
|
'random+sobo': sobo_pipeline,
|
||||||
|
'test': test_pipeline,
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
import os
|
||||||
|
import bo
|
||||||
|
import ptycho
|
||||||
|
|
||||||
|
|
||||||
|
def test_pipeline(config):
|
||||||
|
|
||||||
|
result_dir = config['io']['result_dir']
|
||||||
|
os.makedirs(result_dir, exist_ok=True)
|
||||||
|
|
||||||
|
RANDOM_ITERS = config['job'].get('random_iters', 0)
|
||||||
|
SOBO_ITERS = config['job'].get('sobo_iters')
|
||||||
|
METRIC = config['bo']['metric']
|
||||||
|
PTYCHOENGINE = ptycho.engines[config['ptycho']['engine']]
|
||||||
|
|
||||||
|
randombo = bo.RandomBOEngine(config)
|
||||||
|
|
||||||
|
# bo_txt = os.path.join(result_dir, "bo.txt")
|
||||||
|
# with open(bo_txt, "w") as f:
|
||||||
|
# f.write(f" iter\tmetric\t{"\t".join([p[:7] for p in randombo.params])}\n")
|
||||||
|
|
||||||
|
for j in range(RANDOM_ITERS):
|
||||||
|
print(f"RANDOM sampling; iteration {j}")
|
||||||
|
job_configs = randombo.ask(n=4)
|
||||||
|
for job_config in job_configs:
|
||||||
|
ptycho_engine = PTYCHOENGINE(job_config)
|
||||||
|
ptycho_engine.run(run_id=f"bo-{j:03d}")
|
||||||
|
y_value = ptycho_engine.metric(METRIC)
|
||||||
|
randombo.tell(job_config, y_value)
|
||||||
|
# with open(bo_txt, "a") as f:
|
||||||
|
# p = [f'{job_config['ptycho']['params'][key]:.2f}' for key in randombo.params]
|
||||||
|
# f.write(f"{j: 8d}\t{y_value:.4f}\t{"\t".join(p)}\n")
|
||||||
|
|
||||||
|
|
||||||
|
sobo = bo.SingleObjectiveBOEngine(config)
|
||||||
|
sobo.train_x = randombo.train_x
|
||||||
|
sobo.train_y = randombo.train_y
|
||||||
|
|
||||||
|
for j in range(SOBO_ITERS):
|
||||||
|
print(f"SOBO sampling; iteration {RANDOM_ITERS + j}")
|
||||||
|
job_configs = sobo.ask(n=4)
|
||||||
|
for job_config in job_configs:
|
||||||
|
ptycho_engine = PTYCHOENGINE(job_config)
|
||||||
|
ptycho_engine.run(run_id=f"bo-{RANDOM_ITERS + j:03d}")
|
||||||
|
y_value = ptycho_engine.metric(METRIC)
|
||||||
|
sobo.tell(job_config, y_value)
|
||||||
|
# with open(bo_txt, "a") as f:
|
||||||
|
# p = [f'{job_config['ptycho']['params'][key]:.2f}' for key in sobo.params]
|
||||||
|
# f.write(f"{RANDOM_ITERS + j: 8d}\t{y_value:.4f}\t{"\t".join(p)}\n")
|
||||||
Reference in New Issue
Block a user