Skip to content

Commit e0cc650

Browse files
summary writer mechanism updated
1 parent bc06082 commit e0cc650

10 files changed

Lines changed: 237 additions & 73 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,4 @@ notebooks/00_spotPython_tests_files/libs/quarto-html/tippy.css
321321
notebooks/00_spotPython_tests_files/libs/quarto-html/tippy.umd.min.js
322322
notebooks/00_spotPython_tests.html
323323
data.csv
324+
spot_00_experiment.pickle

notebooks/00_spotPython_tests.ipynb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4629,6 +4629,55 @@
46294629
"assert y0 >= 0"
46304630
]
46314631
},
4632+
{
4633+
"cell_type": "markdown",
4634+
"metadata": {},
4635+
"source": [
4636+
"# Tensorboard Path"
4637+
]
4638+
},
4639+
{
4640+
"cell_type": "code",
4641+
"execution_count": 2,
4642+
"metadata": {},
4643+
"outputs": [
4644+
{
4645+
"data": {
4646+
"text/plain": [
4647+
"'runs/spot_logs/00_ubuntu_2021-08-31_14-30-00'"
4648+
]
4649+
},
4650+
"execution_count": 2,
4651+
"metadata": {},
4652+
"output_type": "execute_result"
4653+
}
4654+
],
4655+
"source": [
4656+
"from spotPython.utils.init import get_spot_tensorboard_path\n",
4657+
"get_spot_tensorboard_path(\"00_ubuntu_2021-08-31_14-30-00\")"
4658+
]
4659+
},
4660+
{
4661+
"cell_type": "code",
4662+
"execution_count": 4,
4663+
"metadata": {},
4664+
"outputs": [
4665+
{
4666+
"data": {
4667+
"text/plain": [
4668+
"'00_p040025_2024-08-08_18-42-23'"
4669+
]
4670+
},
4671+
"execution_count": 4,
4672+
"metadata": {},
4673+
"output_type": "execute_result"
4674+
}
4675+
],
4676+
"source": [
4677+
"from spotPython.utils.init import get_experiment_name\n",
4678+
"get_experiment_name(prefix=\"00\")"
4679+
]
4680+
},
46324681
{
46334682
"cell_type": "code",
46344683
"execution_count": null,

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.14.62"
10+
version = "0.14.63"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/build/kriging.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -433,21 +433,20 @@ def update_log(self) -> None:
433433
# get the length of the log
434434
self.log_length = len(self.log["negLnLike"])
435435
if self.spot_writer is not None:
436-
writer = self.spot_writer
437436
negLnLike = self.negLnLike.copy()
438-
writer.add_scalar("spot_negLnLike", negLnLike, self.counter+self.log_length)
437+
self.spot_writer.add_scalar("spot_negLnLike", negLnLike, self.counter+self.log_length)
439438
# add the self.n_theta theta values to the writer with one key "theta",
440439
# i.e, the same key for all theta values
441440
theta = self.theta.copy()
442-
writer.add_scalars("spot_theta", {f"theta_{i}": theta[i] for i in range(self.n_theta)},
443-
self.counter+self.log_length)
441+
self.spot_writer.add_scalars("spot_theta", {f"theta_{i}": theta[i] for i in range(self.n_theta)},
442+
self.counter+self.log_length)
444443
if self.noise:
445444
Lambda = self.Lambda.copy()
446-
writer.add_scalar("spot_Lambda", Lambda, self.counter+self.log_length)
445+
self.spot_writer.add_scalar("spot_Lambda", Lambda, self.counter+self.log_length)
447446
if self.optim_p:
448447
p = self.p.copy()
449-
writer.add_scalars("spot_p", {f"p_{i}": p[i] for i in range(self.n_p)}, self.counter+self.log_length)
450-
writer.flush()
448+
self.spot_writer.add_scalars("spot_p", {f"p_{i}": p[i] for i in range(self.n_p)}, self.counter+self.log_length)
449+
self.spot_writer.flush()
451450

452451
def fit(self, nat_X: np.ndarray, nat_y: np.ndarray) -> object:
453452
"""

src/spotPython/hyperparameters/update.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ def get_ith_hyperparameter_name_from_fun_control(fun_control, key, i):
12231223
Examples:
12241224
>>> from spotPython.utils.device import getDevice
12251225
from spotPython.utils.init import fun_control_init
1226-
from spotPython.utils.file import get_experiment_name, get_spot_tensorboard_path
1226+
from spotPython.utils.file import get_experiment_name
12271227
import numpy as np
12281228
from spotPython.data.diabetes import Diabetes
12291229
from spotPython.light.regression.netlightregression import NetLightRegression
@@ -1234,7 +1234,6 @@ def get_ith_hyperparameter_name_from_fun_control(fun_control, key, i):
12341234
from spotPython.hyperparameters.values import set_control_hyperparameter_value
12351235
experiment_name = get_experiment_name(prefix="000")
12361236
fun_control = fun_control_init(
1237-
spot_tensorboard_path=get_spot_tensorboard_path(experiment_name),
12381237
_L_in=10,
12391238
_L_out=1,
12401239
TENSORBOARD_CLEAN=True,

src/spotPython/hyperparameters/values.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,7 @@ def get_ith_hyperparameter_name_from_fun_control(fun_control, key, i):
12491249
Examples:
12501250
>>> from spotPython.utils.device import getDevice
12511251
from spotPython.utils.init import fun_control_init
1252-
from spotPython.utils.file import get_experiment_name, get_spot_tensorboard_path
1252+
from spotPython.utils.file import get_experiment_name
12531253
import numpy as np
12541254
from spotPython.data.diabetes import Diabetes
12551255
from spotPython.light.regression.netlightregression import NetLightRegression
@@ -1260,7 +1260,6 @@ def get_ith_hyperparameter_name_from_fun_control(fun_control, key, i):
12601260
from spotPython.hyperparameters.values import set_control_hyperparameter_value
12611261
experiment_name = get_experiment_name(prefix="000")
12621262
fun_control = fun_control_init(
1263-
spot_tensorboard_path=get_spot_tensorboard_path(experiment_name),
12641263
_L_in=10,
12651264
_L_out=1,
12661265
TENSORBOARD_CLEAN=True,

src/spotPython/spot/spot.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from math import isfinite
1717
import matplotlib.pyplot as plt
1818
from numpy import argmin
19+
from torch.utils.tensorboard import SummaryWriter
1920

2021
from numpy import repeat
2122
from numpy import sqrt
@@ -257,9 +258,8 @@ def __init__(
257258
self.max_surrogate_points = self.fun_control["max_surrogate_points"]
258259
self.progress_file = self.fun_control["progress_file"]
259260

260-
# if the key "spot_writer" is not in the dictionary fun_control,
261-
# set self.spot_writer to None else to the value of the key "spot_writer"
262-
self.spot_writer = self.fun_control.get("spot_writer", None)
261+
# Tensorboard:
262+
self.init_spot_writer()
263263

264264
# Bounds are internal, because they are functions of self.lower and self.upper
265265
# and used by the optimizer:
@@ -846,8 +846,8 @@ def run(self, X_start=None) -> Spot:
846846
# progress bar:
847847
self.show_progress_if_needed(timeout_start)
848848
if self.spot_writer is not None:
849-
writer = self.spot_writer
850-
writer.close()
849+
self.spot_writer.flush()
850+
self.spot_writer.close()
851851
if self.fun_control["db_dict_name"] is not None:
852852
self.write_db_dict()
853853
self.save_experiment()
@@ -929,16 +929,18 @@ def initialize_design(self, X_start=None) -> None:
929929
#
930930
self.counter = self.y.size
931931
if self.spot_writer is not None:
932-
writer = self.spot_writer
932+
# writer = self.spot_writer
933933
# range goes to init_size -1 because the last value is added by update_stats(),
934934
# which always adds the last value.
935935
# Changed in 0.5.9:
936936
for j in range(len(self.y)):
937937
X_j = self.X[j].copy()
938938
y_j = self.y[j].copy()
939939
config = {self.var_name[i]: X_j[i] for i in range(self.k)}
940-
writer.add_hparams(config, {"spot_y": y_j})
941-
writer.flush()
940+
# see: https://github.com/pytorch/pytorch/issues/32651
941+
# self.spot_writer.add_hparams(config, {"spot_y": y_j}, run_name=self.spot_tensorboard_path)
942+
self.spot_writer.add_hparams(config, {"spot_y": y_j})
943+
self.spot_writer.flush()
942944
#
943945
self.X, self.y = remove_nan(self.X, self.y, stop_on_zero_return=True)
944946
logger.debug("In Spot() initialize_design(), final X val, after remove nan: self.X: %s", self.X)
@@ -1257,17 +1259,17 @@ def update_stats(self) -> None:
12571259

12581260
def update_writer(self) -> None:
12591261
if self.spot_writer is not None:
1260-
writer = self.spot_writer
1262+
# writer = self.spot_writer
12611263
# get the last y value:
12621264
y_last = self.y[-1].copy()
12631265
if self.noise is False:
12641266
y_min = self.min_y.copy()
12651267
X_min = self.min_X.copy()
12661268
# y_min: best y value so far
12671269
# y_last: last y value, can be worse than y_min
1268-
writer.add_scalars("spot_y", {"min": y_min, "last": y_last}, self.counter)
1270+
self.spot_writer.add_scalars("spot_y", {"min": y_min, "last": y_last}, self.counter)
12691271
# X_min: X value of the best y value so far
1270-
writer.add_scalars("spot_X", {f"X_{i}": X_min[i] for i in range(self.k)}, self.counter)
1272+
self.spot_writer.add_scalars("spot_X", {f"X_{i}": X_min[i] for i in range(self.k)}, self.counter)
12711273
else:
12721274
# get the last n y values:
12731275
y_last_n = self.y[-self.fun_repeats :].copy()
@@ -1277,23 +1279,25 @@ def update_writer(self) -> None:
12771279
X_min_mean = self.min_mean_X.copy()
12781280
# y_min_var: variance of the min y value so far
12791281
y_min_var = self.min_var_y.copy()
1280-
writer.add_scalar("spot_y_min_var", y_min_var, self.counter)
1282+
self.spot_writer.add_scalar("spot_y_min_var", y_min_var, self.counter)
12811283
# y_min_mean: best mean y value so far (see above)
1282-
writer.add_scalar("spot_y", y_min_mean, self.counter)
1284+
self.spot_writer.add_scalar("spot_y", y_min_mean, self.counter)
12831285
# last n y values (noisy):
1284-
writer.add_scalars(
1286+
self.spot_writer.add_scalars(
12851287
"spot_y", {f"y_last_n{i}": y_last_n[i] for i in range(self.fun_repeats)}, self.counter
12861288
)
12871289
# X_min_mean: X value of the best mean y value so far (see above)
1288-
writer.add_scalars(
1290+
self.spot_writer.add_scalars(
12891291
"spot_X_noise", {f"X_min_mean{i}": X_min_mean[i] for i in range(self.k)}, self.counter
12901292
)
12911293
# get last value of self.X and convert to dict. take the values from self.var_name as keys:
12921294
X_last = self.X[-1].copy()
12931295
config = {self.var_name[i]: X_last[i] for i in range(self.k)}
12941296
# hyperparameters X and value y of the last configuration:
1295-
writer.add_hparams(config, {"spot_y": y_last})
1296-
writer.flush()
1297+
# see: https://github.com/pytorch/pytorch/issues/32651
1298+
# self.spot_writer.add_hparams(config, {"spot_y": y_last}, run_name=self.spot_tensorboard_path)
1299+
self.spot_writer.add_hparams(config, {"spot_y": y_last})
1300+
self.spot_writer.flush()
12971301

12981302
def suggest_new_X(self) -> np.array:
12991303
"""
@@ -2185,3 +2189,13 @@ def save_experiment(self, filename=None) -> None:
21852189
pprint.pprint(spot_tuner)
21862190

21872191
raise e
2192+
2193+
def init_spot_writer(self) -> None:
2194+
"""
2195+
Initialize the spot_writer for the current experiment.
2196+
"""
2197+
self.spot_tensorboard_path = self.fun_control["spot_tensorboard_path"]
2198+
if self.spot_tensorboard_path is not None:
2199+
self.spot_writer = SummaryWriter(log_dir=self.spot_tensorboard_path)
2200+
else:
2201+
self.spot_writer = None

0 commit comments

Comments
 (0)