PyTorch调用GPU
时间: 2023-06-02 11:08:26 浏览: 110
pytorch gpu版
可以通过以下方式调用GPU:
```python
import torch
# 检查是否有可用的GPU
if torch.cuda.is_available():
device = torch.device("cuda")
print(f'There are {torch.cuda.device_count()} GPU(s) available.')
print(f'Using GPU: {torch.cuda.get_device_name(0)}') # 选择第一个GPU
else:
print('No GPU available, using CPU instead.')
device = torch.device("cpu")
```
然后,将模型和数据移动到GPU上进行计算:
```python
model.to(device)
inputs = inputs.to(device)
masks = masks.to(device)
outputs = model(inputs, attention_mask=masks)
```
阅读全文