WXL's blog

Talk is cheap, show me your work.

0%

miniconda+tf2.x_gpu+torch_gpu环境配置

情况描述

需要运行联邦学习图神经网络,同时需要torch和tf2.x,运行图神经网络还用到了stellargraph库,这个库依赖于tf,而且要求tf版本>=2.1.0,在代码内部还用到了keras,需要keras和tf版本相匹配。

版本适配

首先弄清需要安装的包:torch, tf2.x, stellargraph, cudatoolkit, cudnn。

为了不让自己所需要的cuda环境影响到服务器中其他用户的使用,我通过conda安装cudatoolkit来解决。

通过查找发现,torch和tf2.x共用同一个版本的cuda而且tf版本>2.1.0的就发现一个:CUDA 10.1

当然我只是通过官方提供的下载指令查找的,如果还是找不到自己需要的版本适配,可以通过这个网址自己凑出来torch、torchvision、cuda版本(我没试过,可以尝试一下)。

下面提供官方下载指令,但是不是下载流程,下载流程请继续往下看。

官方下载torch指令:

1
conda install pytorch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 cudatoolkit=10.1 -c pytorch

下载tf2.3指令:

1
pip install tensorflow-gpu==2.3.1

安装流程

安装miniconda:

下载安装包:

1
wget -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-4.6.14-Linux-x86_64.sh

安装miniconda:

1
bash Miniconda3-4.6.14-Linux-x86_64.sh

安装过程中注意这一步选择yes,不要一直回车给按过去了:

miniconda安装注意点

安装好了之后,可以看到miniconda3的目录路径,比如我的是:/home/me/miniconda3。

输入vi ~/.bashrc,在文件底下插入如下内容:

1
export PATH=~/home/me/miniconda3/bin:$PATH

然后激活环境变量:

1
source ~/.bashrc

即可通过conda --version查看版本。

添加镜像

添加pip镜像:

1
2
3
4
mkdir ~/.pip
cd ~/.pip
touch pip.conf
vi ~/.pip/pip.conf

将下面的清华镜像加入该文件并保存:

1
2
3
4
5
6
[global]
timeout = 60000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
use-mirrors = true
mirrors = https://pypi.tuna.tsinghua.edu.cn

添加conda镜像:

1
2
3
conda config --set show_channel_urls yes
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/

创建新的虚拟环境

创建名为cuda101,python版本为3.7.5的虚拟环境:

1
conda create -n cuda101 python=3.7.5

安装相关包

安装下面几个包的顺序非常重要。

先安装pytorch,下面的指令会顺带下载cudatoolkit 10.1:

1
conda install pytorch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 cudatoolkit=10.1 -c pytorch

然后下载tf:

1
pip install tensorflow-gpu==2.3.1

安装stellargraph:

1
pip install stellargraph

安装keras:

1
pip install keras==2.4.3

安装cudnn:

1
conda install cudnn=7.6.5

然后可以试试导入tf和torch是否成功了。

【参考】

https://blog.csdn.net/weixin_43141320/article/details/107955115

https://blog.csdn.net/weixin_43141320/article/details/108343528

https://tensorflow.google.cn/install/source

https://blog.csdn.net/qq_37774098/article/details/109895048

https://blog.csdn.net/qq_42406643/article/details/109545766?utm_medium=distribute.pc_relevant.none-task-blog-baidulandingword-2&spm=1001.2101.3001.4242

https://pytorch.org/get-started/previous-versions/

https://tensorflow.google.cn/install/source_windows#gpu

https://pypi.org/project/stellargraph/#install-stellargraph-in-anaconda-python

行行好,赏一杯咖啡吧~