Skip to content

Commit

Permalink
enhancing readability (pytorch#495)
Browse files Browse the repository at this point in the history
* enhancing readability

Removing `sys` package as it wasn't used. Also, renaming all `input` instances to `predictor` as `input` is a builtin keyword.

* renaming `input` to `images`
  • Loading branch information
ndrwnaguib authored and soumith committed May 27, 2019
1 parent fa3f739 commit 97304e2
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions imagenet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import shutil
import time
import warnings
import sys

import torch
import torch.nn as nn
Expand Down Expand Up @@ -269,23 +268,23 @@ def train(train_loader, model, criterion, optimizer, epoch, args):
model.train()

end = time.time()
for i, (input, target) in enumerate(train_loader):
for i, (images, target) in enumerate(train_loader):
# measure data loading time
data_time.update(time.time() - end)

if args.gpu is not None:
input = input.cuda(args.gpu, non_blocking=True)
images = images.cuda(args.gpu, non_blocking=True)
target = target.cuda(args.gpu, non_blocking=True)

# compute output
output = model(input)
output = model(images)
loss = criterion(output, target)

# measure accuracy and record loss
acc1, acc5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(acc1[0], input.size(0))
top5.update(acc5[0], input.size(0))
losses.update(loss.item(), images.size(0))
top1.update(acc1[0], images.size(0))
top5.update(acc5[0], images.size(0))

# compute gradient and do SGD step
optimizer.zero_grad()
Expand Down Expand Up @@ -313,20 +312,20 @@ def validate(val_loader, model, criterion, args):

with torch.no_grad():
end = time.time()
for i, (input, target) in enumerate(val_loader):
for i, (images, target) in enumerate(val_loader):
if args.gpu is not None:
input = input.cuda(args.gpu, non_blocking=True)
images = images.cuda(args.gpu, non_blocking=True)
target = target.cuda(args.gpu, non_blocking=True)

# compute output
output = model(input)
output = model(images)
loss = criterion(output, target)

# measure accuracy and record loss
acc1, acc5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(acc1[0], input.size(0))
top5.update(acc5[0], input.size(0))
losses.update(loss.item(), images.size(0))
top1.update(acc1[0], images.size(0))
top5.update(acc5[0], images.size(0))

# measure elapsed time
batch_time.update(time.time() - end)
Expand Down

0 comments on commit 97304e2

Please sign in to comment.