【深度学习】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! (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)

__EOF__

本文作者在青青草原上抓羊
本文链接https://www.cnblogs.com/seansheep/p/16020753.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   SeanSiyang  阅读(8192)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示