【Python】pytorch,CUDA是否可用,查看显卡显存剩余容量

CUDA可用,共有 1 个GPU设备可用。

当前使用的GPU设备索引:0

当前使用的GPU设备名称:NVIDIA T1000

GPU显存总量:4.00 GB

已使用的GPU显存:0.00 GB

剩余GPU显存:4.00 GB

PyTorch版本:1.10.1+cu102

python 复制代码
import torch

# 检查CUDA是否可用
cuda_available = torch.cuda.is_available()

if cuda_available:
    # 获取GPU设备数量
    num_gpu = torch.cuda.device_count()

    # 获取当前使用的GPU索引
    current_gpu_index = torch.cuda.current_device()

    # 获取当前GPU的名称
    current_gpu_name = torch.cuda.get_device_name(current_gpu_index)

    # 获取GPU显存的总量和已使用量
    total_memory = torch.cuda.get_device_properties(current_gpu_index).total_memory / (1024 ** 3)  # 显存总量(GB)
    used_memory = torch.cuda.memory_allocated(current_gpu_index) / (1024 ** 3)  # 已使用显存(GB)
    free_memory = total_memory - used_memory  # 剩余显存(GB)

    print(f"CUDA可用,共有 {num_gpu} 个GPU设备可用。")
    print(f"当前使用的GPU设备索引:{current_gpu_index}")
    print(f"当前使用的GPU设备名称:{current_gpu_name}")
    print(f"GPU显存总量:{total_memory:.2f} GB")
    print(f"已使用的GPU显存:{used_memory:.2f} GB")
    print(f"剩余GPU显存:{free_memory:.2f} GB")
else:
    print("CUDA不可用。")

# 检查PyTorch版本
print(f"PyTorch版本:{torch.__version__}")

windows先装显卡驱动,再装CUDA10.2,最后装了pytorch。

pip install torch1.10.1+cu102 torchvision0.13.1+cu102 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu102

相关推荐
郝学胜-神的一滴8 分钟前
机器学习数据工程之基石:论数据集划分之道与sklearn实践
开发语言·人工智能·python·程序人生·机器学习·sklearn
轻竹办公PPT9 分钟前
AI生成PPT好用吗?工作总结场景下的工具排名更新
人工智能·python·powerpoint
SCBAiotAigc10 分钟前
bash前面的(base)实现的原理
人工智能·python·ubuntu
wa的一声哭了11 分钟前
内积空间 内积空间二
java·开发语言·python·spring·java-ee·django·maven
小蒜学长20 分钟前
python餐厅点餐系统(代码+数据库+LW)
数据库·spring boot·后端·python
山土成旧客21 分钟前
【Python学习打卡-Day34】GPU为何“变慢”?从性能悖论到`__call__`的魔力
开发语言·python·学习
水龙吟啸24 分钟前
项目设计与开发:智慧校园食堂系统
python·机器学习·前端框架·c#·团队开发·visual studio·数据库系统
极客小云29 分钟前
【IEEE Transactions系列期刊全览:计算机领域核心期刊深度解析】
android·论文阅读·python
じ☆冷颜〃39 分钟前
基于多数据结构融合的密码学性能增强框架
数据结构·经验分享·笔记·python·密码学
无所事事的海绵宝宝42 分钟前
python基础
开发语言·python