diff --git a/beginner_source/introyt/tensorboardyt_tutorial.py b/beginner_source/introyt/tensorboardyt_tutorial.py index 80bac7bcb..19bbb2708 100644 --- a/beginner_source/introyt/tensorboardyt_tutorial.py +++ b/beginner_source/introyt/tensorboardyt_tutorial.py @@ -7,10 +7,12 @@ `Training Models `_ || `Model Understanding `_ -PyTorch TensorBoard Support +PyTorch TensorBoard 지원 =========================== -Follow along with the video below or on `youtube `__. +**번역**: `박정은 `_ + +아래 영상이나 `youtube `_\를 참고하세요. .. raw:: html @@ -18,77 +20,77 @@ -Before You Start +시작하기에 앞서 ---------------- -To run this tutorial, you’ll need to install PyTorch, TorchVision, -Matplotlib, and TensorBoard. +이 튜토리얼을 실행하기 위해서 PyTorch, TorchVision, +Matplotlib 그리고 TensorBoard를 설치해야 합니다. -With ``conda``: +``conda`` 사용 시: .. code-block:: sh conda install pytorch torchvision -c pytorch conda install matplotlib tensorboard -With ``pip``: +``pip`` 사용 시: .. code-block:: sh pip install torch torchvision matplotlib tensorboard -Once the dependencies are installed, restart this notebook in the Python -environment where you installed them. +한번 의존성이 있는 모듈을 설치하고 나서, +설치한 환경에서 이 notebook을 다시 시작합니다. -Introduction +개요 ------------ -In this notebook, we’ll be training a variant of LeNet-5 against the -Fashion-MNIST dataset. Fashion-MNIST is a set of image tiles depicting -various garments, with ten class labels indicating the type of garment -depicted. +이 notebook에서는 변형된 LeNet-5를 +Fashion-MNIST 데이터셋으로 학습시킬 것입니다. +Fashion-MNIST는 의복의 종류를 나타내는 10개의 클래스 레이블을 포함하는 +다양한 의류의 타일 이미지 세트입니다. """ -# PyTorch model and training necessities +# PyTorch 모델과 훈련 필수 요소 import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim -# Image datasets and image manipulation +# 이미지 데이터셋과 이미지 조작 import torchvision import torchvision.transforms as transforms -# Image display +# 이미지 시각화 import matplotlib.pyplot as plt import numpy as np -# PyTorch TensorBoard support +# PyTorch TensorBoard 지원 from torch.utils.tensorboard import SummaryWriter -# In case you are using an environment that has TensorFlow installed, -# such as Google Colab, uncomment the following code to avoid -# a bug with saving embeddings to your TensorBoard directory +# 만약 Google Colab처럼 TensorFlow가 설치된 환경을 사용 중이라면 +# 아래의 코드를 주석 해제하여 +# TensorBoard 디렉터리에 임베딩을 저장할 때의 버그를 방지하세요. # import tensorflow as tf # import tensorboard as tb # tf.io.gfile = tb.compat.tensorflow_stub.io.gfile ###################################################################### -# Showing Images in TensorBoard +# TensorBoard에서 이미지 나타내기 # ----------------------------- # -# Let’s start by adding sample images from our dataset to TensorBoard: +# 먼저, 데이터셋에서 TensorBoard로 샘플 이미지를 추가합니다: # -# Gather datasets and prepare them for consumption +# 데이터셋을 모아서 사용 가능하도록 준비하기 transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]) -# Store separate training and validations splits in ./data +# 훈련과 검증으로 분할하여 각각 ./data에 저장하기 training_set = torchvision.datasets.FashionMNIST('./data', download=True, train=True, @@ -109,64 +111,64 @@ shuffle=False, num_workers=2) -# Class labels +# 클래스 레이블 classes = ('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle Boot') -# Helper function for inline image display +# 인라인 이미지 시각화를 위한 함수 def matplotlib_imshow(img, one_channel=False): if one_channel: img = img.mean(dim=0) - img = img / 2 + 0.5 # unnormalize + img = img / 2 + 0.5 # 비정규화(unnormalize) npimg = img.numpy() if one_channel: plt.imshow(npimg, cmap="Greys") else: plt.imshow(np.transpose(npimg, (1, 2, 0))) -# Extract a batch of 4 images +# 4개의 이미지로부터 배치 하나를 추출하기 dataiter = iter(training_loader) images, labels = next(dataiter) -# Create a grid from the images and show them +# 이미지를 나타내기 위한 격자 생성 img_grid = torchvision.utils.make_grid(images) matplotlib_imshow(img_grid, one_channel=True) ######################################################################## -# Above, we used TorchVision and Matplotlib to create a visual grid of a -# minibatch of our input data. Below, we use the ``add_image()`` call on -# ``SummaryWriter`` to log the image for consumption by TensorBoard, and -# we also call ``flush()`` to make sure it’s written to disk right away. +# 위에서 TorchVision과 Matplotlib을 사용하여 +# 입력 데이터의 미니 배치를 시각적으로 배열한 격자를 만들었습니다. 아래에서는 TensorBoard에서 사용될 +# 이미지를 기록하기 위해 ``SummaryWriter`` 의 ``add_image()`` 를 호출하고, +# 또한 ``flush()`` 를 호출하여 이미지가 즉시 디스크에 기록되도록 합니다. # -# Default log_dir argument is "runs" - but it's good to be specific -# torch.utils.tensorboard.SummaryWriter is imported above +# log_dir 인수 기본값은 "runs"입니다 - 하지만 구체적으로 정하는 것이 좋습니다. +# 위에서 torch.utils.tensorboard.SummaryWriter를 가져왔습니다. writer = SummaryWriter('runs/fashion_mnist_experiment_1') -# Write image data to TensorBoard log dir +# TensorBoard 로그 디렉터리에 이미지 데이터 쓰기(write) writer.add_image('Four Fashion-MNIST Images', img_grid) writer.flush() -# To view, start TensorBoard on the command line with: +# 눈으로 보기 위해서는 커맨드 라인에서 TensorBoard를 시작하세요: # tensorboard --logdir=runs -# ...and open a browser tab to http://localhost:6006/ +# ...그런 다음 브라우저에서 http://localhost:6006/ 를 열어보세요. ########################################################################## -# If you start TensorBoard at the command line and open it in a new -# browser tab (usually at `localhost:6006 `__), you should -# see the image grid under the IMAGES tab. +# 만약 TensorBoard를 커맨드 라인에서 구동시켜 +# 그것을 새 브라우저 탭(보통 `localhost:6006 `__)에서 열었다면, +# IMAGES 탭에서 이미지 격자를 확인할 수 있을 것입니다. # -# Graphing Scalars to Visualize Training +# 훈련 시각화를 위한 스칼라 그래프 그리기 # -------------------------------------- # -# TensorBoard is useful for tracking the progress and efficacy of your -# training. Below, we’ll run a training loop, track some metrics, and save -# the data for TensorBoard’s consumption. +# TensorBoard는 훈련 진행 과정과 효과를 추적하기에 +# 유용합니다. 아래에서 훈련 루프를 실행하고 몇몇 지표를 추적하며 +# TensorBoard에서 사용할 데이터를 저장할 것입니다. # -# Let’s define a model to categorize our image tiles, and an optimizer and -# loss function for training: +# 이미지 타일을 분류할 모델과 옵티마이저 +# 그리고 훈련의 손실 함수를 정의해 봅시다: # class Net(nn.Module): @@ -195,16 +197,16 @@ def forward(self, x): ########################################################################## -# Now let’s train a single epoch, and evaluate the training vs. validation -# set losses every 1000 batches: +# 이제 단일 에폭을 훈련하고, 매 1000 배치마다 훈련 셋과 검증 셋의 +# 손실을 평가해 봅니다: # print(len(validation_loader)) -for epoch in range(1): # loop over the dataset multiple times +for epoch in range(1): # 데이터 셋을 여러 번 반복(필요 시 횟수를 조정합니다.) running_loss = 0.0 for i, data in enumerate(training_loader, 0): - # basic training loop + # 기본 훈련 루프 inputs, labels = data optimizer.zero_grad() outputs = net(inputs) @@ -213,24 +215,24 @@ def forward(self, x): optimizer.step() running_loss += loss.item() - if i % 1000 == 999: # Every 1000 mini-batches... + if i % 1000 == 999: # 매 1000 미니 배치마다... print('Batch {}'.format(i + 1)) - # Check against the validation set + # 검증 셋과 비교 running_vloss = 0.0 - # In evaluation mode some model specific operations can be omitted eg. dropout layer - net.train(False) # Switching to evaluation mode, eg. turning off regularisation + # 평가 모드에서는 일부 모델의 특정 작업을 생략할 수 있습니다 예시: 드롭아웃 레이어 + net.train(False) # 평가 모드로 전환, 예시: 정규화(regularisation) 끄기 for j, vdata in enumerate(validation_loader, 0): vinputs, vlabels = vdata voutputs = net(vinputs) vloss = criterion(voutputs, vlabels) running_vloss += vloss.item() - net.train(True) # Switching back to training mode, eg. turning on regularisation + net.train(True) # 훈련 모드로 돌아가기, 예시: 정규화 켜기 avg_loss = running_loss / 1000 avg_vloss = running_vloss / len(validation_loader) - # Log the running loss averaged per batch + # 배치별 평균 실행 손실을 기록 writer.add_scalars('Training vs. Validation Loss', { 'Training' : avg_loss, 'Validation' : avg_vloss }, epoch * len(training_loader) + i) @@ -242,59 +244,59 @@ def forward(self, x): ######################################################################### -# Switch to your open TensorBoard and have a look at the SCALARS tab. +# 열린 TensorBoard로 전환하여 SCALARS탭을 살펴보세요. # -# Visualizing Your Model +# 모델 시각화하기 # ---------------------- # -# TensorBoard can also be used to examine the data flow within your model. -# To do this, call the ``add_graph()`` method with a model and sample -# input: +# TensorBoard는 모델 내 데이터 흐름을 검사하는 데에도 유용합니다. +# 이를 위해, 모델과 샘플 입력을 이용해 ``add_graph()`` 메소드를 +# 호출합니다: # -# Again, grab a single mini-batch of images +# 다시, 이미지의 미니 배치 하나를 가져옵니다. dataiter = iter(training_loader) images, labels = next(dataiter) -# add_graph() will trace the sample input through your model, -# and render it as a graph. +# add_graph()는 샘플 입력이 모델을 통과하는 과정을 추적하고, +# 이를 그래프로 시각화합니다. writer.add_graph(net, images) writer.flush() ######################################################################### -# When you switch over to TensorBoard, you should see a GRAPHS tab. -# Double-click the “NET” node to see the layers and data flow within your -# model. +# TensorBoard로 전환하면, GRAPHS 탭이 보일 것입니다. +# “NET” 노드를 더블 클릭하여 모델 내 계층과 데이터 흐름을 +# 확인하세요. # -# Visualizing Your Dataset with Embeddings +# 임베딩으로 데이터셋 시각화하기 # ---------------------------------------- # -# The 28-by-28 image tiles we’re using can be modeled as 784-dimensional -# vectors (28 \* 28 = 784). It can be instructive to project this to a -# lower-dimensional representation. The ``add_embedding()`` method will -# project a set of data onto the three dimensions with highest variance, -# and display them as an interactive 3D chart. The ``add_embedding()`` -# method does this automatically by projecting to the three dimensions -# with highest variance. +# 우리가 사용하는 28x28 이미지 타일은 784차원의 +# 벡터(28 \* 28 = 784)가 될 수 있습니다. 더 낮은 차원으로 투영하는 쪽이 +# 유리할 수 있습니다. ``add_embedding()`` 메소드는 +# 가장 분산이 높은 세 차원으로 데이터 세트를 투영하고, +# 상호작용 가능한 3D 차트로 시각화해 줄 것입니다. ``add_embedding()`` +# 메소드는 가장 높은 분산을 가진 세 차원에 자동적으로 +# 투영하여 이를 수행합니다. # -# Below, we’ll take a sample of our data, and generate such an embedding: +# 아래에서 데이터 샘플을 가져와 임베딩을 생성할 것입니다: # -# Select a random subset of data and corresponding labels +# 데이터의 랜덤 부분집합과 대응하는 레이블을 선택 def select_n_random(data, labels, n=100): assert len(data) == len(labels) perm = torch.randperm(len(data)) return data[perm][:n], labels[perm][:n] -# Extract a random subset of data +# 데이터의 랜덤 부분집합 추출 images, labels = select_n_random(training_set.data, training_set.targets) -# get the class labels for each image +# 각 이미지별 클래스 레이블 얻기(get) class_labels = [classes[label] for label in labels] -# log embeddings +# 로그 임베딩 features = images.view(-1, 28 * 28) writer.add_embedding(features, metadata=class_labels, @@ -304,24 +306,24 @@ def select_n_random(data, labels, n=100): ####################################################################### -# Now if you switch to TensorBoard and select the PROJECTOR tab, you -# should see a 3D representation of the projection. You can rotate and -# zoom the model. Examine it at large and small scales, and see whether -# you can spot patterns in the projected data and the clustering of -# labels. +# 이제 TensorBoard로 전환하여 PROJECTOR 탭을 선택하면, +# 3D로 표현된 투영이 보일 것입니다. 그 모델을 회전하거나 +# 확대할 수 있습니다. 크거나 작은 규모(scale)로 그것을 살펴보며, +# 투영된 데이터와 레이블의 클러스터링에서 패턴을 발견할 수 있는지 +# 보세요. # -# For better visibility, it’s recommended to: +# 가시성을 높이려면, 다음을 권장합니다: # -# - Select “label” from the “Color by” drop-down on the left. -# - Toggle the Night Mode icon along the top to place the -# light-colored images on a dark background. +# - 좌측에 있는 “Color by” 드롭다운에서 “label”을 선택하세요. +# - 상단에 있는 야간 모드 아이콘을 전환(toggle)하여 +# 밝은 색상 이미지를 어두운 배경 위에 배치할 수 있습니다. # -# Other Resources +# 기타 자료 # --------------- # -# For more information, have a look at: +# 더 알고 싶다면 여기를 참조하세요: # -# - PyTorch documentation on `torch.utils.tensorboard.SummaryWriter `__ -# - Tensorboard tutorial content in the `PyTorch.org Tutorials `__ -# - For more information about TensorBoard, see the `TensorBoard -# documentation `__ +# - `torch.utils.tensorboard.SummaryWriter `_\에 대한 PyTorch 문서 +# - `PyTorch.org Tutorials `_\에 있는 Tensorboard 튜토리얼 콘텐츠 +# - TensorBoard에 대한 보다 더 자세한 내용은 `TensorBoard +# 문서 `_\를 참고하세요.