简体   繁体   English

如何在CUDA上使用PyTorch进行CIFAR-10?

[英]How to do CIFAR-10 with PyTorch on CUDA?

I'm following the CIFAR-10 PyTorch tutorial at this pytorch page , and can't get PyTorch running on the GPU. 我正在这个pytorch页面上关注CIFAR-10 PyTorch教程,并且无法在GPU上运行PyTorch。 The code is exactly as in the tutorial. 代码与教程中的完全相同。

The error I get is 我得到的错误是

Traceback (most recent call last):
  File "(file path)/CIFAR10_tutorial.py", line 116, in <module>
   outputs = net(images)
  File "/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py", line 491, in __call__
result = self.forward(*input, **kwargs)
  File "(file path)/CIFAR10_tutorial.py", line 65, in forward
x = self.pool(F.relu(self.conv1(x)).cuda())
  File "/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py", line 491, in __call__
result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/torch/nn/modules/conv.py", line 301, in forward
self.padding, self.dilation, self.groups)

My CUDA version is 9.0, Pytorch 0.4.0. 我的CUDA版本是9.0,Pytorch 0.4.0。 I have used tensorflow-gpu on the machine, so I know CUDA is set up correctly. 我在机器上使用过tensorflow-gpu,所以我知道CUDA设置正确。 Where exactly must I use .cuda() and .to(device) as suggested in the tutorial? 按照教程中的建议,我到底应该在哪里使用.cuda()和.to(device)?

I'm leaving an answer, in case anyone else is stuck on the same. 我要留下答案,以防其他人陷入困境。

First, configure Pytorch to use the GPU if available 首先,将Pytorch配置为使用GPU(如果有)

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)

Then, in the init function, cast to gpu by calling .cuda() on every element of the NN, eg 然后,在init函数中,通过对NN的每个元素调用.cuda()来强制转换为gpu,例如

self.conv1 = nn.Conv2d(3, 24, 5).cuda()
self.pool = nn.MaxPool2d(2, 2).cuda()

If you're not sure about the GPU, call .to(device) on every element. 如果不确定GPU,请在每个元素上调用.to(device)

In the forward(self, x) function, before the steps, I did 在步骤的前向我(x)函数中,我做了

x = x.to(device)

Right after net object is created, cast it to device by 创建完网络对象后,立即将其投射到设备上

net.to(device)

All inputs and labels should be cast to device before any operation is performed on them. 在对所有输入和标签进行任何操作之前,应将其全部投射到设备上。

inputs, labels = inputs.to(device), labels.to(device)

I am, skipping writing the entire code as the link has already been mentioned in the question. 我正在跳过编写整个代码的过程,因为问题中已经提到了链接。 If there are seem to be a few redundant casts to gpu, they're not breaking anything. 如果似乎有一些对gpu的多余转换,则它们没有破坏任何内容。 I might also put together an ipynb with the changes. 我可能还会将ipynb与这些更改放在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM