查看电脑torch能否使用GPU
- import torch
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
-
- print(device) #cpu
-
- import torch
- flag = torch.cuda.is_available()
- print(flag)
-
- ngpu= 1
- # Decide which device we want to run on
- device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
- print(device)
- print(torch.cuda.get_device_name(0))
- print(torch.rand(3,3).cuda())
-
-
- True
- cuda:0
- NVIDIA GeForce GTX 1650
- tensor([[0.6134, 0.0682, 0.9818],
- [0.2449, 0.2887, 0.8753],
- [0.8587, 0.6035, 0.9593]], device='cuda:0')
-