AttributeError:模块'torch'没有属性“device”

时间:2018-06-10 06:07:59

标签: pytorch

---> 13 device = torch.device({"cuda"} if torch.cuda.is_available() else {"cpu"})
     14
     15

AttributeError: module 'torch' has no attribute 'device'

我99%肯定这是因为我没有将pytorch从0.31升级到0.4但是我现在无法升级pytorch。

我需要将.device(0.4)翻译成在0.31中运行的东西。

我检查了migration document但是它没有提供我如何在回顾中转换torch.device。请帮忙!

2 个答案:

答案 0 :(得分:3)

torch.cuda.device()是一个上下文管理器。

torch.cuda.set_device(0)
# On device 0
with torch.cuda.device(1):
    print("Inside device is 1")    
    # On device 1
print("Outside is still 0")
# On device 0

以上的作品来自0.2版本。

答案 1 :(得分:0)

在1.5版中

if torch.cuda.is_available():
    device = torch.device("cuda:0")
    print("running on the GPU")
else:
    device = torch.device("cpu")
    print("running on the CPU")