1In [1]: import torch
2
3In [2]: torch.cuda.current_device()
4Out[2]: 0
5
6In [3]: torch.cuda.device(0)
7Out[3]: <torch.cuda.device at 0x7efce0b03be0>
8
9In [4]: torch.cuda.device_count()
10Out[4]: 1
11
12In [5]: torch.cuda.get_device_name(0)
13Out[5]: 'GeForce GTX 950M'
14
15In [6]: torch.cuda.is_available()
16Out[6]: True
17
1import torch
2
3torch.cuda.is_available()
4>>> True
5
6torch.cuda.current_device()
7>>> 0
8
9torch.cuda.device(0)
10>>> <torch.cuda.device at 0x7efce0b03be0>
11
12torch.cuda.device_count()
13>>> 1
14
15torch.cuda.get_device_name(0)
16>>> 'GeForce GTX 950M'
17
1device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
2#或device = torch.device("cuda:0")
3device1 = torch.device("cuda:1")
4for batch_idx, (img, label) in enumerate(train_loader):
5 img=img.to(device)
6 label=label.to(device)
7