1
1
"""
2
2
How to use TensorBoard with PyTorch
3
3
===================================
4
- TensorBoard is a visualization toolkit for machine learning experimentation.
5
- TensorBoard allows tracking and visualizing metrics such as loss and accuracy,
6
- visualizing the model graph, viewing histograms, displaying images and much more.
7
- In this tutorial we are going to cover TensorBoard installation,
4
+ TensorBoard is a visualization toolkit for machine learning experimentation.
5
+ TensorBoard allows tracking and visualizing metrics such as loss and accuracy,
6
+ visualizing the model graph, viewing histograms, displaying images and much more.
7
+ In this tutorial we are going to cover TensorBoard installation,
8
8
basic usage with PyTorch, and how to visualize data you logged in TensorBoard UI.
9
9
10
10
Installation
11
11
----------------------
12
- PyTorch should be installed to log models and metrics into TensorBoard log
13
- directory. The following command will install PyTorch 1.4+ via
14
- Anaconda (recommended):
15
-
16
- .. code-block:: sh
17
-
18
- $ conda install pytorch torchvision -c pytorch
19
-
20
-
21
- or pip
12
+ PyTorch should be installed to log models and metrics into TensorBoard log
13
+ directory. The following command will install PyTorch 1.4+ via
14
+ pip:
22
15
23
16
.. code-block:: sh
24
17
29
22
######################################################################
30
23
# Using TensorBoard in PyTorch
31
24
# -----------------------------
32
- #
33
- # Let’s now try using TensorBoard with PyTorch! Before logging anything,
25
+ #
26
+ # Let’s now try using TensorBoard with PyTorch! Before logging anything,
34
27
# we need to create a ``SummaryWriter`` instance.
35
- #
28
+ #
36
29
37
30
import torch
38
31
from torch .utils .tensorboard import SummaryWriter
32
+
39
33
writer = SummaryWriter ()
40
34
41
35
######################################################################
42
36
# Writer will output to ``./runs/`` directory by default.
43
- #
37
+ #
44
38
45
39
46
40
######################################################################
47
41
# Log scalars
48
42
# -----------
49
- #
50
- # In machine learning, it’s important to understand key metrics such as
51
- # loss and how they change during training. Scalar helps to save
52
- # the loss value of each training step, or the accuracy after each epoch.
53
- #
54
- # To log a scalar value, use
55
- # ``add_scalar(tag, scalar_value, global_step=None, walltime=None)``.
56
- # For example, lets create a simple linear regression training, and
43
+ #
44
+ # In machine learning, it’s important to understand key metrics such as
45
+ # loss and how they change during training. Scalar helps to save
46
+ # the loss value of each training step, or the accuracy after each epoch.
47
+ #
48
+ # To log a scalar value, use
49
+ # ``add_scalar(tag, scalar_value, global_step=None, walltime=None)``.
50
+ # For example, lets create a simple linear regression training, and
57
51
# log loss value using ``add_scalar``
58
52
#
59
53
62
56
63
57
model = torch .nn .Linear (1 , 1 )
64
58
criterion = torch .nn .MSELoss ()
65
- optimizer = torch .optim .SGD (model .parameters (), lr = 0.1 )
59
+ optimizer = torch .optim .SGD (model .parameters (), lr = 0.1 )
60
+
66
61
67
62
def train_model (iter ):
68
63
for epoch in range (iter ):
@@ -72,18 +67,19 @@ def train_model(iter):
72
67
optimizer .zero_grad ()
73
68
loss .backward ()
74
69
optimizer .step ()
75
-
70
+
71
+
76
72
train_model (10 )
77
73
writer .flush ()
78
74
79
75
80
- ######################################################################
81
- # Call ``flush()`` method to make sure that all pending events
76
+ ######################################################################
77
+ # Call ``flush()`` method to make sure that all pending events
82
78
# have been written to disk.
83
- #
84
- # See `torch.utils.tensorboard tutorials <https://pytorch.org/docs/stable/tensorboard.html>`_
79
+ #
80
+ # See `torch.utils.tensorboard tutorials <https://pytorch.org/docs/stable/tensorboard.html>`_
85
81
# to find more TensorBoard visualization types you can log.
86
- #
82
+ #
87
83
# If you do not need the summary writer anymore, call ``close()`` method.
88
84
#
89
85
@@ -92,17 +88,17 @@ def train_model(iter):
92
88
######################################################################
93
89
# Run TensorBoard
94
90
# ----------------
95
- #
91
+ #
96
92
# Install TensorBoard through the command line to visualize data you logged
97
93
#
98
94
# .. code-block:: sh
99
95
#
100
96
# pip install tensorboard
101
97
#
102
98
#
103
- # Now, start TensorBoard, specifying the root log directory you used above.
104
- # Argument ``logdir`` points to directory where TensorBoard will look to find
105
- # event files that it can display. TensorBoard will recursively walk
99
+ # Now, start TensorBoard, specifying the root log directory you used above.
100
+ # Argument ``logdir`` points to directory where TensorBoard will look to find
101
+ # event files that it can display. TensorBoard will recursively walk
106
102
# the directory structure rooted at ``logdir``, looking for ``.*tfevents.*`` files.
107
103
#
108
104
# .. code-block:: sh
@@ -114,17 +110,17 @@ def train_model(iter):
114
110
# .. image:: ../../_static/img/thumbnails/tensorboard_scalars.png
115
111
# :scale: 40 %
116
112
#
117
- # This dashboard shows how the loss and accuracy change with every epoch.
118
- # You can use it to also track training speed, learning rate, and other
119
- # scalar values. It’s helpful to compare these metrics across different
113
+ # This dashboard shows how the loss and accuracy change with every epoch.
114
+ # You can use it to also track training speed, learning rate, and other
115
+ # scalar values. It’s helpful to compare these metrics across different
120
116
# training runs to improve your model.
121
117
#
122
118
123
119
124
120
########################################################################
125
121
# Learn More
126
122
# ----------------------------
127
- #
123
+ #
128
124
# - `torch.utils.tensorboard <https://pytorch.org/docs/stable/tensorboard.html>`_ docs
129
125
# - `Visualizing models, data, and training with TensorBoard <https://pytorch.org/tutorials/intermediate/tensorboard_tutorial.html>`_ tutorial
130
126
#
0 commit comments