Skip to content

Commit 59c2dc4

Browse files
Update tensorboardyt tutorial to torchvision v2 APIs
1 parent fe8a6a1 commit 59c2dc4

1 file changed

Lines changed: 9 additions & 16 deletions

File tree

beginner_source/introyt/tensorboardyt_tutorial.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
# Image datasets and image manipulation
6161
import torchvision
62-
import torchvision.transforms as transforms
62+
from torchvision.transforms import v2
6363

6464
# Image display
6565
import matplotlib.pyplot as plt
@@ -68,14 +68,6 @@
6868
# PyTorch TensorBoard support
6969
from torch.utils.tensorboard import SummaryWriter
7070

71-
# In case you are using an environment that has TensorFlow installed,
72-
# such as Google Colab, uncomment the following code to avoid
73-
# a bug with saving embeddings to your TensorBoard directory
74-
75-
# import tensorflow as tf
76-
# import tensorboard as tb
77-
# tf.io.gfile = tb.compat.tensorflow_stub.io.gfile
78-
7971
######################################################################
8072
# Showing Images in TensorBoard
8173
# -----------------------------
@@ -84,9 +76,10 @@
8476
#
8577

8678
# Gather datasets and prepare them for consumption
87-
transform = transforms.Compose(
88-
[transforms.ToTensor(),
89-
transforms.Normalize((0.5,), (0.5,))])
79+
transform = v2.Compose(
80+
[v2.ToImage(),
81+
v2.ToDtype(torch.float32, scale=True),
82+
v2.Normalize((0.5,), (0.5,))])
9083

9184
# Store separate training and validations splits in ./data
9285
training_set = torchvision.datasets.FashionMNIST('./data',
@@ -171,7 +164,7 @@ def matplotlib_imshow(img, one_channel=False):
171164

172165
class Net(nn.Module):
173166
def __init__(self):
174-
super(Net, self).__init__()
167+
super().__init__()
175168
self.conv1 = nn.Conv2d(1, 6, 5)
176169
self.pool = nn.MaxPool2d(2, 2)
177170
self.conv2 = nn.Conv2d(6, 16, 5)
@@ -214,18 +207,18 @@ def forward(self, x):
214207

215208
running_loss += loss.item()
216209
if i % 1000 == 999: # Every 1000 mini-batches...
217-
print('Batch {}'.format(i + 1))
210+
print(f'Batch {i+1}')
218211
# Check against the validation set
219212
running_vloss = 0.0
220213

221214
# In evaluation mode some model specific operations can be omitted eg. dropout layer
222-
net.train(False) # Switching to evaluation mode, eg. turning off regularisation
215+
net.eval() # Switching to evaluation mode, eg. turning off regularisation
223216
for j, vdata in enumerate(validation_loader, 0):
224217
vinputs, vlabels = vdata
225218
voutputs = net(vinputs)
226219
vloss = criterion(voutputs, vlabels)
227220
running_vloss += vloss.item()
228-
net.train(True) # Switching back to training mode, eg. turning on regularisation
221+
net.train() # Switching back to training mode, eg. turning on regularisation
229222

230223
avg_loss = running_loss / 1000
231224
avg_vloss = running_vloss / len(validation_loader)

0 commit comments

Comments
 (0)