搭建tensorflow2.x环境
- 首先创建一个新的环境
conda create -n 环境名称 python=xx(python版本)
- 激活环境
conda activate 刚才创建的环境名称
- 安装tensorflow2.xpip install tensorflow==2.x
pip install tensorflow==2.x
若安装非常慢,可以使用镜像网站安装
pip install tensorflow==2.x -i https://pypi.tuna.tsinghua.edu.cn/simple
国内镜像网址
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/
网址来源:解决安装tensorflow等速度慢,超时 - whw1314 - 博客园 (cnblogs.com)
- 安装好之后还需要安装与TensorFlow版本对应的cuda和cudnn库,否则不能正确调用gpu,要查看版本的对应,可以在tensorflow的官方网站:https://tensorflow.google.cn/install/source?hl=zh-cn
https://tensorflow.google.cn/install/source?hl=zh-cn
- 安装cudatoolkit和cudnn库,以cudatoolkit=11.0和cudnn=8.0为例,利用如下命令
conda install cudatoolkit==11.0
conda install cudnn==8.0
若找不到对应版本的cudatoolkit和cudnn,则可以使用如下代码,查看具体的版本号
conda search cudnn -c conda-forge
会出现如下版本,在其中找到自己需要的版本,再次进行安装,安装命令如下。
conda install cudnn==8.0.5.39 -c conda-forge
conda install cudatoolkit==11.0.3 -c conda-forge
- 最后查看gpu是否可以使用,输入如下命令
python
import tensorflow as tf
gpu_out=tf.config.list_physical_devices('GPU')
print(gpu_out)
exit()
搭建tensorflow1.x环境
步骤同上,所以只写了完整的代码,以安装tensorflow-gpu-1.14.0+cuda10.0+cudnn7.4为例。
conda create -n tf python=3.5
conda activate tf
pip install tensorflow_gpu==1.14.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
conda install cudatoolkit==10.0
conda install cudnn
(或
conda search cudatoolkit -c conda-forge
conda search cudnn -c conda-forge
conda install cudatoolkit==7.4 -c conda-forge
conda install cudnn==10.0.130 -c conda-forge
)
python
import tensorflow as tf
tf.test.gpu_device_name()
exit()
在实际安装的时候找不到cudnn7.4,所以直接使用了conda install cudnn命令,安装的cudnn版本为7.6.5,在使用时,需要将protobuf降低版本在3.20.x之下,降低之后也可以正常使用,不知道是不是cudnn版本能够向下兼容。后面使用vscode调试代码的时候还将gast版本降为了0.2.2。
但是在3090上再次安装此虚拟环境时,却一直遇到问题,包括tensorflow2.x版本。
2023.9.20更新,环境的问题解决了,3090上使用tensorflow2.15.0+python3.9+cudnn8.1+cudatoolkit11.2,解决的过程中看到3090最好配置cudatoolkit11.2以上
参考资料:
【仙女踩坑实录】conda找不到高版本(8.0)的cudnn_cudnn 8.0找不到_vivigreeeen的博客-CSDN博客