Tensorflow 源码安装 ubuntu16.04 GPU

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/majinlei121/article/details/79170831

Tensorflow 安装与使用 (ubuntu16.04 GPU)

%#################################################################%

由于计算机里已安装 caffe, cuda, cudnn, 为了安装tensorflow后不影响其使用,所以在anaconda中新建了一个环境,名称:tensorflow, 建完之后选择 numpy 安装

这里写图片描述

%#################################################################%

查看cuda版本 cat /usr/local/cuda/version.txt
查看cudnn版本 cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

本计算机 cuda: 8.0 cudnn: 5.1.5

这里写图片描述

%#################################################################%

激活环境
source activate tensorflow

下载安装Bazel,Bazel是Google开源构建工具,类似于Make的工具,用来编译构建tensorflow

官网提供的地址一直连接不上,放在百度网盘里(bazel-0.5.4-installer-linux-x86_64.sh):https://pan.baidu.com/s/1o93507o

放在主目录文件夹 /tensorflow_install 下,执行

cd /home/majinlei/tensorflow_install

chmod +x bazel-0.5.4-installer-linux-x86_64.sh

./bazel-0.5.4-installer-linux-x86_64.sh --user

export PATH="$PATH:$HOME/bin"

执行上述 4 个命令之后开始配置(不要使用sudo ./configure)

./configure

具体配置选择如下:

(tensorflow) majinlei@majinlei:~/tensorflow$ ./configure
WARNING: Running Bazel server needs to be killed, because the startup options are different.
You have bazel 0.5.4 installed.
Please specify the location of python. [Default is /home/majinlei/anaconda2/envs/tensorflow/bin/python]: /home/majinlei/anaconda2/envs/tensorflow/bin/python


Found possible Python library paths:
  /usr/local/lib/python2.7/site-packages
  /home/majinlei/anaconda2/envs/tensorflow/lib/python2.7/site-packages
Please input the desired Python library path to use.  Default is [/usr/local/lib/python2.7/site-packages]
/home/majinlei/anaconda2/envs/tensorflow/lib/python2.7/site-packages
Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: y
jemalloc as malloc support will be enabled for TensorFlow.

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

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

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

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

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

Do you wish to build TensorFlow with VERBS support? [y/N]: y
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 9.0]: 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]: /usr/local/cuda


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


Please specify the location where cuDNN 5.1.5 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:/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


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]: /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]: -match=native


Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: n
Not configuring the WORKSPACE for Android builds.

Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See tools/bazel.rc for more details.
    --config=mkl            # Build with MKL support.
    --config=monolithic     # Config for mostly static monolithic build.
Configuration finished

%#################################################################%

接下来下载tensorflow源码(下载至主目录tensorflow文件夹下)

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

使用Bazel编译构建

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

中途出现错误:

ERROR: /home/majinlei/tensorflow/tensorflow/contrib/gdr/BUILD:52:1: C++ compilation of rule '//tensorflow/contrib/gdr:gdr_memory_manager' failed (Exit 1).
tensorflow/contrib/gdr/gdr_memory_manager.cc:28:27: fatal error: rdma/rdma_cma.h: No such file or directory

解决办法:

sudo apt-get install librdmacm-dev

重新输入命令:

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

进行编译,编译过程时间很长, 等待完成之后

生成 pip 安装包

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

cd 到 /tmp/tensorflow_pkg目录下,找到编译好的whl文件,使用PIP工具安装

cd /tmp/tensorflow_pkg

pip install --config=cuda /tmp/tensorflow_pkg/tensorflow-1.5.0rc1-cp27-cp27mu-linux_x86_64.whl

安装过程如下

(tensorflow) majinlei@majinlei:/tmp/tensorflow_pkg$ pip install /tmp/tensorflow_pkg/tensorflow-1.5.0rc1-cp27-cp27mu-linux_x86_64.whl
Processing ./tensorflow-1.5.0rc1-cp27-cp27mu-linux_x86_64.whl
Collecting enum34>=1.1.6 (from tensorflow==1.5.0rc1)
  Downloading enum34-1.1.6-py2-none-any.whl
Collecting absl-py>=0.1.6 (from tensorflow==1.5.0rc1)
  Downloading absl-py-0.1.9.tar.gz (79kB)
    100% |████████████████████████████████| 81kB 176kB/s 
Collecting six>=1.10.0 (from tensorflow==1.5.0rc1)
  Using cached six-1.11.0-py2.py3-none-any.whl
Collecting tensorflow-tensorboard>=0.4.0 (from tensorflow==1.5.0rc1)
  Downloading tensorflow_tensorboard-1.5.0-py2-none-any.whl (3.0MB)
    100% |████████████████████████████████| 3.0MB 71kB/s 
Collecting termcolor>=1.1.0 (from tensorflow==1.5.0rc1)
  Downloading termcolor-1.1.0.tar.gz
Collecting gast>=0.2.0 (from tensorflow==1.5.0rc1)
  Downloading gast-0.2.0.tar.gz
Requirement already satisfied: numpy>=1.12.1 in /home/majinlei/anaconda2/envs/tensorflow/lib/python2.7/site-packages (from tensorflow==1.5.0rc1)
Requirement already satisfied: wheel in /home/majinlei/anaconda2/envs/tensorflow/lib/python2.7/site-packages (from tensorflow==1.5.0rc1)
Collecting backports.weakref>=1.0rc1 (from tensorflow==1.5.0rc1)
  Downloading backports.weakref-1.0.post1-py2.py3-none-any.whl
Collecting astor>=0.6.0 (from tensorflow==1.5.0rc1)
  Downloading astor-0.6.2-py2.py3-none-any.whl
Collecting mock>=2.0.0 (from tensorflow==1.5.0rc1)
  Downloading mock-2.0.0-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 258kB/s 
Collecting protobuf>=3.4.0 (from tensorflow==1.5.0rc1)
  Downloading protobuf-3.5.1-cp27-cp27mu-manylinux1_x86_64.whl (6.4MB)
    100% |████████████████████████████████| 6.4MB 194kB/s 
Collecting werkzeug>=0.11.10 (from tensorflow-tensorboard>=0.4.0->tensorflow==1.5.0rc1)
  Downloading Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
    100% |████████████████████████████████| 327kB 668kB/s 
Collecting html5lib==0.9999999 (from tensorflow-tensorboard>=0.4.0->tensorflow==1.5.0rc1)
  Downloading html5lib-0.9999999.tar.gz (889kB)
    100% |████████████████████████████████| 890kB 1.4MB/s 
Collecting markdown>=2.6.8 (from tensorflow-tensorboard>=0.4.0->tensorflow==1.5.0rc1)
  Downloading Markdown-2.6.11-py2.py3-none-any.whl (78kB)
    100% |████████████████████████████████| 81kB 798kB/s 
Collecting futures>=3.1.1 (from tensorflow-tensorboard>=0.4.0->tensorflow==1.5.0rc1)
  Downloading futures-3.2.0-py2-none-any.whl
Collecting bleach==1.5.0 (from tensorflow-tensorboard>=0.4.0->tensorflow==1.5.0rc1)
  Downloading bleach-1.5.0-py2.py3-none-any.whl
Collecting funcsigs>=1; python_version < "3.3" (from mock>=2.0.0->tensorflow==1.5.0rc1)
  Downloading funcsigs-1.0.2-py2.py3-none-any.whl
Collecting pbr>=0.11 (from mock>=2.0.0->tensorflow==1.5.0rc1)
  Downloading pbr-3.1.1-py2.py3-none-any.whl (99kB)
    100% |████████████████████████████████| 102kB 733kB/s 
Requirement already satisfied: setuptools in /home/majinlei/anaconda2/envs/tensorflow/lib/python2.7/site-packages (from protobuf>=3.4.0->tensorflow==1.5.0rc1)
Building wheels for collected packages: absl-py, termcolor, gast, html5lib
  Running setup.py bdist_wheel for absl-py ... done
  Stored in directory: /home/majinlei/.cache/pip/wheels/04/f5/7c/5d4eab10ddf87dec875016e74ba289d87270a90fb2662a76fc
  Running setup.py bdist_wheel for termcolor ... done
  Stored in directory: /home/majinlei/.cache/pip/wheels/de/f7/bf/1bcac7bf30549e6a4957382e2ecab04c88e513117207067b03
  Running setup.py bdist_wheel for gast ... done
  Stored in directory: /home/majinlei/.cache/pip/wheels/8e/fa/d6/77dd17d18ea23fd7b860e02623d27c1be451521af40dd4a13e
  Running setup.py bdist_wheel for html5lib ... done
  Stored in directory: /home/majinlei/.cache/pip/wheels/6f/85/6c/56b8e1292c6214c4eb73b9dda50f53e8e977bf65989373c962
Successfully built absl-py termcolor gast html5lib
Installing collected packages: enum34, six, absl-py, protobuf, werkzeug, html5lib, markdown, futures, bleach, tensorflow-tensorboard, termcolor, gast, backports.weakref, astor, funcsigs, pbr, mock, tensorflow
Successfully installed absl-py-0.1.9 astor-0.6.2 backports.weakref-1.0.post1 bleach-1.5.0 enum34-1.1.6 funcsigs-1.0.2 futures-3.2.0 gast-0.2.0 html5lib-0.9999999 markdown-2.6.11 mock-2.0.0 pbr-3.1.1 protobuf-3.5.1 six-1.11.0 tensorflow-1.5.0rc1 tensorflow-tensorboard-1.5.0 termcolor-1.1.0 werkzeug-0.14.1

测试(命令行输入)

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

输出

Hello, TensorFlow!

这里写图片描述

猜你喜欢

转载自blog.csdn.net/majinlei121/article/details/79170831