如何检查PyTorch是否使用GPU?nvidia-smi命令可以检测GPU活动,但我想直接从Python脚本中检查它。
当前回答
步骤1:导入火炬库
import torch
#步骤2:创建张量
tensor = torch.tensor([5, 6])
#步骤3:查找设备类型
#输出1:在下面的输出中,我们可以得到size(tensor.shape), dimension(tensor.ndim), #和处理张量的设备
tensor, tensor.device, tensor.ndim, tensor.shape
(tensor([5, 6]), device(type='cpu'), 1, torch.Size([2]))
#or
#输出2:在下面,输出我们可以得到的唯一设备类型
tensor.device
device(type='cpu')
#由于我的系统使用cpu处理器“第11代英特尔(R)酷睿(TM) i5-1135G7 @ 2.40GHz 2.42 GHz”
#find,如果张量处理GPU?
print(tensor, torch.cuda.is_available()
# the output will be
tensor([5, 6]) False
#上面的输出是假的,因此它不在gpu上
#快乐编码:)
其他回答
在官方网站的入门页面,你可以像这样检查PyTorch的GPU是否可用:
import torch
torch.cuda.is_available()
参考:PyTorch | Get Started
这些函数应该有助于:
>>> import torch
>>> torch.cuda.is_available()
True
>>> torch.cuda.device_count()
1
>>> torch.cuda.current_device()
0
>>> torch.cuda.device(0)
<torch.cuda.device at 0x7efce0b03be0>
>>> torch.cuda.get_device_name(0)
'GeForce GTX 950M'
这告诉我们:
CUDA是可用的,可以在一台设备上使用。 Device 0指GPU GeForce GTX 950M,目前被PyTorch选中。
这里几乎所有的答案都引用torch.cuda.is_available()。然而,这只是硬币的一部分。它告诉你GPU(实际上是CUDA)是否可用,而不是它是否实际被使用。在一个典型的设置中,你会像这样设置你的设备:
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
但在更大的环境中(例如研究),通常也会给用户更多的选择,因此根据输入,他们可以禁用CUDA,指定CUDA id,等等。在这种情况下,是否使用GPU不仅仅取决于GPU是否可用。在设备被设置为torch设备之后,您可以获取它的type属性来验证它是否是CUDA。
if device.type == 'cuda':
# do something
由于这里没有提出,我添加了一个使用torch.device的方法,因为这非常方便,在正确的设备上初始化张量时也是如此。
# setting device on GPU if available, else CPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('Using device:', device)
print()
#Additional Info when using cuda
if device.type == 'cuda':
print(torch.cuda.get_device_name(0))
print('Memory Usage:')
print('Allocated:', round(torch.cuda.memory_allocated(0)/1024**3,1), 'GB')
print('Cached: ', round(torch.cuda.memory_reserved(0)/1024**3,1), 'GB')
编辑:torch.cuda。Memory_cached已重命名为torch.cuda.memory_reserved。因此,对于旧版本使用memory_cached。
输出:
Using device: cuda
Tesla K80
Memory Usage:
Allocated: 0.3 GB
Cached: 0.6 GB
如上所述,使用设备可以:
将张量移动到相应的设备: torch.rand(10),(设备) 直接在设备上创建一个张量: 火炬。兰特(10,设备=设备)
这使得CPU和GPU之间的切换舒适,而不改变实际的代码。
编辑:
由于有一些问题和困惑的缓存和分配内存,我添加了一些关于它的额外信息:
max_memory_cached(device=None)返回缓存分配器管理的最大GPU内存,单位为字节 鉴于设备。 memory_allocated(device=None)返回给定设备的当前GPU内存使用情况(以字节为单位)。
你可以直接移交一个设备,就像上面提到的那样,或者你可以让它为None,它将使用current_device()。
附加注意:旧的图形卡与Cuda计算能力3.0或更低可能是可见的,但不能被Pytorch使用!感谢hekimgil指出这一点!“发现了GPU0 GeForce GT 750M, cuda能力3.0。PyTorch不再支持这个GPU,因为它太老了。我们支持的cuda最低能力是3.5。”
Query | Command |
---|---|
Does PyTorch see any GPUs? | torch.cuda.is_available() |
Are tensors stored on GPU by default? | torch.rand(10).device |
Set default tensor type to CUDA: | torch.set_default_tensor_type(torch.cuda.FloatTensor) |
Is this tensor a GPU tensor? | my_tensor.is_cuda |
Is this model stored on the GPU? | all(p.is_cuda for p in my_model.parameters()) |
推荐文章
- 在python中,年龄从出生日期开始
- 使用pip安装SciPy
- 在Python中,我应该如何测试变量是否为None, True或False
- 如何在Python中从毫秒创建datetime ?
- 如何解窝(爆炸)在一个熊猫数据帧列,成多行
- 如何使用pip安装opencv ?
- 在pip冻结命令的输出中“pkg-resources==0.0.0”是什么
- 格式y轴为百分比
- 熊猫连接问题:列重叠但没有指定后缀
- 为什么空字典在Python中是一个危险的默认值?
- 在Python中,冒号等于(:=)是什么意思?
- Python "SyntaxError:文件中的非ascii字符'\xe2' "
- 如何从psycopg2游标获得列名列表?
- Python中dict对象的联合
- 如何有效地比较两个无序列表(不是集合)?