首页 > 代码库 > 使用亚马逊的云服务器EC2做深度学习(三)配置TensorFlow

使用亚马逊的云服务器EC2做深度学习(三)配置TensorFlow

这是《使用亚马逊的云服务器EC2做深度学习》系列的第三篇文章。

(一)申请竞价实例  (二)配置Jupyter Notebook服务器  (三)配置TensorFlow

技术分享

TensorFlow是Google发布的深度学习框架,支持Python和C++的接口。TensorFlow既可以用于学术研究,也可以用于生产环境。许多Google的内部服务,就使用了TensorFlow,比如Gmail、语音识别等。

网络上TensorFlow的教程也很丰富,官方文档在第一时间就被翻译成来中文。

如果让我来评价一下的话,我会说Google出品必属精品。

 

配置TensorFlow的环境,需要安装很多GPU的驱动,非常繁琐。下面的配置脚本是我根据其它教程提供的脚本修改而来。

TensorFlow的版本是目前的最新版本0.11,Python使用的是Anaconda3发行版,Python的版本是Python3.5。

一个注意事项是,选择AWS EC2的区的时候,尽量选择美国或者欧洲地区,不然下载驱动的输入比较慢,需要耗费很长时间。

(1)更新系统,安装必要文件

# install the required packagessudo apt-get update && sudo apt-get -y upgradesudo apt-get -y install linux-headers-$(uname -r) linux-image-extra-`uname -r`

(2)安装Cuda 7.5

# install cuda 7.5CUDA_FILE=cuda-repo-ubuntu1404_7.5-18_amd64.debwget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1404/x86_64/${CUDA_FILE}sudo dpkg -i ${CUDA_FILE}rm ${CUDA_FILE}sudo apt-get updatesudo apt-get install -y cuda-7-5

(3)安装cudnn 5.1

# get cudnn 5.1CUDNN_FILE=cudnn-7.5-linux-x64-v5.1.tgzwget http://developer.download.nvidia.com/compute/redist/cudnn/v5.1/${CUDNN_FILE}tar xvzf ${CUDNN_FILE}rm ${CUDNN_FILE}sudo cp cuda/include/cudnn.h /usr/local/cuda/include # move library files to /usr/local/cudasudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*rm -rf cuda

(4)添加环境变量

# set the appropriate library pathecho export CUDA_HOME=/usr/local/cudaexport CUDA_ROOT=/usr/local/cudaexport PATH=$PATH:$CUDA_ROOT/bin:$HOME/binexport LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CUDA_ROOT/lib64 >> ~/.bashrc

(5)安装Anaconda

# install anacondaANACONDA_FILE=Anaconda3-4.2.0-Linux-x86_64.shwget https://repo.continuum.io/archive/${ANACONDA_FILE}bash ${ANACONDA_FILE} -b -p /mnt/bin/anaconda3rm ${ANACONDA_FILE}echo export PATH="/mnt/bin/anaconda3/bin:$PATH" >> ~/.bashrc

(6)安装TensorFlow

# install tensorflowTF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.11.0rc0-cp35-cp35m-linux_x86_64.whl/mnt/bin/anaconda3/bin/pip install $TF_BINARY_URL

exec bash

 

下面是完整的配置脚本:

#!/bin/bash# stop on errorset -e############################################# install into /mnt/binsudo mkdir -p /mnt/binsudo chown ubuntu:ubuntu /mnt/bin# install the required packagessudo apt-get update && sudo apt-get -y upgradesudo apt-get -y install linux-headers-$(uname -r) linux-image-extra-`uname -r`# install cuda 7.5CUDA_FILE=cuda-repo-ubuntu1404_7.5-18_amd64.debwget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1404/x86_64/${CUDA_FILE}sudo dpkg -i ${CUDA_FILE}rm ${CUDA_FILE}sudo apt-get updatesudo apt-get install -y cuda-7-5# get cudnn 5.1CUDNN_FILE=cudnn-7.5-linux-x64-v5.1.tgzwget http://developer.download.nvidia.com/compute/redist/cudnn/v5.1/${CUDNN_FILE}tar xvzf ${CUDNN_FILE}rm ${CUDNN_FILE}sudo cp cuda/include/cudnn.h /usr/local/cuda/include # move library files to /usr/local/cudasudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*rm -rf cuda# set the appropriate library pathecho export CUDA_HOME=/usr/local/cudaexport CUDA_ROOT=/usr/local/cudaexport PATH=$PATH:$CUDA_ROOT/bin:$HOME/binexport LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CUDA_ROOT/lib64 >> ~/.bashrc# install anacondaANACONDA_FILE=Anaconda3-4.2.0-Linux-x86_64.shwget https://repo.continuum.io/archive/${ANACONDA_FILE}bash ${ANACONDA_FILE} -b -p /mnt/bin/anaconda3rm ${ANACONDA_FILE}echo export PATH="/mnt/bin/anaconda3/bin:$PATH" >> ~/.bashrc# install tensorflowTF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.11.0rc0-cp35-cp35m-linux_x86_64.whl/mnt/bin/anaconda3/bin/pip install $TF_BINARY_URL# install monitoring programs#sudo wget https://git.io/gpustat.py -O /usr/local/bin/gpustat#sudo chmod +x /usr/local/bin/gpustat#sudo nvidia-smi daemon#sudo apt-get -y install htop# reload .bashrcexec bash

 

使用亚马逊的云服务器EC2做深度学习(三)配置TensorFlow