CSVLogger#

class braintools.trainer.CSVLogger(save_dir, name='logs', version=None, flush_logs_every_n_steps=100)#

CSV file logging backend.

Logs metrics to a CSV file for simple analysis.

Parameters:
  • save_dir (str) – Directory to save CSV logs.

  • name (str) – Experiment name (subdirectory).

  • version (str | None) – Version string.

  • flush_logs_every_n_steps (int) – How often to flush logs to disk.

Examples

>>> import shutil, tempfile
>>> import braintools
>>> tmp = tempfile.mkdtemp()
>>> logger = braintools.trainer.CSVLogger(tmp, name='my_experiment', version='v0')
>>> logger.log_hyperparams({'lr': 0.01})
>>> logger.log_metrics({'loss': 0.5}, step=0)
>>> logger.log_metrics({'loss': 0.25, 'acc': 0.75}, step=1)
>>> logger.finalize()
>>> with open(logger.metrics_file_path) as f:
...     print(f.read().strip())
step,loss,acc
0,0.5,
1,0.25,0.75
>>> shutil.rmtree(tmp)

Pass it to the trainer with Trainer(logger=logger).

finalize(status='success')[source]#

Finalize logging (called at end of training).

Parameters:

status (str) – Final status (‘success’, ‘failed’, ‘interrupted’).

property log_dir: str#

Directory for this specific run’s logs.

log_hyperparams(params)[source]#

Log hyperparameters.

Parameters:

params (Dict[str, Any]) – Dictionary of hyperparameter names to values.

log_metrics(metrics, step=None)[source]#

Log metrics.

Parameters:
  • metrics (Dict[str, float]) – Dictionary of metric names to values.

  • step (int | None) – Global step number.

property root_dir: str#

Root directory for logs.

save()[source]#

Flush buffered metrics to CSV.

If metrics with new column names have appeared since the header was written, the whole file is rewritten with the wider header so that late-appearing metrics are not silently dropped and earlier rows stay column-aligned. (T-14)