9  TensorBoard Logging in SpotOptim

SpotOptim supports TensorBoard logging for monitoring optimization progress in real-time.

9.1 Quick Start

9.1.1 Enable TensorBoard Logging

from spotoptim import SpotOptim
import numpy as np

def sphere(X):
    return np.sum(X**2, axis=1)

optimizer = SpotOptim(
    fun=sphere,
    bounds=[(-5, 5), (-5, 5)],
    max_iter=20,
    n_initial=10,
    tensorboard_log=True,  # Enable logging
    tensorboard_clean=True,
    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_100541
Cleaned 1 old TensorBoard log directory
TensorBoard logging enabled: runs/spotoptim_20251220_100843
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_100843
Best value: 0.000001
Logs saved to: runs/runs/spotoptim_20251220_100843

9.1.2 View Logs in TensorBoard

In a separate terminal, run:

tensorboard --logdir=runs

Then open your browser to http://localhost:6006

9.1.3 Cleaning Old Logs

You can automatically remove old TensorBoard logs before starting a new optimization:

optimizer = SpotOptim(
    fun=objective,
    bounds=[(-5, 5), (-5, 5)],
    tensorboard_log=True,
    tensorboard_clean=True,  # Remove old logs from 'runs' directory
    verbose=True
)
NoteWarning

This permanently deletes all subdirectories in the runs folder. Make sure to save important logs elsewhere before enabling this feature.

9.1.4 Use Cases

  1. Clean Start - Remove old logs and create new one:

    tensorboard_log=True, tensorboard_clean=True
  2. Preserve History - Keep old logs and add new one (default):

    tensorboard_log=True, tensorboard_clean=False
  3. Just Clean - Remove old logs without new logging:

    tensorboard_log=False, tensorboard_clean=True

9.1.5 Custom Log Directory

Specify a custom path for TensorBoard logs:

optimizer = SpotOptim(
    fun=objective,
    bounds=[(-5, 5), (-5, 5)],
    tensorboard_log=True,
    tensorboard_path="my_experiments/run_001",
    ...
)

9.1.6 What Gets Logged

9.1.6.1 Scalar Metrics

For Deterministic Functions:

  • y_values/min: Best (minimum) y value found so far
  • y_values/last: Most recently evaluated y value
  • X_best/x0, X_best/x1, ...: Coordinates of the best point

For Noisy Functions (repeats > 1):

  • y_values/min: Best single evaluation
  • y_values/mean_best: Best mean y value
  • y_values/last: Most recent evaluation
  • y_variance_at_best: Variance at the best mean point
  • X_mean_best/x0, X_mean_best/x1, ...: Coordinates of best mean point

9.1.6.2 Hyperparameters

Each function evaluation is logged with:

  • Input coordinates (x0, x1, x2, …)
  • Function value (hp_metric)

This allows you to explore the relationship between hyperparameters and objective values in the HPARAMS tab.

9.1.7 Examples

Example 9.1 (Basic Tensorboard Usage)  

import numpy as np
from spotoptim import SpotOptim

optimizer = SpotOptim(
    fun=lambda X: np.sum(X**2, axis=1),
    bounds=[(-5, 5), (-5, 5)],
    max_iter=20,
    n_initial=10,
    tensorboard_log=True,
    tensorboard_clean=True,
    verbose=True,
    seed=42
)
result = optimizer.optimize()
print(f"Best value: {result.fun:.6f}")
Removed old TensorBoard logs: runs/spotoptim_20251220_100843
Cleaned 1 old TensorBoard log directory
TensorBoard logging enabled: runs/spotoptim_20251220_100847
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_100847
Best value: 0.000001

Example 9.2 (Noisy Optimization)  

import numpy as np
from spotoptim import SpotOptim

def noisy_objective(X):
    base = np.sum(X**2, axis=1)
    noise = np.random.normal(0, 0.1, size=base.shape)
    return base + noise

optimizer = SpotOptim(
    fun=noisy_objective,
    bounds=[(-5, 5), (-5, 5)],
    max_iter=20,
    n_initial=10,
    repeats_initial=3,
    repeats_surrogate=2,
    tensorboard_log=True,
    tensorboard_clean=True,
    tensorboard_path="runs/noisy_exp",
    seed=42
)
result = optimizer.optimize()
print(f"Best value: {result.fun:.6f}")
Best value: 2.277589

Example 9.3 (With OCBA)  

import numpy as np
from spotoptim import SpotOptim

def noisy_objective(X):
    base = np.sum(X**2, axis=1)
    noise = np.random.normal(0, 0.1, size=base.shape)
    return base + noise

optimizer = SpotOptim(
    fun=noisy_objective,
    bounds=[(-5, 5), (-5, 5)],
    max_iter=20,
    n_initial=10,
    repeats_initial=2,
    ocba_delta=3,  # Re-evaluate 3 promising points per iteration
    tensorboard_log=True,
    tensorboard_clean=True,
    seed=42
)
result = optimizer.optimize()
print(f"Best value: {result.fun:.6f}")
Best value: 2.464723

Example 9.4 (Comparing Multiple Runs) Run multiple optimizations with different settings:

# Run 1: Standard
opt1 = SpotOptim(..., tensorboard_path="runs/standard")
opt1.optimize()

# Run 2: With OCBA
opt2 = SpotOptim(..., ocba_delta=3, tensorboard_path="runs/with_ocba")
opt2.optimize()

# Run 3: More initial points
opt3 = 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
  1. Organize Experiments: Use descriptive tensorboard_path names:

    tensorboard_path=f"runs/exp_{date}_{config_name}"
  2. Compare Algorithms: Run multiple optimization strategies and compare:

    # Different acquisition functions
    for acq in ['ei', 'pi', 'y']:
        opt = SpotOptim(..., acquisition=acq, tensorboard_path=f"runs/acq_{acq}")
        opt.optimize()
  3. Clean Up Old Runs: Use tensorboard_clean=True for automatic cleanup, or manually:

    rm -rf runs/old_experiment
  4. 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 np
from spotoptim import SpotOptim

def sphere(X):
    """Simple sphere function"""
    return np.sum(X**2, axis=1)

# Remove old logs and create new log directory
optimizer = 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."""
    if self.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 subdirectory
            for subdir in subdirs:
                try:
                    shutil.rmtree(subdir)
                    if self.verbose:
                        print(f"Removed old TensorBoard logs: {subdir}")
                except Exception as e:
                    if self.verbose:
                        print(f"Warning: Could not remove {subdir}: {e}")

9.3.4 Execution Flow

  1. User creates SpotOptim instance with tensorboard_clean=True
  2. During initialization, _clean_tensorboard_logs() is called
  3. Method checks if ‘runs’ directory exists
  4. Removes all subdirectories (but preserves files)
  5. If tensorboard_log=True, a new log directory is created
  6. 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.

9.8 Performance Notes

TensorBoard logging has minimal overhead:

  • Event files are efficiently buffered and written
  • Writer is properly closed after optimization completes

9.9 Jupyter Notebook

Note