代码之家  ›  专栏  ›  技术社区  ›  aerin

属性错误:模块“torch”没有属性“device”

  •  2
  • aerin  · 技术社区  · 6 年前
    ---> 13 device = torch.device({"cuda"} if torch.cuda.is_available() else {"cpu"})
         14
         15
    
    AttributeError: module 'torch' has no attribute 'device'
    

    我99%肯定这是因为我没有把火炬从0.31升级到0.4但是我现在不能升级Py火炬。

    我需要将.device(0.4)转换为在0.31中工作的内容。

    我检查 migration document 然而,它不能提供我如何转换火炬。请帮助!

    1 回复  |  直到 6 年前
        1
  •  3
  •   Wasi Ahmad    6 年前

    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版本的。