python如何确定在代码运行过程中所使用的GPU型号并把型号print出来?
要确定代码运行过程中所使用的GPU型号并将其打印出来,可以使用torch.cuda.get_device_name()函数。
下面是一个示例代码:
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if device.type == "cuda":
print(torch.cuda.get_device_name(0))
else:
print("No GPU available.")
此代码首先检查是否有可用的GPU,然后使用torch.cuda.get_device_name(0)获取第一个可用GPU的名称,并将其打印出来。如果没有可用的GPU,则打印出"No GPU available."。
原文地址: https://www.cveoy.top/t/topic/iHzt 著作权归作者所有。请勿转载和采集!