How to run PyTorch on GPU by default?(如何默认在 GPU 上运行 PyTorch?)
问题描述
我想使用 cuda 运行 PyTorch.我为所有张量设置了 model.cuda()
和 torch.cuda.LongTensor()
.
I want to run PyTorch using cuda. I set model.cuda()
and torch.cuda.LongTensor()
for all tensors.
如果我使用了 model.cuda()
,我是否必须明确地使用 .cuda
创建张量?
Do I have to create tensors using .cuda
explicitly if I have used model.cuda()
?
有没有办法让所有计算默认在 GPU 上运行?
Is there a way to make all computations run on GPU by default?
推荐答案
我认为您不能指定默认情况下要使用 cuda 张量.但是,您应该查看 pytorch 官方示例.
I do not think you can specify that you want to use cuda tensors by default. However you should have a look to the pytorch offical examples.
在 imagenet 训练/测试脚本中,他们在名为 DataParallel.这个包装器有两个优点:
In the imagenet training/testing script, they use a wrapper over the model called DataParallel. This wrapper has two advantages:
- 它处理多个 GPU 上的数据并行性
- 它处理将 cpu 张量转换为 cuda 张量
正如您在 L164 中所见,您不必手动将输入/目标投射到 cuda.
As you can see in L164, you don't have to cast manually your inputs/targets to cuda.
请注意,如果您有多个 GPU 并且您想使用单个 GPU,请启动任何带有 CUDA_VISIBLE_DEVICES 前缀的 python/pytorch 脚本.例如CUDA_VISIBLE_DEVICES=0 python main.py
.
Note that, if you have multiple GPUs and you want to use a single one, launch any python/pytorch scripts with the CUDA_VISIBLE_DEVICES prefix. For instance CUDA_VISIBLE_DEVICES=0 python main.py
.
这篇关于如何默认在 GPU 上运行 PyTorch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何默认在 GPU 上运行 PyTorch?


基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01