【深度学习】RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

报错代码:

if __name__ == '__main__':
    model = Perception(2, 3, 2).cuda()

    input = torch.randn(4, 2).cuda()
    output = model(input)
    # output = output.cuda()

    label = torch.Tensor([0, 1, 1, 0]).long()
    criterion = nn.CrossEntropyLoss()
    loss_nn = criterion(output, label)
    print(loss_nn)
    loss_functional = F.cross_entropy(output, label)
    print(loss_functional)

报错截图如下:

【深度学习】RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument target in method wrapper_nll_loss_forward)

报这个错的原因在于,代码中的Tensor,一会在CPU中运行,一会在GPU中运行,所以最好是都放在同一个device中执行

核心代码:

device = torch.device('cuda:0')

并且将用到的Tensor都改为同一个device: Tensor.to(device)

上述代码修改后:

if __name__ == '__main__':
    device = torch.device('cuda:0')
    model = Perception(2, 3, 2).to(device)

    input = torch.randn(4, 2).to(device)
    output = model(input).to(device)

    label = torch.Tensor([0, 1, 1, 0]).long().to(device)
    criterion = nn.CrossEntropyLoss()
    loss_nn = criterion(output, label).to(device)
    print(loss_nn)
    loss_functional = F.cross_entropy(output, label)
    print(loss_functional)

这样就不会报错了

完整代码:

import torch
from torch import nn
import torch.nn.functional as F

from torch.nn import Linear

class linear(nn.Module):  # 继承nn.Module
    def __init__(self, in_dim, out_dim):
        super(Linear, self).__init__()  # 调用nn.Module的构造函数

        # 使用nn.Parameter来构造需要学习的参数
        self.w = nn.Parameter(torch.randn(in_dim, out_dim))
        self.b = nn.Parameter(torch.randn(out_dim))

    # 在forward中实现前向传播过程
    def forward(self, x):
        x = x.matmul(self.w)
        y = x + self.b.expand_as(x)  # expand_as保证矩阵形状一致

        return y

class Perception(nn.Module):
    def __init__(self, in_dim, hid_dim, out_dim):
        super(Perception, self).__init__()
        self.layer = nn.Sequential(
            nn.Linear(in_dim, hid_dim),
            nn.Sigmoid(),
            nn.Linear(hid_dim, out_dim),
            nn.Sigmoid()
        )
        # self.layer1 = Linear(in_dim, hid_dim)
        # self.layer2 = Linear(hid_dim, out_dim)

    def forward(self, x):
        # x = self.layer1(x)
        # y = torch.sigmoid(x)
        # y = self.layer2(y)
        # y = torch.sigmoid(y)
        y = self.layer(x)
        return y

if __name__ == '__main__':
    device = torch.device('cuda:0')
    model = Perception(2, 3, 2).to(device)

    input = torch.randn(4, 2).to(device)
    output = model(input).to(device)
    # output = output.cuda()

    label = torch.Tensor([0, 1, 1, 0]).long().to(device)
    criterion = nn.CrossEntropyLoss()
    loss_nn = criterion(output, label).to(device)
    print(loss_nn)
    loss_functional = F.cross_entropy(output, label)
    print(loss_functional)

Original: https://www.cnblogs.com/seansheep/p/16020753.html
Author: 在青青草原上抓羊
Title: 【深度学习】RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/608997/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球