pytorch采坑(Expected all tensors to be on the same device, but found at least two devices)

在PyTorch中,将模型和输入数据放在GPU上时遇到错误,原因是reshape操作创建的新张量默认在CPU上。解决方法是确保reshape后的张量也转移到GPU设备上,可以通过添加.to(device)实现。正确做法是在reshape前或后立即将张量移动到目标设备。
摘要由CSDN通过智能技术生成

pytorch采坑(Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0)

问题描述

pytorch版本:2.0.1+cu117

使用pytorch的时候,我将模型部署到gpu上,然后将输入也部署到gpu上,但是却出现了如下报错

在这里插入图片描述
贴上我的源代码:

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


class Modeltest(nn.Module):

    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv1d(1, 20, 5)
        self.conv2 = nn.Conv1d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))


device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

input = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8, 9,
                      10]).to(device).type(torch.FloatTensor)

input = torch.reshape(input, (1, -1))

modeltest = Modeltest().to(device)

output = modeltest(input)

output2 = output.sum() * 0.1

print(output.device)
print(output2.device)


从代码中可以看出,我定义了input,并写了to(device),然后对input进行了reshape操作,结果出现了报错

解决方案

我们最开始定义的input确实是在gpu上,但是使用了reshape之后,相当于重新定义了一个input,而默认情况下,新定义的变量都是在cpu上的,所以出现了问题,正确的代码应该如下:

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


class Modeltest(nn.Module):

    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv1d(1, 20, 5)
        self.conv2 = nn.Conv1d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))


device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

input = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8, 9,
                      10]).type(torch.FloatTensor)

input = torch.reshape(input, (1, -1)).to(device)

modeltest = Modeltest().to(device)

output = modeltest(input)

output2 = output.sum() * 0.1

print(output.device)
print(output2.device)


或者第一个input定义的时候直接使用reshape,也可以,如下:

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


class Modeltest(nn.Module):

    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv1d(1, 20, 5)
        self.conv2 = nn.Conv1d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))


device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

input = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8, 9,
                      10]).type(torch.FloatTensor).reshape(1, -1).to(device)

modeltest = Modeltest().to(device)

output = modeltest(input)

output2 = output.sum() * 0.1

print(output.device)
print(output2.device)
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值