Skip to content

Commit d84737a

Browse files
v0.5.8
hparams update, plot_progress fixed
1 parent 692447c commit d84737a

6 files changed

Lines changed: 68 additions & 32 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotPython"
10-
version = "0.5.7"
10+
version = "0.5.8"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/build/kriging.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,18 +310,18 @@ def update_log(self):
310310
if self.spot_writer is not None:
311311
writer = self.spot_writer
312312
negLnLike = self.negLnLike.copy()
313-
writer.add_scalar("negLnLike", negLnLike, self.counter+self.log_length)
313+
writer.add_scalar("spot_negLnLike", negLnLike, self.counter+self.log_length)
314314
# add the self.n_theta theta values to the writer with one key "theta",
315315
# i.e, the same key for all theta values
316316
theta = self.theta.copy()
317-
writer.add_scalars("theta", {f"theta_{i}": theta[i] for i in range(self.n_theta)},
317+
writer.add_scalars("spot_theta", {f"theta_{i}": theta[i] for i in range(self.n_theta)},
318318
self.counter+self.log_length)
319319
if self.noise:
320320
Lambda = self.Lambda.copy()
321-
writer.add_scalar("Lambda", Lambda, self.counter+self.log_length)
321+
writer.add_scalar("spot_Lambda", Lambda, self.counter+self.log_length)
322322
if self.optim_p:
323323
p = self.p.copy()
324-
writer.add_scalars("p", {f"p_{i}": p[i] for i in range(self.n_p)}, self.counter+self.log_length)
324+
writer.add_scalars("spot_p", {f"p_{i}": p[i] for i in range(self.n_p)}, self.counter+self.log_length)
325325
writer.flush()
326326

327327
def fit_old(self, nat_X, nat_y):

src/spotPython/light/traintest.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
def train_model(config, fun_control):
1313
_L_in = fun_control["_L_in"]
1414
_L_out = fun_control["_L_out"]
15-
print(f"_L_in: {_L_in}")
16-
print(f"_L_out: {_L_out}")
15+
# print(f"_L_in: {_L_in}")
16+
# print(f"_L_out: {_L_out}")
1717
if fun_control["enable_progress_bar"] is None:
1818
enable_progress_bar = False
1919
else:
@@ -27,7 +27,7 @@ def train_model(config, fun_control):
2727
kaiming_init(model)
2828
else:
2929
pass
30-
print(f"model: {model}")
30+
# print(f"model: {model}")
3131

3232
# Init DataModule
3333
dm = CSVDataModule(
@@ -56,7 +56,7 @@ def train_model(config, fun_control):
5656
result = trainer.validate(model=model, datamodule=dm)
5757
# unlist the result (from a list of one dict)
5858
result = result[0]
59-
print(f"train_model result: {result}")
59+
# print(f"train_model result: {result}")
6060
return result["val_loss"]
6161

6262

@@ -84,7 +84,7 @@ def test_model(config, fun_control):
8484
kaiming_init(model)
8585
else:
8686
pass
87-
print(f"model: {model}")
87+
# print(f"model: {model}")
8888
# Init trainer
8989
trainer = L.Trainer(
9090
# Where to save models
@@ -103,7 +103,7 @@ def test_model(config, fun_control):
103103
trainer.fit(model=model, datamodule=dm)
104104
test_result = trainer.test(datamodule=dm, ckpt_path="last")
105105
test_result = test_result[0]
106-
print(f"test_model result: {test_result}")
106+
# print(f"test_model result: {test_result}")
107107
return test_result["val_loss"], test_result["val_acc"]
108108

109109

@@ -131,7 +131,7 @@ def cv_model(config, fun_control):
131131
kaiming_init(model)
132132
else:
133133
pass
134-
print(f"model: {model}")
134+
# print(f"model: {model}")
135135

136136
dm = CrossValidationDataModule(
137137
k=k,
@@ -170,7 +170,7 @@ def cv_model(config, fun_control):
170170
results.append(score["valid_mapk"])
171171

172172
mapk_score = sum(results) / num_folds
173-
print(f"cv_model mapk result: {mapk_score}")
173+
# print(f"cv_model mapk result: {mapk_score}")
174174
return mapk_score
175175

176176

src/spotPython/spot/spot.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ def append_X_ocba(self, X_ocba, X0):
326326

327327
def run(self, X_start=None):
328328
self.initialize_design(X_start)
329+
# New: self.update_stats() moved here:
330+
self.update_stats()
329331
# (S-5) Calling the spotLoop Function
330332
# and
331333
# (S-9) Termination Criteria, Conditions:
@@ -365,8 +367,22 @@ def initialize_design(self, X_start=None):
365367
self.y = self.fun(X=X_all, fun_control=self.fun_control)
366368
# TODO: Error if only nan values are returned
367369
logger.debug("New y value: %s", self.y)
370+
#
371+
self.counter = self.y.size
372+
if self.spot_writer is not None:
373+
writer = self.spot_writer
374+
# range goes to init_size -1 because the last value is added by update_stats(),
375+
# which always adds the last value.
376+
for j in range(len(self.y) - 1):
377+
X_j = self.X[j].copy()
378+
y_j = self.y[j].copy()
379+
config = {self.var_name[i]: X_j[i] for i in range(self.k)}
380+
writer.add_hparams(config, {"spot_y": y_j})
381+
writer.flush()
382+
#
368383
self.X, self.y = remove_nan(self.X, self.y)
369-
self.update_stats()
384+
# self.update_stats() moved to run()!
385+
# self.update_stats()
370386
# (S-4): Imputation:
371387
# Not implemented yet.
372388
# (S-11) Surrogate Fit:
@@ -422,13 +438,16 @@ def update_stats(self):
422438
y_min = self.min_y.copy()
423439
y_last = self.last_y.copy()
424440
X_min = self.min_X.copy()
441+
# y_min: best y value so far
442+
# y_last: last y value, can he worse than y_min
425443
writer.add_scalars("spot_y", {"min": y_min, "last": y_last}, self.counter)
444+
# X_min: X value of the best y value so far
426445
writer.add_scalars("spot_X", {f"X_{i}": X_min[i] for i in range(self.k)}, self.counter)
427446
# get last value of self.X and convert to dict. take the values from self.var_name as keys:
428447
X_last = self.X[-1].copy()
429448
config = {self.var_name[i]: X_last[i] for i in range(self.k)}
430-
y_val = self.y[-1].copy()
431-
writer.add_hparams(config, {"fun_torch: loss": y_val})
449+
# hyperparameters X and value y of the last configuration:
450+
writer.add_hparams(config, {"spot_y": y_last})
432451
writer.flush()
433452
# Update aggregated x and y values (if noise):
434453
if self.noise:
@@ -440,11 +459,17 @@ def update_stats(self):
440459
self.min_mean_X = self.mean_X[argmin(self.mean_y)]
441460
if self.spot_writer is not None:
442461
writer = self.spot_writer
462+
# y_min_mean: best mean y value so far
443463
y_min_mean = self.min_mean_y.copy()
464+
# X_min_mean: X value of the best mean y value so far
444465
X_min_mean = self.min_mean_X.copy()
466+
# y_var: variance of the y values
445467
y_var = self.var_y.copy()
446-
writer.add_scalars("spot_y_noise", {"min_mean_y": y_min_mean, "last": y_last}, self.counter)
447468
writer.add_scalar("spot_y_var", y_var, self.counter)
469+
# y_min_mean: best mean y value so far (see above)
470+
# y_last: last y value, can he worse than y_min_mean
471+
writer.add_scalars("spot_y_noise", {"min_mean_y": y_min_mean, "last": y_last}, self.counter)
472+
# X_min_mean: X value of the best mean y value so far (see above)
448473
writer.add_scalars(
449474
"spot_X_noise", {f"X_min_mean{i}": X_min_mean[i] for i in range(self.k)}, self.counter
450475
)
@@ -574,10 +599,10 @@ def plot_progress(
574599
range(1, n_init + 1),
575600
s_y[:n_init],
576601
style[0],
577-
range(1, n_init + 1),
578-
[s_c[:n_init].min()] * n_init,
602+
range(1, n_init + 2),
603+
[s_c[:n_init].min()] * (n_init + 1),
579604
style[1],
580-
range(n_init, len(s_c)),
605+
range(n_init + 1, len(s_c) + 1),
581606
s_c[n_init:],
582607
style[2],
583608
)

src/spotPython/utils/file.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datetime import datetime
55
from dateutil.tz import tzlocal
66
import pickle
7+
import os
78

89

910
def load_data(data_dir="./data"):
@@ -74,3 +75,15 @@ def load_pickle(filename: str):
7475
with open(filename, "rb") as f:
7576
obj = pickle.load(f)
7677
return obj
78+
79+
80+
def get_spot_tensorboard_path(experiment_name):
81+
"""Get the path to the spot tensorboard files.
82+
Args:
83+
experiment_name (str): The name of the experiment.
84+
Returns:
85+
spot_tensorboard_path (str): The path to the folder where the spot tensorboard files are saved.
86+
"""
87+
spot_tensorboard_path = os.environ.get("PATH_TENSORBOARD", "runs/spot_logs/")
88+
spot_tensorboard_path = os.path.join(spot_tensorboard_path, experiment_name)
89+
return spot_tensorboard_path

src/spotPython/utils/init.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def fun_control_init(
1010
_L_in=None,
1111
_L_out=None,
1212
enable_progress_bar=False,
13-
tensorboard_path=None,
13+
spot_tensorboard_path="runs_spot/",
1414
num_workers=0,
1515
device=None,
1616
):
@@ -20,7 +20,8 @@ def fun_control_init(
2020
_L_in (int): The number of input features.
2121
_L_out (int): The number of output features.
2222
enable_progress_bar (bool): Whether to enable the progress bar or not.
23-
tensorboard_path (str): The path to the folder where the tensorboard files are saved.
23+
spot_tensorboard_path (str): The path to the folder where the spot tensorboard files are saved.
24+
If None, no spot tensorboard files are saved.
2425
num_workers (int): The number of workers to use for the data loading.
2526
device (str): The device to use for the training. It can be either "cpu", "mps", or "cuda".
2627
Returns:
@@ -66,15 +67,6 @@ def fun_control_init(
6667
# Setting the seed
6768
L.seed_everything(42)
6869

69-
if tensorboard_path is not None:
70-
check_and_create_dir(tensorboard_path)
71-
# Starting with v0.2.41, Summary Writer should be not initialized here but by Lightning
72-
# it is only available for compatibility reasons.
73-
# So, set this to None and let Lightning manage the logging.
74-
spot_writer = SummaryWriter(tensorboard_path)
75-
else:
76-
spot_writer = None
77-
7870
# Path to the folder where the pretrained models are saved
7971
CHECKPOINT_PATH = os.environ.get("PATH_CHECKPOINT", "saved_models/")
8072
os.makedirs(CHECKPOINT_PATH, exist_ok=True)
@@ -87,6 +79,12 @@ def fun_control_init(
8779
# Path to the folder where the tensorboard files are saved
8880
TENSORBOARD_PATH = os.environ.get("PATH_TENSORBOARD", "runs/")
8981
os.makedirs(TENSORBOARD_PATH, exist_ok=True)
82+
if spot_tensorboard_path is not None:
83+
os.makedirs(spot_tensorboard_path, exist_ok=True)
84+
spot_writer = SummaryWriter(spot_tensorboard_path)
85+
86+
if not os.path.exists("./figures"):
87+
os.makedirs("./figures")
9088

9189
fun_control = {
9290
"CHECKPOINT_PATH": CHECKPOINT_PATH,
@@ -119,7 +117,7 @@ def fun_control_init(
119117
"train": None,
120118
"test": None,
121119
"task": task,
122-
"tensorboard_path": tensorboard_path,
120+
"spot_tensorboard_path": spot_tensorboard_path,
123121
"weights": 1.0,
124122
"spot_writer": spot_writer,
125123
}

0 commit comments

Comments
 (0)