在 pytorch 中使用 CUDA? [英] Using CUDA with pytorch?

查看:44
本文介绍了在 pytorch 中使用 CUDA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里搜索过,但只找到过时的帖子.

I have searched on here but I found only outdated posts.

我想在我的 GPU 上运行训练.我在一些论坛上发现我需要将 .cuda() 应用到我想使用 CUDA 的任何东西上(我已经将它应用到我可以应用的所有东西上,而不会使程序崩溃).令人惊讶的是,这使得训练速度更慢.

I want to run the training on my GPU. I found on some forums that I need to apply .cuda() on anything I want to use CUDA with (I've applied it to everything I could without making the program crash). Surprisingly, this makes the training even slower.

然后,我发现你可以使用这个 torch.set_default_tensor_type('torch.cuda.FloatTensor') 来使用 CUDA.启用两者后,没有任何变化.发生了什么?

Then, I found that you could use this torch.set_default_tensor_type('torch.cuda.FloatTensor') to use CUDA. With both enabled, nothing changes. What is happening?

有没有办法在整个模型上可靠地启用 CUDA?

Is there a way to reliably enable CUDA on the whole model?

另外,MyModel() 是什么意思?我需要更多有形的例子,比如代码例子.(这是我指的帖子)

Also, what does MyModel() mean? I need more tangible examples, like code examples. (This is the post I am referring to)

推荐答案

您可以使用 tensor.to(device) 命令将张量移动到设备.

You can use the tensor.to(device) command to move a tensor to a device.

.to() 命令也用于将整个模型移动到设备,就像您链接到的帖子一样.

The .to() command is also used to move a whole model to a device, like in the post you linked to.

另一种可能性是在创建期间使用 device= 关键字参数设置张量的设备,例如 t = torch.tensor(some_list, device=device)

Another possibility is to set the device of a tensor during creation using the device= keyword argument, like in t = torch.tensor(some_list, device=device)

要在代码中动态设置设备,您可以使用

To set the device dynamically in your code, you can use

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

如果可能,将 cuda 设置为您的设备.

to set cuda as your device if possible.

PyTorch 教程 和上面链接的文档中有各种代码示例可以帮助您.

There are various code examples on PyTorch Tutorials and in the documentation linked above that could help you.

这篇关于在 pytorch 中使用 CUDA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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