0
# TensorBoardX
1
2
TensorBoardX lets you watch Tensors Flow without TensorFlow. A Python library that provides TensorBoard event logging capabilities without requiring TensorFlow, enabling visualization of machine learning experiments through simple function calls. Supports comprehensive visualization types including scalars, images, figures, histograms, audio, text, computational graphs, ONNX graphs, embeddings, precision-recall curves, 3D meshes, hyperparameters, and videos.
3
4
## Package Information
5
6
- **Package Name**: tensorboardX
7
- **Language**: Python
8
- **Installation**: `pip install tensorboardX`
9
- **Optional Performance**: `pip install crc32c` (for speed), `pip install soundfile` (for audio)
10
11
## Core Imports
12
13
```python
14
from tensorboardX import SummaryWriter
15
```
16
17
Multiple classes available:
18
19
```python
20
from tensorboardX import (
21
SummaryWriter, # Main writer class
22
FileWriter, # Low-level protocol buffer writer
23
GlobalSummaryWriter, # Thread-safe writer with auto-incrementing steps
24
RecordWriter, # Low-level TensorFlow record format writer
25
TorchVis # Multi-backend visualization (TensorBoard + Visdom)
26
)
27
```
28
29
## Basic Usage
30
31
```python
32
from tensorboardX import SummaryWriter
33
import numpy as np
34
35
# Create a SummaryWriter
36
writer = SummaryWriter('runs/experiment_1')
37
38
# Log scalar values
39
for step in range(100):
40
loss = np.random.random()
41
accuracy = np.random.random()
42
43
writer.add_scalar('Loss/Train', loss, step)
44
writer.add_scalar('Accuracy/Train', accuracy, step)
45
46
# Log images, histograms, and other data types
47
dummy_img = np.random.rand(3, 64, 64) # CHW format
48
writer.add_image('Sample_Image', dummy_img, step)
49
50
# Close the writer
51
writer.close()
52
53
# View in TensorBoard
54
# tensorboard --logdir=runs
55
```
56
57
## Architecture
58
59
TensorBoardX provides multiple writer classes with different capabilities:
60
61
- **SummaryWriter**: High-level API for all visualization types with automatic event file management
62
- **FileWriter**: Lower-level protocol buffer writer for direct event file control
63
- **GlobalSummaryWriter**: Thread-safe writer with automatic step incrementing for concurrent environments
64
- **RecordWriter**: Basic TensorFlow record format writer for custom event handling
65
- **TorchVis**: Multi-backend wrapper supporting both TensorBoard and Visdom simultaneously
66
67
The library integrates seamlessly with PyTorch, NumPy, and other ML frameworks while maintaining compatibility with TensorBoard's visualization ecosystem.
68
69
## Capabilities
70
71
### Summary Writer
72
73
Main high-level writer class providing comprehensive logging capabilities for scalars, media, graphs, embeddings, and advanced visualizations with automatic file management and flexible configuration options.
74
75
```python { .api }
76
class SummaryWriter:
77
def __init__(
78
self,
79
logdir: Optional[str] = None,
80
comment: Optional[str] = "",
81
purge_step: Optional[int] = None,
82
max_queue: Optional[int] = 10,
83
flush_secs: Optional[int] = 120,
84
filename_suffix: Optional[str] = '',
85
write_to_disk: Optional[bool] = True,
86
log_dir: Optional[str] = None,
87
comet_config: Optional[dict] = {"disabled": True}
88
): ...
89
90
def add_scalar(self, tag: str, scalar_value, global_step: Optional[int] = None, walltime: Optional[float] = None): ...
91
def add_image(self, tag: str, img_tensor, global_step: Optional[int] = None, walltime: Optional[float] = None): ...
92
def add_graph(self, model, input_to_model=None, verbose=False): ...
93
def close(self): ...
94
```
95
96
[Summary Writer](./summary-writer.md)
97
98
### File Writer
99
100
Low-level protocol buffer writer for direct event file control, providing fine-grained management of TensorBoard event files with manual summary and event handling.
101
102
```python { .api }
103
class FileWriter:
104
def __init__(self, logdir: str, max_queue: int = 10, flush_secs: int = 120, filename_suffix: str = ''): ...
105
106
def add_event(self, event, step=None, walltime=None): ...
107
def add_summary(self, summary, global_step=None, walltime=None): ...
108
def get_logdir(self) -> str: ...
109
def flush(self): ...
110
def close(self): ...
111
```
112
113
[File Writer](./file-writer.md)
114
115
### Global Writer
116
117
Thread-safe writer with automatic step incrementing for concurrent logging across processes, simplifying multi-threaded experiment tracking without manual step management.
118
119
```python { .api }
120
class GlobalSummaryWriter:
121
def __init__(
122
self,
123
logdir: Optional[str] = None,
124
comment: str = '',
125
purge_step: Optional[int] = None,
126
max_queue: int = 10,
127
flush_secs: int = 120,
128
filename_suffix: str = '',
129
write_to_disk: bool = True,
130
log_dir: Optional[str] = None,
131
coalesce_process: bool = True
132
): ...
133
134
def add_scalar(self, tag: str, scalar_value, walltime: Optional[float] = None): ...
135
def add_image(self, tag: str, img_tensor, walltime: Optional[float] = None): ...
136
@staticmethod
137
def getSummaryWriter() -> 'GlobalSummaryWriter': ...
138
```
139
140
[Global Writer](./global-writer.md)
141
142
### Multi-Backend Visualization
143
144
Multi-backend visualization class supporting both TensorBoard and Visdom simultaneously, enabling cross-platform experiment monitoring and visualization comparison.
145
146
```python { .api }
147
class TorchVis:
148
def __init__(self, *args, **init_kwargs): ...
149
150
def register(self, *args, **init_kwargs): ...
151
def unregister(self, *args): ...
152
# All SummaryWriter methods available through dynamic proxy
153
```
154
155
[Multi-Backend Visualization](./torchvis.md)
156
157
### Record Writer
158
159
Low-level TensorFlow record format writer for direct binary record writing with support for local filesystem, Amazon S3, and Google Cloud Storage backends.
160
161
```python { .api }
162
class RecordWriter:
163
def __init__(self, path: str): ...
164
165
def write(self, data: bytes): ...
166
def flush(self): ...
167
def close(self): ...
168
```
169
170
[Record Writer](./record-writer.md)
171
172
### Utility Functions
173
174
Data conversion and tensor processing utilities for preparing data for TensorBoard visualization, including format conversion, figure handling, and tensor manipulation.
175
176
```python { .api }
177
def figure_to_image(figures, close: bool = True): ...
178
def make_np(x): ...
179
def convert_to_HWC(tensor, input_format: str): ...
180
def convert_to_NTCHW(tensor, input_format: str): ...
181
```
182
183
[Utility Functions](./utilities.md)