Add TensorBoard support (#87)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>pull/89/head^2
parent
248d54ca03
commit
cb4f20f3cf
6 changed files with 133 additions and 51 deletions
@ -0,0 +1,26 @@ |
||||
from torch.utils.tensorboard import SummaryWriter |
||||
|
||||
writer = None # TensorBoard SummaryWriter instance |
||||
|
||||
|
||||
def _log_scalars(scalars, step=0): |
||||
for k, v in scalars.items(): |
||||
writer.add_scalar(k, v, step) |
||||
|
||||
|
||||
def on_train_start(trainer): |
||||
global writer |
||||
writer = SummaryWriter(str(trainer.save_dir)) |
||||
trainer.console.info(f"Logging results to {trainer.save_dir}\n" |
||||
f"Starting training for {trainer.args.epochs} epochs...") |
||||
|
||||
|
||||
def on_batch_end(trainer): |
||||
_log_scalars(trainer.label_loss_items(trainer.tloss, prefix="train"), trainer.epoch) |
||||
|
||||
|
||||
def on_val_end(trainer): |
||||
_log_scalars(trainer.metrics, trainer.epoch) |
||||
|
||||
|
||||
callbacks = {"on_train_start": on_train_start, "on_val_end": on_val_end, "on_batch_end": on_batch_end} |
Loading…
Reference in new issue