diff --git a/cifar.py b/cifar.py index 9572cf81..9dfe0016 100644 --- a/cifar.py +++ b/cifar.py @@ -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 @@ -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() @@ -303,7 +303,9 @@ 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) @@ -311,9 +313,9 @@ def test(testloader, model, criterion, 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)) # measure elapsed time batch_time.update(time.time() - end) diff --git a/utils/eval.py b/utils/eval.py index 50513501..d6f11c66 100644 --- a/utils/eval.py +++ b/utils/eval.py @@ -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 \ No newline at end of file