우선, pytorch를 사용하는 환경과 동일한 위치에서 텐서보드를 설치해준다.
# 필자는 pytorch를 구동하기 위해 따로 가상환경을 만들어서 해당 위치에 설치
conda activate virtual_environment
# pip을 활용하여 tensorboard를 설치
pip install tensorboard
(Ref Link : https://pytorch.org/tutorials/recipes/recipes/tensorboard_with_pytorch.html)
How to use TensorBoard with PyTorch — PyTorch Tutorials 1.12.1+cu102 documentation
Note Click here to download the full example code How to use TensorBoard with PyTorch TensorBoard is a visualization toolkit for machine learning experimentation. TensorBoard allows tracking and visualizing metrics such as loss and accuracy, visualizing th
pytorch.org
이후, Jupyter Lab을 실행시키고, 아래의 코드가 정상적으로 돌아가면 설치가 잘 된 것이다.
from torch.utils.tensorboard import SummaryWriter
# model이 학습되는 log들을 writer변수에 담을 예정
writer = SummaryWriter()
PyTorch Tutorial에 따르면, 스칼라(Scalar)값을 로그로 남기기 위해서는 writer에 내장되어 있는 addscalar함수를 사용하면 된다. 적용된 코드는 아래와 같다.
x = torch.arange(-5, 5, 0.1).view(-1, 1)
y = -5 * x + 0.1 * torch.randn(x.size())
model = torch.nn.Linear(1, 1)
criterion = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr = 0.1)
def train_model(iter):
for epoch in range(iter):
y1 = model(x)
loss = criterion(y1, y)
writer.add_scalar("Loss/train", loss, epoch)
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_model(10)
writer.flush()
- scalar값뿐만 아니라 histogram, image, figure, audio, video, text, graph, embedding, pr_curve, mesh, hparams도 가능하다. (ref : https://pytorch.org/docs/stable/tensorboard.html#module-torch.utils.tensorboard)
torch.utils.tensorboard — PyTorch 1.13 documentation
torch.utils.tensorboard Before going further, more details on TensorBoard can be found at https://www.tensorflow.org/tensorboard/ Once you’ve installed TensorBoard, these utilities let you log PyTorch models and metrics into a directory for visualization
pytorch.org
그리고 해당 커맨드를 터미널에서 실행하고, http://localhost:6006 링크로 들어가면 TensorBoard가 아래의 이미지처럼 뜬다.
tensorboard --logdir=runs

기타 예시들
https://pytorch.org/tutorials/intermediate/tensorboard_tutorial.html
Visualizing Models, Data, and Training with TensorBoard — PyTorch Tutorials 1.13.0+cu117 documentation
Visualizing Models, Data, and Training with TensorBoard In the 60 Minute Blitz, we show you how to load in data, feed it through a model we define as a subclass of nn.Module, train this model on training data, and test it on test data. To see what’s happ
pytorch.org
How to create a confusion matrix with TensorBoard and PyTorch
This article shows how you can create a confusion matrix with Tensorboard and Pytorch
christianbernecker.medium.com
https://stackoverflow.com/questions/48951136/plot-multiple-graphs-in-one-plot-using-tensorboard
Plot multiple graphs in one plot using Tensorboard
I am using Keras with Tensorflow backend. My work involves comparing the performances of several models such as Inception, VGG, Resnet etc on my dataset. I would like to plot the training accuracie...
stackoverflow.com


'Programming > python-visualization' 카테고리의 다른 글
Matplotlib 그래프 시각화 방법 - 두 그래프 평행하게 그리기 (tensorflow, numpy 수치데이터) (0) | 2023.09.18 |
---|