Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Outdated PyTorch Operations for AlexNet Training on CIFAR #57

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 10 additions & 8 deletions cifar.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def train(trainloader, model, criterion, optimizer, epoch, use_cuda):
data_time.update(time.time() - end)

if use_cuda:
inputs, targets = inputs.cuda(), targets.cuda(async=True)
inputs, targets = inputs.cuda(), targets.cuda()
inputs, targets = torch.autograd.Variable(inputs), torch.autograd.Variable(targets)

# compute output
Expand All @@ -254,9 +254,9 @@ def train(trainloader, model, criterion, optimizer, epoch, use_cuda):

# measure accuracy and record loss
prec1, prec5 = accuracy(outputs.data, targets.data, topk=(1, 5))
losses.update(loss.data[0], inputs.size(0))
top1.update(prec1[0], inputs.size(0))
top5.update(prec5[0], inputs.size(0))
losses.update(loss.item(), inputs.size(0))
top1.update(prec1.item(), inputs.size(0))
top5.update(prec5.item(), inputs.size(0))

# compute gradient and do SGD step
optimizer.zero_grad()
Expand Down Expand Up @@ -303,17 +303,19 @@ def test(testloader, model, criterion, epoch, use_cuda):

if use_cuda:
inputs, targets = inputs.cuda(), targets.cuda()
inputs, targets = torch.autograd.Variable(inputs, volatile=True), torch.autograd.Variable(targets)
with torch.no_grad():
inputs, targets = torch.autograd.Variable(inputs), torch.autograd.Variable(targets)


# compute output
outputs = model(inputs)
loss = criterion(outputs, targets)

# measure accuracy and record loss
prec1, prec5 = accuracy(outputs.data, targets.data, topk=(1, 5))
losses.update(loss.data[0], inputs.size(0))
top1.update(prec1[0], inputs.size(0))
top5.update(prec5[0], inputs.size(0))
losses.update(loss.item(), inputs.size(0))
top1.update(prec1.item(), inputs.size(0))
top5.update(prec5.item(), inputs.size(0))

# measure elapsed time
batch_time.update(time.time() - end)
Expand Down
4 changes: 2 additions & 2 deletions utils/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def accuracy(output, target, topk=(1,)):

_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
correct = pred.eq(target.reshape(1, -1).expand_as(pred))

res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
correct_k = correct[:k].reshape(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res