import torch
# setting device on GPU if available, else CPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('Using device:', device)
#Additional Info when using cuda
if device.type == 'cuda':
print(f'current_device: {torch.cuda.current_device()}\n')
for i in range(torch.cuda.device_count()):
print(f'device {i}: ', torch.cuda.get_device_name(i))
print(' Allocated:', round(torch.cuda.memory_allocated(i)/1024**3,1), 'GB')
print(' Cached: ', round(torch.cuda.memory_reserved(i)/1024**3,1), 'GB')
查看tensorflow是否可用GPU
import tensorflow as tf
print(tf.test.is_gpu_available())
print(tf.test.gpu_device_name())
from tensorflow.python.client import device_lib
# 列出所有的本地机器设备
local_device_protos = device_lib.list_local_devices()
# 只打印GPU设备
[print(x) for x in local_device_protos if x.device_type == 'GPU']