前言
這篇是我在使用 Pytorch 的時候碰到的問題,出現了以下的訊息:
Traceback (most recent call last):
RuntimeError: Expected condition, x and y to be on the same device, but condition is on cuda:0 and x and y are on cuda:0 and cpu respectively
解決方法
解決方法:
cuda:0 指的是 GPU 位置,
上述的內容指的是「指定的數值」並沒有被放置對應的「GPU記憶體位置」導致無法計算。
一般來說數值會儲存於 CPU 上,
我們需要進行額外的動作才可以再把數值從「CPU 轉到 GPU 上」。
修改範例
只需要把值傳入對應的 GPU 即可
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
x = x.to(DEVICE)
y = y.to(DEVICE)
只有位於同一個「GPU記憶體位置」的數值能夠互相計算。