首先查看自己的NVIDIA 版本(如果没有请自行下载一个安装):
参考:https://blog.csdn.net/weixin_41194129?spm=1001.2101.3001.5113
电脑终端输入:nvcc -V
执行结果:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Wed_Oct_23_19:32:27_Pacific_Daylight_Time_2019
Cuda compilation tools, release 10.2, V10.2.89
说明我的版本是V10.2.89,因此要找与之对应的tensorflow-gpu:
TensorFlow - FloydHub Documentationdocs.floydhub.com因此安装gpu命令为:
pip install --index-url https://pypi.douban.com/simple tensorflow-gpu==2.2.0
成功安装后检验一下是不是gpu,建立一个.py文件,在该编译环境下写入:
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
执行结果:
name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 1442306457
locality {
bus_id: 1
links {
}
}
incarnation: 90673536119032307
physical_device_desc: "device: 0, name: GeForce MX130, pci bus id: 0000:02:00.0, compute capability: 5.0"
, name: "/device:XLA_GPU:0"
device_type: "XLA_GPU"
memory_limit: 17179869184
locality {
}
incarnation: 7580331184281744497
physical_device_desc: "device: XLA_GPU device"
]
或者直接使用下面最直观的查询方式:
# import tensorflow as tf
from tensorflow.python.client import device_lib
# 列出所有的本地机器设备
local_device_protos = device_lib.list_local_devices()
# 打印
# print(local_device_protos)
# 只打印GPU设备
[print(x) for x in local_device_protos if x.device_type == 'GPU']
执行结果:
2020-11-29 15:16:26.750915: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/device:GPU:0 with 1375 MB memory) -> physical GPU (device: 0, name: GeForce MX130, pci bus id: 0000:02:00.0, compute capability: 5.0)
2020-11-29 15:16:26.754064: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x178e7306f60 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2020-11-29 15:16:26.754323: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): GeForce MX130, Compute Capability 5.0
name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 1442306457
locality {
bus_id: 1
links {
}
}
incarnation: 13018729120500724821
physical_device_desc: "device: 0, name: GeForce MX130, pci bus id: 0000:02:00.0, compute capability: 5.0"
测试成功!