关于搭建TensorFlow,其实官方的配置指南其实写得已经很清楚了,但是其中并没有比较详细的GPU相关的配置方法,于是在Google一番之后,发现了一篇写得非常详细的教程——從AWS搭一個GPU運算環境來玩tensorflow。在此对两篇教程中提及的步骤以及涉及的命令做个简单的总结,方便以后再有类似需求的时候,可以快速地完成环境搭建。
-
首先是申请一个Instance,初次申请记得提交case上调Instance Limit;
-
ssh之前,记得把密钥权限改成600;
-
登陆之后首先确认一下GPU信息:
$(local) lspci | grep -i nvidia
- GPU方面需要安装的东西有两个:NVIDIA CUDA Toolkit 和 cuDNN library;
- 首先是 CUDA Toolkit:
$ wget https://developer.nvidia.com/compute/cuda/8.0/prod/local_installers/cuda_8.0.44_linux-run
$ chmod 755 cuda_8.0.44_linux-run
$ ./cuda_8.0.44_linux-run -extract=/root
$ ./NVIDIA-Linux-x86_64-367.48.run -s
$ ./cuda-linux64-rel-8.0.44-21122537.run -noprompt
$ vim ~/.bashrc
#增加下面三行
export CUDA_ROOT=/usr/local/cuda-8.0
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64
export PATH=$PATH:$CUDA_ROOT/bin
$ source ~/.bashrc
#测试一下
$ nvidia-smi -q | head
- 然后是cuDNN library:
首先去https://developer.nvidia.com/cudnn 注册Nvidia Developer的账号,然后在本地下载好相应的tgz包,将它scp到服务器上去。
$ tar -zxvf cudnn-8.0-linux-x64-v5.1.tgz
$ cp cuda/lib64/* /usr/local/cuda-8.0/lib64/
$ cp cuda/include/* /usr/local/cuda-8.0/include/
至此,GPU Drivers的部分基本配置完成。
- 然后是TensorFlow的配置,这里采用官方推荐的Installing with virtualenv的方法:
# Install pip and virtualen
$ sudo apt install python-pip python-dev python-virtualenv # for Python 2.7
$ sudo apt install python3-pip python3-dev python-virtualenv # for Python 3.n
# Create a virtualenv environment
$ virtualenv --system-site-packages targetDirectory # for Python 2.7
$ virtualenv --system-site-packages -p python3 'targetDirectory' # for Python 3.n
# 'targetDirectory' specifies the top of the virtualenv tree, which you may choose by yourself.
# Activate the virtualenv environment
$ source ~/tensorflow/bin/activate # bash, sh, ksh, or zsh
$ source ~/tensorflow/bin/activate.csh # csh or tcsh
#现在的命令行前面的标识会变成这个样子:
(tensorflow)$
#Install TensorFlow, ensure the version of pip >= 8.1
(tensorflow)$ pip install --upgrade tensorflow # for Python 2.7
(tensorflow)$ pip3 install --upgrade tensorflow # for Python 3.n
(tensorflow)$ pip install --upgrade tensorflow-gpu # for Python 2.7 and GPU
(tensorflow)$ pip3 install --upgrade tensorflow-gpu # for Python 3.n and GPU
至此,TensorFlow安装完成
-
激活命令为
$ source ~/tensorflow/bin/activate # bash, sh, ksh, or zsh $ source ~/tensorflow/bin/activate.csh # csh or tcsh
-
退出环境
(tensorflow)$ deactivate
-
卸载
$ rm -r targetDirectory