如果我没有指定使用 CPU/GPU,那么我的脚本使用的是哪一个? [英] If I'm not specifying to use CPU/GPU, which one is my script using?

查看:13
本文介绍了如果我没有指定使用 CPU/GPU,那么我的脚本使用的是哪一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 pytorch 中,如果我没有写任何关于使用 CPU/GPU 的内容,并且我的机器支持 CUDA (torch.cuda.is_available() == True):

  1. 我的脚本使用的是 CPU 还是 GPU?
  2. 如果是 CPU,我应该怎么做才能让它在 GPU 上运行?我需要重写所有内容吗?
  3. 如果是 GPU,如果 torch.cuda.is_available() == False,这个脚本会崩溃吗?
  4. 这对加快训练速度有帮助吗?
  5. 我知道将 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):

  1. What is my script using, CPU or GPU?
  2. If CPU, what should I do to make it run on GPU? Do I need to rewrite everything?
  3. If GPU, will this script crash if torch.cuda.is_available() == False?
  4. Does this do anything about making the training faster?
  5. 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,那么我的脚本使用的是哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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