1.GPU设备
print(torch.cuda.is_available())
print(torch.cuda.device_count())
print(torch.cuda.current_device())
print(torch.cuda.get_device_name(0))
2.Tensor on GPU
import torch
x = torch.randn(3)
print(x)
x = x.cuda(0)
print(x)
print(x.device)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
x = torch.randn(3, device=device)
y = torch.randn(3).to(device)
print(x, y)
3.model on GPU
net = torch.nn.Linear(3, 1)
net.cuda(0)
net_device = list(net.parameters())[0].device
print(net_device)
x = torch.tensor([1, 2, 3], dtype=torch.float).cuda()
print(net(x))