Skip to content

Commit 90e56ea

Browse files
authored
Merge branch 'main' into resolve-2332
2 parents 718fc2f + dd6a55d commit 90e56ea

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

beginner_source/basics/optimization_tutorial.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ def forward(self, x):
149149

150150
def train_loop(dataloader, model, loss_fn, optimizer):
151151
size = len(dataloader.dataset)
152+
# Set the model to training mode - important for batch normalization and dropout layers
153+
# Unnecessary in this situation but added for best practices
154+
model.train()
152155
for batch, (X, y) in enumerate(dataloader):
153156
# Compute prediction and loss
154157
pred = model(X)
@@ -165,10 +168,15 @@ def train_loop(dataloader, model, loss_fn, optimizer):
165168

166169

167170
def test_loop(dataloader, model, loss_fn):
171+
# Set the model to evaluation mode - important for batch normalization and dropout layers
172+
# Unnecessary in this situation but added for best practices
173+
model.eval()
168174
size = len(dataloader.dataset)
169175
num_batches = len(dataloader)
170176
test_loss, correct = 0, 0
171177

178+
# Evaluating the model with torch.no_grad() ensures that no gradients are computed during test mode
179+
# also serves to reduce unnecessary gradient computations and memory usage for tensors with requires_grad=True
172180
with torch.no_grad():
173181
for X, y in dataloader:
174182
pred = model(X)

beginner_source/introyt/introyt1_tutorial.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def num_flat_features(self, x):
288288

289289
transform = transforms.Compose(
290290
[transforms.ToTensor(),
291-
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
291+
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2470, 0.2435, 0.2616))])
292292

293293

294294
##########################################################################
@@ -297,9 +297,28 @@ def num_flat_features(self, x):
297297
# - ``transforms.ToTensor()`` converts images loaded by Pillow into
298298
# PyTorch tensors.
299299
# - ``transforms.Normalize()`` adjusts the values of the tensor so
300-
# that their average is zero and their standard deviation is 0.5. Most
300+
# that their average is zero and their standard deviation is 1.0. Most
301301
# activation functions have their strongest gradients around x = 0, so
302302
# centering our data there can speed learning.
303+
# The values passed to the transform are the means (first tuple) and the
304+
# standard deviations (second tuple) of the rgb values of the images in
305+
# the dataset. You can calculate these values yourself by running these
306+
# few lines of code:
307+
# ```
308+
# from torch.utils.data import ConcatDataset
309+
# transform = transforms.Compose([transforms.ToTensor()])
310+
# trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
311+
# download=True, transform=transform)
312+
#
313+
# #stack all train images together into a tensor of shape
314+
# #(50000, 3, 32, 32)
315+
# x = torch.stack([sample[0] for sample in ConcatDataset([trainset])])
316+
#
317+
# #get the mean of each channel
318+
# mean = torch.mean(x, dim=(0,2,3)) #tensor([0.4914, 0.4822, 0.4465])
319+
# std = torch.std(x, dim=(0,2,3)) #tensor([0.2470, 0.2435, 0.2616])
320+
#
321+
# ```
303322
#
304323
# There are many more transforms available, including cropping, centering,
305324
# rotation, and reflection.

prototype_source/fx_graph_mode_ptq_static.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ Download the `torchvision resnet18 model <https://download.pytorch.org/models/re
214214
float_model = load_model(saved_model_dir + float_model_file).to("cpu")
215215
float_model.eval()
216216
217-
# deepcopy the model since we need to keep the original model around
218-
import copy
219-
model_to_quantize = copy.deepcopy(float_model)
217+
# create another instance of the model since
218+
# we need to keep the original model around
219+
model_to_quantize = load_model(saved_model_dir + float_model_file).to("cpu")
220220
221221
3. Set model to eval mode
222222
-------------------------
@@ -408,4 +408,4 @@ Running the model in AIBench (with single threading) gives the following result:
408408
409409
As we can see for resnet18 both FX graph mode and eager mode quantized model get similar speedup over the floating point model,
410410
which is around 2-4x faster than the floating point model. But the actual speedup over floating point model may vary
411-
depending on model, device, build, input batch sizes, threading etc.
411+
depending on model, device, build, input batch sizes, threading etc.

0 commit comments

Comments
 (0)