问题
----------one epoch begin----------
the compression rate now is 0.700000
pruning_cifar10_resnet.py:302: UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.
input_var = torch.autograd.Variable(input, volatile=True)
pruning_cifar10_resnet.py:303: UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.
target_var = torch.autograd.Variable(target, volatile=True)
Traceback (most recent call last):
File "pruning_cifar10_resnet.py", line 460, in <module>
main()
File "pruning_cifar10_resnet.py", line 176, in main
val_acc_1, val_los_1 = validate(test_loader, net, criterion, log)
File "pruning_cifar10_resnet.py", line 313, in validate
top1.update(prec1[0], input.size(0))
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
root@633634ef4702:/home/xjr_directory/py/rethinking-network-pruning/cifar/soft-filter-pruning#
原因
报错原因:版本升级,用法发生改变
解决方式
方式一:
top1.update(prec1[0], input.size(0))
# 更改为
top1.update(prec1.item(), input.size(0))
方式二:
top1.update(prec1[0], input.size(0))
# 更改为
top1.update(prec1.data , input.size(0))
常见的还有
total_loss += loss_val.data[0]
# 需要修改为以下
total_loss += loss_val.data
# 或者
total_loss += loss_val.item()