# import torch
from torch import nn

m = nn.Softmax(dim=1)
input = torch.randn(2, 3)
output = m(input)

执行上面的代码时,会报错:NameError: name 'torch' is not defined。

而执行下面的代码时,就是正常的。

import torch
from torch import nn

m = nn.Softmax(dim=1)
input = torch.randn(2, 3)
output = m(input)

至于原因,我实在是不懂。比如下面的代码,我直接用from numpy import argmax就是正常的。

import torch
from torch import nn
from numpy import argmax

m = nn.Softmax(dim=1)
input = torch.randn(2, 3)
output = m(input)

max1= argmax([1,2,3])

可能只有torch这个模块才需要这样吧!

另外如果在jupyter notebook中导入torch时出现这个错误,可能是因为安装torch的那个环境没有安装jupyter,这时候用指令打开的jupyter notebook对应的环境就不是torch环境了,因此也就出错了。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐