linux下tensorflow安装

如无特殊说明,所有操作都在Linux系统上完成,并假定只有普通用户权限。(仅有CUDA和cudnn安装需要用到管理员权限)

一、 安装TensorFlow

1. 使用pip从二进制文件安装

注意:

  • 最新版本的二进制文件链接地址,可以通过TensorFlow官网获取 https://www.tensorflow.org/install/install_linux
  • 请安装最新版的pip,或者使用pip install –upgrade pip来进行更新
  • 如果安装GPU版本,请确认已经提前安装配置好CUDA

Python 2.7

// CPU版本
pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.0-cp27-none-linux_x86_64.whl

// GPU版本
pip install https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.4.0-cp27-none-linux_x86_64.whl

Python 3.4

// CPU版本
pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.0-cp34-cp34m-linux_x86_64.whl

// GPU版本
pip install https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.4.0-cp34-cp34m-linux_x86_64.whl

Python 3.5

// CPU版本
pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.0-cp35-cp35m-linux_x86_64.whl

// GPU版本
pip install https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.4.0-cp35-cp35m-linux_x86_64.whl

Python 3.6

// CPU版本
pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.0-cp36-cp36m-linux_x86_64.whl

// GPU版本
pip install https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.4.0-cp36-cp36m-linux_x86_64.whl

2. 从源码安装

2.1 安装JDK8

从官网http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html下载对应版本。

使用tar命令解压文件

tar zxvf jdk-8u151-linux-x64.tar.gz

使用vi编辑器设置环境变量

# 当前用户(该文件为隐藏文件,可以用ll命令查看)
vi /$HOME/.bashrc

在打开的文件末尾添加下列内容并保存,其中JAVA_HOME为解压路径

export JAVA_HOME=/usr/jdk1.8.0_151
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

使用下述命令让环境变量立即生效:

# 当前用户(该文件为隐藏文件,可以用ll命令查看)
source /$HOME/.bashrc
2.2 安装bazel

首先到Release界面https://github.com/bazelbuild/bazel/releases下载bazel。或者使用下面的命令

wget https://github.com/bazelbuild/bazel/releases/download/0.7.0/bazel-0.7.0-dist.zip

下载完成后解压并进入该目录进行编译:

unzip /home/zhaofan/bazel -d bazel

cd bazel

./compile.sh

安装完成后会在文件夹下生成一个“/output”文件夹,将该路径加入.bashrc中,并使用source立即生效。比如,我的路径为:

export PATH=$PATH:/home/****/3rdparty/bazel/output
2.3 安装CUDA以及cudnn

建议安装CUDA8.0(好像9.0也已经支持了)。CUDA必须以管理员身份安装,否则即使能够成功编译,也会无法正常使用GPU。所以,如果CUDA没有安装,联系你的管理员安装吧。

安装CUDA可以参考http://blog.csdn.net/u013832707/article/details/53157976

建议安装cudnn6或者更高版本,且cudnn文件应和CUDA处于同一目录,即:

cudnn.h应位于cuda/include中, libcudnn.\文件应位于cuda\lib64中

如果需要升级替换较低版本的cudnn,可以参考《ubuntu14.04更改cudnn版本》

2.4 在Anaconda基础上安装tensorflow

为了避免非管理员无法升级安装最新版本python包,建议使用Anaconda。Anaconda的安装方法可参考《 linux下安装Anaconda》

下载tensorflow最新版源码:

git clone --recurse-submodules https://github.com/tensorflow/tensorflow

进入tensorflow根目录中,执行

./configure 

按照提示输入y/n或者路径等信息。需要注意的有:python路径,开启GPU支持,CUDA路径、CUDNN路径等。具体如下:

WARNING: Running Bazel server needs to be killed, because the startup options are different.
You have bazel 0.7.0- (@non-git) installed.
Please specify the location of python. [Default is /home/zhaofan/anaconda3/bin/python]: 


Found possible Python library paths:
  /home/zhaofan/anaconda3/lib/python3.6/site-packages
Please input the desired Python library path to use.  Default is [/home/zhaofan/anaconda3/lib/python3.6/site-packages]

Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: n
No jemalloc as malloc support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: n
No Google Cloud Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: n
No Hadoop File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: n
No Amazon S3 File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with XLA JIT support? [y/N]: n
No XLA JIT support will be enabled for TensorFlow.

Do you wish to build TensorFlow with GDR support? [y/N]: n
No GDR support will be enabled for TensorFlow.

Do you wish to build TensorFlow with VERBS support? [y/N]: n
No VERBS support will be enabled for TensorFlow.

Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: n
No OpenCL SYCL support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]: y
CUDA support will be enabled for TensorFlow.

Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 8.0]: 


Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 


Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]: 6


Please specify the location where cuDNN 6 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:


Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 6.1,6.1,6.1,6.1]


Do you want to use clang as CUDA compiler? [y/N]: n
nvcc will be used as CUDA compiler.

Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: 


Do you wish to build TensorFlow with MPI support? [y/N]: n
No MPI support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: 


Add "--config=mkl" to your bazel command to build with MKL support.
Please note that MKL on MacOS or windows is still not supported.
If you would like to use a local MKL instead of downloading, please set the environment variable "TF_MKL_ROOT" every time before build.
Configuration finished

配置完成后执行:

bazel build --config=opt --config=cuda --config=monolithic //tensorflow/tools/pip_package:build_pip_package

上面的过程比较耗时,结束后继续执行:

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

最后安装生成的whl文件:

pip install /tmp/tensorflow_pkg/tensorflow-1.4.0-cp36-cp36m-linux_x86_64.whl

安装完成后,退出tensorflow目录,打开python,执行

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

如果可以成功打印,则表明安装成功。

3. 遇到的一些问题

3.1 关于开启MKL支持

编译时增加mkl标志,即可自动下载所需的mkl:可惜国内网速很可能导致此步失败

bazel build --config=opt --config=cuda --config=monolithic --config=mkl//tensorflow/tools/pip_package:build_pip_package

或者自己下载mkl安装,然后设置TF_MKL_ROOT变量。 可惜这个方法我也没成功

3.2 找不到动态库

比如下面的错误:

ImportError: libcusolver.so.8.0: cannot open shared object file: No such file or directory

解决方法,将库所在路径加入环境变量。

编辑.bashrc文件:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64
阅读更多
版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/shuzfan/article/details/78516542
文章标签: tensorflow deep-learning
个人分类: TensorFlow
上一篇linux下安装Anaconda
下一篇如何在tensorflow中屏蔽Debug信息
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭