# 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环境了,因此也就出错了。
版权声明:本文为PSpiritV原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。