环境搭建
首先,请确保电脑上已经安装了 Anaconda。如果没有,请按照anaconda官网下载安装
安装和使用可参考:http://t.csdnimg.cn/lCJ1Ihttp://t.csdnimg.cn/lCJ1I
继续去官网安装cudahttps://developer.nvidia.com/cuda-toolkit-archive和cuDNNhttps://developer.nvidia.com/rdp/cudnn-archive下载对应版本,本机是cuda 12.4和cudnn 11.6
输入以下命令来创建一个名为 py38_test
的新虚拟环境(名称自己定),指定 Python 版本为 3.8.8
conda create --name py38_test python=3.8.8
激活环境:
conda activate py38_test
为了支持 TensorFlow-GPU,我们需要安装CUDA Toolkit 11.3和cuDNN 8.2.1以支持GPU加速
安装 CUDA Toolkit 和 cuDNN:
conda install cudatoolkit=11.3 cudnn=8.2.1
然后安装tensorflow-gpu
pip install tensorflow-gpu==2.10.0 keras==2.10.0 -i https://pypi.tuna.tsinghua.edu.cn/simple/
可能出现的问题
问题一:Loaded runtime CuDNN library: 8.0.4 but source was compiled with: 8.1.0. CuDNN library needs to have matching major version and equa
如果安装2.10版本的 tensorflow
同时也会报错:WARNING:tensorflow:Layer lstm will not use cuDNN kernels since it doesn't meet the criteria. It will use a generic GPU kernel as fallback when running on GPU.
问题二:device_lib.list_local_devices()只有一个cpu
运行以下代码发现只有cpu一个设备
from tensorflow.python.client import device_lib
print("local_devices:", device_lib.list_local_devices())
命令行查看版本
nvidia-smi
切换gpu版本
程序默认使用最后安装的tensorflow2.10.0版本,我先安装的tensorflow-gpu,后安装的tensorflow2.10.0,所以默认使用的是tensorflow2.10.0,卸载tensorflow-gpu:
pip uninstall tensorflow
重新安装
pip install tensorflow-gpu==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
安装指定版本,不然cuda和cudnn不兼容
问题三:DEBUG:tensorflow:Layer lstm will use cuDNN kernels when running on GPU.
可尝试以下方法
http://t.csdnimg.cn/ANoU6http://t.csdnimg.cn/ANoU6