当前位置: 代码迷 >> 综合 >> 【pytorch】RuntimeError: Expected object of backend CPU but got backend CUDA for argument #2 ‘weight‘
  详细解决方案

【pytorch】RuntimeError: Expected object of backend CPU but got backend CUDA for argument #2 ‘weight‘

热度:20   发布时间:2023-11-25 04:22:10.0

该错误就是说没有在GPU是计算

怎么纠正?

首先

# 检测是否有用的GPU,如果有使用GPU,否则CPU
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")net = Net() #实例化网络为net
net = net.to(device)

然后在

for data in testLoader:images, labels = dataimages, labels = images.to(device), labels.to(device)

就可以使用GPU啦

  相关解决方案