-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtensorboard_utils.py
More file actions
56 lines (46 loc) · 1.82 KB
/
tensorboard_utils.py
File metadata and controls
56 lines (46 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# TensorBoard logging utilities for experiments
# Usage:
# from tensorboard_utils import TBLogger
# logger = TBLogger(output_dir, "exp_name")
# logger.log(step, loss=0.5, train_acc=0.8, test_acc=0.7)
# logger.close()
import os
try:
from torch.utils.tensorboard import SummaryWriter
HAS_TENSORBOARD = True
except ImportError:
HAS_TENSORBOARD = False
class TBLogger:
"""Simple TensorBoard logger that gracefully handles missing tensorboard."""
def __init__(self, output_dir, experiment_name):
self.writer = None
if HAS_TENSORBOARD:
log_dir = os.path.join(output_dir, "runs", experiment_name)
self.writer = SummaryWriter(log_dir=log_dir)
print(f"TensorBoard logging to: {log_dir}")
else:
print("TensorBoard not installed, skipping TB logging")
def log(self, step, **kwargs):
"""Log metrics to TensorBoard.
Common kwargs:
loss: training loss
train_acc: training accuracy (0-1 or 0-100, logged as-is)
test_acc: test accuracy
Any kwarg is logged as a scalar with name based on key.
"""
if self.writer is None:
return
for key, value in kwargs.items():
if value is not None:
# Convert common names to TensorBoard conventions
if key == "loss":
self.writer.add_scalar("Loss/train", value, step)
elif key == "train_acc":
self.writer.add_scalar("Accuracy/train", value, step)
elif key == "test_acc":
self.writer.add_scalar("Accuracy/test", value, step)
else:
self.writer.add_scalar(key, value, step)
def close(self):
if self.writer is not None:
self.writer.close()