Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions beginner_source/transfer_learning_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,12 @@ def imshow(inp, title=None):
# - Scheduling the learning rate
# - Saving the best model
#
# In the following, parameter ``scheduler`` is an LR scheduler object from
# ``torch.optim.lr_scheduler``.
# In this tutorial, `scheduler` is an LR scheduler object (e.g. StepLR).
# For schedulers like StepLR, the recommended usage is:
# optimizer.step() followed by scheduler.step()
# which is why `scheduler.step()` is called once at the end of each epoch,
# after all optimizer steps for that epoch are complete.



def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
Expand Down Expand Up @@ -185,7 +189,8 @@ def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
_, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)

# backward + optimize only if in training phase
# backward pass + optimizer step (only in training phase)

if phase == 'train':
loss.backward()
optimizer.step()
Expand Down