If I#39;m not specifying to use CPU/GPU, which one is my script using?(如果我没有指定使用 CPU/GPU,那么我的脚本使用的是哪一个?)
问题描述
在 pytorch 中,如果我没有写任何关于使用 CPU/GPU 的内容,并且我的机器支持 CUDA (torch.cuda.is_available() == True
):
- 我的脚本使用的是 CPU 还是 GPU?
- 如果是 CPU,我应该怎么做才能让它在 GPU 上运行?我需要重写所有内容吗?
- 如果是 GPU,如果
torch.cuda.is_available() == False
,这个脚本会崩溃吗? - 这对加快训练速度有帮助吗?
- 我知道将 PyTorch 代码从 CPU 移植到GPU 但这是旧的.这种情况在 v0.4 或即将推出的 v1.0 中会发生变化吗?
我的方法是这样的(pytorch 0.4以下):
dtype = torch.cuda.float if torch.cuda.is_available() else torch.floattorch.zeros(2, 2, dtype=dtype)
更新 pytorch 0.4:
device = torch.device("cuda" if use_cuda else "cpu")模型 = MyRNN().to(device)
来自PyTorch 0.4.0 迁移指南.>
In pytorch, if I'm not writing anything about using CPU/GPU, and my machine supports CUDA (torch.cuda.is_available() == True
):
- What is my script using, CPU or GPU?
- If CPU, what should I do to make it run on GPU? Do I need to rewrite everything?
- If GPU, will this script crash if
torch.cuda.is_available() == False
? - Does this do anything about making the training faster?
- I'm aware of Porting PyTorch code from CPU to GPU but this is old. Does this situation change in v0.4 or the upcoming v1.0?
My way is like this (below pytorch 0.4):
dtype = torch.cuda.float if torch.cuda.is_available() else torch.float
torch.zeros(2, 2, dtype=dtype)
UPDATE pytorch 0.4:
device = torch.device("cuda" if use_cuda else "cpu")
model = MyRNN().to(device)
from PyTorch 0.4.0 Migration Guide.
这篇关于如果我没有指定使用 CPU/GPU,那么我的脚本使用的是哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!