【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

相关推荐
深蓝海拓21 小时前
PySide6从0开始学习的笔记(十四)创建一个简单的实用UI项目
开发语言·笔记·python·qt·学习·ui·pyqt
Echo_NGC223721 小时前
【传统JSCC+Deep JSCC】联合信源信道编码完全指南
人工智能·python·深度学习·神经网络·conda·无人机·jscc
东方佑21 小时前
使用Python标准库将Word文档转换为HTML:深入解析.docx文件处理脚本
python·html·word
祁思妙想21 小时前
Python中CORS 跨域中间件的配置和作用原理
开发语言·python·中间件
天才测试猿21 小时前
Postman常见问题及解决方法
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman
棒棒的皮皮21 小时前
【OpenCV】Python图像处理形态学之腐蚀
图像处理·python·opencv·计算机视觉
坐吃山猪21 小时前
Python命令行工具argparse
开发语言·python
创作者mateo1 天前
python进阶之文件处理
开发语言·python
前端程序猿之路1 天前
模型应用开发的基础工具与原理之Web 框架
前端·python·语言模型·学习方法·web·ai编程·改行学it