If I#39;m not specifying to use CPU/GPU, which one is my script using?(如果我没有指定使用 CPU/GPU,那么我的脚本使用的是哪一个?)
本文介绍了如果我没有指定使用 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,那么我的脚本使用的是哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如果我没有指定使用 CPU/GPU,那么我的脚本使用的
基础教程推荐
猜你喜欢
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 包装空间模型 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 求两个直方图的卷积 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
