【问题标题】:AttributeError: module 'torch' has no attribute "device"AttributeError:模块“火炬”没有属性“设备”
【发布时间】:2018-11-19 17:13:30
【问题描述】:
---> 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 回想起来。请帮忙!

【问题讨论】:

    标签: pytorch


    【解决方案1】:

    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 版本开始。

    【讨论】:

      【解决方案2】:

      在 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")
      

      【讨论】:

        猜你喜欢
        • 2023-04-04
        • 2021-05-13
        • 2018-10-10
        • 2018-04-29
        • 2021-01-31
        • 1970-01-01
        • 2021-11-14
        • 2021-09-22
        相关资源
        最近更新 更多