pytorch打印当前设备
时间: 2023-05-24 08:03:34 浏览: 44
A: 在PyTorch中,可以使用以下代码打印当前设备:
```python
import torch
if torch.cuda.is_available():
device = torch.device("cuda") # GPU
print("Current device: ", torch.cuda.get_device_name())
else:
device = torch.device("cpu")
print("Current device: CPU")
```
它将输出当前设备的名称,例如:`Current device: GeForce GTX 1080 Ti`。如果您的系统上没有可用的GPU,则将使用CPU作为默认设备。
相关问题
查看pytorch可用的物理设备
可以使用以下代码查看当前机器上可用的 PyTorch 设备列表:
```
import torch
if torch.cuda.is_available():
device_count = torch.cuda.device_count()
print(f"{device_count} GPUs are available:")
for i in range(device_count):
print(f"GPU {i}: {torch.cuda.get_device_name(i)}")
else:
print("No GPUs available, using CPU instead.")
```
上述代码首先检测当前机器是否可用 GPU,如果可用则打印出可用的 GPU 数量以及每个 GPU 的名称。如果不可用,则使用 CPU 进行计算。
pytorch 测试gpu可用
您可以使用以下代码测试PyTorch是否可以访问GPU:
```python
import torch
if torch.cuda.is_available():
device = torch.device("cuda")
print("GPU可用")
else:
device = torch.device("cpu")
print("GPU不可用")
print("当前设备:", device)
```
这段代码首先检查PyTorch是否可以访问GPU,如果可用,则将设备设置为cuda。否则,将设备设置为cpu。最后,它会打印出当前使用的设备。