如何检查 pytorch 是否正在使用 GPU? [英] How to check if pytorch is using the GPU?

查看:78
本文介绍了如何检查 pytorch 是否正在使用 GPU?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 pytorch 是否在使用我的 GPU.如果在此过程中 GPU 有任何活动,可以使用 nvidia-smi 进行检测,但我想要用 python 脚本编写的内容.

I would like to know if pytorch is using my GPU. It's possible to detect with nvidia-smi if there is any activity from the GPU during the process, but I want something written in a python script.

有没有办法做到这一点?

Is there a way to do so?

推荐答案

这应该有效:

import torch

torch.cuda.is_available()
>>> True

torch.cuda.current_device()
>>> 0

torch.cuda.device(0)
>>> <torch.cuda.device at 0x7efce0b03be0>

torch.cuda.device_count()
>>> 1

torch.cuda.get_device_name(0)
>>> 'GeForce GTX 950M'

这告诉我 CUDA 可用并且可以在您的设备之一 (GPU) 中使用.目前,设备 0 或 GPU GeForce GTX 950M 正在被 PyTorch 使用.

This tells me CUDA is available and can be used in one of your devices (GPUs). And currently, Device 0 or the GPU GeForce GTX 950M is being used by PyTorch.

这篇关于如何检查 pytorch 是否正在使用 GPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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