好的习惯还是在虚拟环境中安装,先使用conda创建一个
conda create -n tfgpu python=3.7
然后进入环境
activate tfgpu
安装带GPU的Tensorflow,当然也可以指定版本来安装,指定镜像站点要快
pip install -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com --trusted-host pypi.douban.com tensorflow_gpu
然后就来测试下是否安装成功
- import tensorflow as tf
- # 查看gpu和cpu的数量
- gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
- cpus = tf.config.experimental.list_physical_devices(device_type='CPU')
- print(gpus, cpus)
不出所料的话,一般都是如下的错误信息:
2022-10-21 12:39:38.439442: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudnn64_8.dll'; dlerror: cudnn64_8.dll not found
2022-10-21 12:39:38.439689: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1934] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
cudnn64_8.dll没有找到,我们可以来到这个地方复制:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\bin
当然这里看自己安装的版本不一样有点区别,我这里是v10.2版本,你们根据自己的情况去找。
将cudnn64_8.dll文件拷贝到 C:\Windows\System
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')] [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]
如果还不可以的话,那就重新安装cudnn
conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0
- from tensorflow.python.client import device_lib
- # 列出全部的本地机器设备
- local_device_protos = device_lib.list_local_devices()
- print(local_device_protos)
- '''
- [name: "/device:CPU:0"
- device_type: "CPU"
- memory_limit: 268435456
- locality {
- }
- incarnation: 1203727834508357835
- xla_global_id: -1
- , name: "/device:GPU:0"
- device_type: "GPU"
- memory_limit: 1389330023
- locality {
- bus_id: 1
- links {
- }
- }
- incarnation: 4726776883789372381
- physical_device_desc: "device: 0, name: NVIDIA GeForce GTX 1050, pci bus id: 0000:01:00.0, compute capability: 6.1"
- xla_global_id: 416903419
- ]
- '''
- # 只打印GPU设备
- [x for x in local_device_protos if x.device_type == 'GPU']
- '''
- [name: "/device:GPU:0"
- device_type: "GPU"
- memory_limit: 1389330023
- locality {
- bus_id: 1
- links {
- }
- }
- incarnation: 4726776883789372381
- physical_device_desc: "device: 0, name: NVIDIA GeForce GTX 1050, pci bus id: 0000:01:00.0, compute capability: 6.1"
- xla_global_id: 416903419
- ]
- '''