Device =torch.device
时间: 2024-05-16 19:13:44 浏览: 116
Pytorch to(device)用法
= torch.device is a function in PyTorch that returns a device object representing the device on which the tensor is allocated. The device can be "cpu" or "cuda" for GPU. It is commonly used to move tensors between CPU and GPU. Here is an example of how to use it:
```
import torch
# Move tensor to GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
x = torch.randn(3, 3).to(device)
# Move tensor back to CPU
x = x.to("cpu")
```
阅读全文