pytorch使用时,从使用spyder转向pycharm过程中遇到的一些问题。

xiaoxiao2021-02-28  47

前言:

    之前一直使用spyder作为运行环境,但是随着学习的深入,需要处理的程序越来越多,spyder的操作变得麻烦起来,是一款渐渐的无法满足需求,与之形成鲜明的比,pycharm功能强大,是python开发的不二之选,于是从spyder转向使用pycharm,但是在这过程中遇到了一些奇怪的问题。

    用spyder运行torch(0.4.0版)官方mnist实例,运行正常,windows上的pycharm运行统一串代码却无法运行,版本(pycharm-community-2018.1.2),(pycharm-community-2018.1.4),(pycharm-professional-2018.1.4)均无法运行,而ubuntu系统下的pycharm-community-2018.1却能正常使用。

 

报错:

  1,运行mnist主程序,报错:_jb_pytest_runner.py: error: unrecognized arguments,大概意思是,args = parser.parse_args()无法得到参数。

 

#只要将 args = parser.parse_args() # 换成: args, unknown = parser.parse_known_args() #便可。

2,随后出现新报错:

UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead. data, target = Variable(data, volatile=True), Variable(target) #和 UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number test_loss += F.nll_loss(output, target, size_average=False).data[0] # sum up batch loss

 

修改如下:

将:

data, target = Variable(data, volatile=True), Variable(target) #和 test_loss += F.nll_loss(output, target, size_average=False).data[0] # sum up batch loss

分别修改为:

data, target = Variable(data), Variable(target) #和 test_loss += F.nll_loss(output, target, size_average=False).item() # sum up batch loss

 

原因分析:

可能是因为最新版pyhcarm提前进入了PyTorch 0.5 的要求。在排斥Torch 0.4.0 的语法。

 

转载请注明原文地址: https://www.6miu.com/read-2626294.html

最新回复(0)