现在位置: 首页 > PyTorch 教程 > 正文

PyTorch torch.is_available 函数


Pytorch torch 参考手册 Pytorch torch 参考手册

torch.is_available 是 PyTorch 中用于检查 CUDA 是否可用的函数。返回 True 表示 CUDA 可用,False 表示不可用。

函数定义

torch.is_available()

使用示例

实例

import torch

# 检查 CUDA 是否可用
if torch.is_available():
    print("CUDA is available!")
    print(f"CUDA device count: {torch.cuda.device_count()}")
else:
    print("CUDA is not available")

Pytorch torch 参考手册 Pytorch torch 参考手册