Example 9.4 (Comparing Multiple Runs) Run multiple optimizations with different settings:
# Run 1: Standardopt1 = SpotOptim(..., tensorboard_path="runs/standard")opt1.optimize()# Run 2: With OCBAopt2 = SpotOptim(..., ocba_delta=3, tensorboard_path="runs/with_ocba")opt2.optimize()# Run 3: More initial pointsopt3 = SpotOptim(..., n_initial=20, tensorboard_path="runs/more_initial")opt3.optimize()
Then view all runs together:
tensorboard--logdir=runs
9.2 TensorBoard Features
9.2.1 SCALARS Tab
View convergence curves
Compare optimization progress across runs
Track how metrics change over iterations
9.2.2 HPARAMS Tab
Explore hyperparameter space
See which parameter combinations work best
Identify patterns in successful configurations
9.2.3 Text Tab
View configuration details
Check run metadata
TipTips
Organize Experiments: Use descriptive tensorboard_path names:
tensorboard_path=f"runs/exp_{date}_{config_name}"
Compare Algorithms: Run multiple optimization strategies and compare:
# Different acquisition functionsfor acq in ['ei', 'pi', 'y']: opt = SpotOptim(..., acquisition=acq, tensorboard_path=f"runs/acq_{acq}") opt.optimize()
Clean Up Old Runs: Use tensorboard_clean=True for automatic cleanup, or manually:
rm-rf runs/old_experiment
Port Conflicts: If port 6006 is busy, use a different port:
tensorboard--logdir=runs --port=6007
9.3 TensorBoard Log Cleaning Feature in SpotOptim
Automatic cleaning of old TensorBoard log directories with the tensorboard_clean parameter.
9.3.1 Basic Usage
import numpy as npfrom spotoptim import SpotOptimdef sphere(X):"""Simple sphere function"""return np.sum(X**2, axis=1)# Remove old logs and create new log directoryoptimizer = SpotOptim( fun=sphere, bounds=[(-5, 5), (-5, 5)], max_iter=20, n_initial=10, tensorboard_log=True, tensorboard_clean=True, # Removes all subdirectories in 'runs' verbose=True, seed=42)result = optimizer.optimize()print(f"Best value: {result.fun:.6f}")print(f"Logs saved to: runs/{optimizer.tensorboard_path}")
Removed old TensorBoard logs: runs/spotoptim_20251220_100850
Cleaned 1 old TensorBoard log directory
TensorBoard logging enabled: runs/spotoptim_20251220_100850
Initial best: f(x) = 2.420807
Iteration 1: New best f(x) = 2.390307
Iteration 2: New best f(x) = 1.813139
Iteration 3: New best f(x) = 0.009007
Iteration 4: New best f(x) = 0.001900
Iteration 5: New best f(x) = 0.000973
Iteration 6: New best f(x) = 0.000001
Iteration 7: New best f(x) = 0.000001
Iteration 8: f(x) = 0.000001
Iteration 9: New best f(x) = 0.000001
Iteration 10: f(x) = 0.000001
TensorBoard writer closed. View logs with: tensorboard --logdir=runs/spotoptim_20251220_100850
Best value: 0.000001
Logs saved to: runs/runs/spotoptim_20251220_100850
9.3.2 Use Cases
tensorboard_log
tensorboard_clean
Behavior
True
True
Clean old logs, create new log directory
True
False
Preserve old logs, create new log directory
False
True
Clean old logs, no new logging
False
False
No logging, no cleaning (default)
9.3.3 Implementation Details
9.3.3.1 Cleaning Method
def _clean_tensorboard_logs(self) ->None:"""Clean old TensorBoard log directories from the runs folder."""ifself.tensorboard_clean: runs_dir ="runs"if os.path.exists(runs_dir) and os.path.isdir(runs_dir):# Get all subdirectories in runs subdirs = [ os.path.join(runs_dir, d)for d in os.listdir(runs_dir)if os.path.isdir(os.path.join(runs_dir, d)) ]# Remove each subdirectoryfor subdir in subdirs:try: shutil.rmtree(subdir)ifself.verbose:print(f"Removed old TensorBoard logs: {subdir}")exceptExceptionas e:ifself.verbose:print(f"Warning: Could not remove {subdir}: {e}")
9.3.4 Execution Flow
User creates SpotOptim instance with tensorboard_clean=True
During initialization, _clean_tensorboard_logs() is called
Method checks if ‘runs’ directory exists
Removes all subdirectories (but preserves files)
If tensorboard_log=True, a new log directory is created
Optimization proceeds normally
9.4 Safety Features
Only removes directories, not files in ‘runs’ folder
Handles missing ‘runs’ directory gracefully
Error handling for permission issues
Verbose output shows what’s being removed
Default is False to prevent accidental deletion
WarningWarning
Setting tensorboard_clean=True permanently deletes all subdirectories in the ‘runs’ folder. Make sure to save important logs elsewhere before enabling this feature.
9.5 Tensorboard Demo Scripts
Run the comprehensive TensorBoard demo:
python demo_tensorboard.py
This demonstrates:
Deterministic optimization (Rosenbrock function)
Noisy optimization with repeated evaluations
OCBA for intelligent re-evaluation
Run the log cleaning demo:
python demo_tensorboard_clean.py
This demonstrates:
Creating multiple log directories
Preserving old logs (default behavior)
Cleaning old logs automatically
Cleaning without creating new logs
9.6 Troubleshooting
Q: TensorBoard shows “No dashboards are active” A: Make sure you’ve run an optimization with tensorboard_log=True first.
Q: Can’t see my latest run A: Refresh TensorBoard (click the reload button in the upper right).
Q: How do I stop TensorBoard? A: Press Ctrl+C in the terminal where TensorBoard is running.
Q: Logs taking up too much space? A: Use tensorboard_clean=True to automatically remove old logs, or manually delete old run directories.
Q: How do I remove all old logs at once? A: Set tensorboard_clean=True when creating your optimizer. This will remove all subdirectories in the runs folder.