为什么我的简单pytorch网络无法在GPU设备上工作? [英] Why doesn't my simple pytorch network work on GPU device?

查看:824
本文介绍了为什么我的简单pytorch网络无法在GPU设备上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个教程中构建了一个简单的网络,但出现了这个错误:

I built a simple network from a tutorial and I got this error:

RuntimeError:类型为torch.cuda.FloatTensor的预期对象,但找到了 为参数#4'mat1'输入torch.FloatTensor

RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.FloatTensor for argument #4 'mat1'

有帮助吗?谢谢!

import torch
import torchvision

device = torch.device("cuda:0")
root = '.data/'

dataset = torchvision.datasets.MNIST(root, transform=torchvision.transforms.ToTensor(), download=True)

dataloader = torch.utils.data.DataLoader(dataset, batch_size=4)


class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.out = torch.nn.Linear(28*28, 10)

    def forward(self, x):
        x = x.view(x.size(0), -1)
        x = self.out(x)
        return x

net = Net()
net.to(device)

for i, (inputs, labels) in enumerate(dataloader):
    inputs.to(device)
    out = net(inputs)

推荐答案

TL; DR
这是解决方法

TL;DR
This is the fix

inputs = inputs.to(device)  

为什么?!
torch.nn.Module.to() torch.Tensor.to() :而Module.to()是-place 运算符,而Tensor.to()不是.因此

Why?!
There is a slight difference between torch.nn.Module.to() and torch.Tensor.to(): while Module.to() is an in-place operator, Tensor.to() is not. Therefore

net.to(device)

更改net本身并将其移动到device.另一方面

Changes net itself and moves it to device. On the other hand

inputs.to(device)

不会更改inputs,而是返回位于device上的inputs副本.要使用该在设备上"副本,您需要将其分配给一个变量,因此

does not change inputs, but rather returns a copy of inputs that resides on device. To use that "on device" copy, you need to assign it into a variable, hence

inputs = inputs.to(device)

这篇关于为什么我的简单pytorch网络无法在GPU设备上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆