【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

相关推荐
Stream_Silver4 分钟前
【安装与配置Anaconda步骤,包含卸载重装】
python·conda
ai_top_trends4 分钟前
AI 生成 PPT 工具横评:效率、质量、稳定性一次说清
人工智能·python·powerpoint
天天睡大觉7 分钟前
Python学习9
开发语言·python·学习
2301_797312268 分钟前
学习Java39天
开发语言·python·学习
曲幽11 分钟前
FastAPI搭档Pydantic:从参数验证到数据转换的全链路实战
python·fastapi·web·path·field·query·pydantic·basemodel·response_model
抠头专注python环境配置23 分钟前
2026终极诊断指南:解决Windows PyTorch GPU安装失败,从迷茫到确定
人工智能·pytorch·windows·深度学习·gpu·环境配置·cuda
Halo_tjn25 分钟前
基于Java的相关知识点
java·开发语言·windows·python·算法
ghostmen28 分钟前
SpringBoot + Vue 实现 Python 在线调试器 - 技术方案文档
java·python·vue·springboot
阳光九叶草LXGZXJ1 小时前
达梦数据库-学习-43-定时备份模式和删除备份(Python+Crontab)
linux·运维·开发语言·数据库·python·学习
深蓝电商API1 小时前
Scrapy与Splash结合爬取JavaScript渲染页面
javascript·爬虫·python·scrapy