ubuntu16.04 从源码安装tensorflow-gpu r1.4版本---2017-12-07

本文详细介绍了如何从源码编译安装TensorFlow,并配置GPU支持。包括安装依赖库、配置环境变量、构建pip包及测试等步骤。
声明:以下文章基于https://www.cnblogs.com/lindaxin/p/7797477.html修改,感谢原作者
参考文章:https://www.tensorflow.org/install/install_sources,可以首先按照其中的说明安装依赖项,具体步骤参考下面
本文假设已安装好cuda 8.0和cudnn 6.0,安装参考文章:https://zhuanlan.zhihu.com/p/27890924

1、安装bazel
参考官网这里https://docs.bazel.build/versions/master/install-ubuntu.html#install-on-ubuntu,只有三步,很简单

 

 2、安装第三方库

在terminal中输入以下命令,务必参考官方教程:https://www.tensorflow.org/install/install_sources

1
2
sudo apt-get install python-numpy swig python-dev python-wheel #安装第三方库
sudo apt-get install git

 3、安装tensorflow

在terminal中输入以下命令,下载tensorflow源码

1
git clone https://github.com/tensorflow/tensorflow

 在terminal中输入以下命令:执行配置文件,对安装步骤进行配置

1
2
cd ~/tensorflow #切换到tensorflow文件夹
./configure #执行configure文件

由于一直在更新, 下面的配置步骤是2个月之前的,不一定完整,具体可搜索相关信息,一般遇到不太认识的就选n

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Do you wish to use jemalloc as the malloc implementation? [Y/n] y
jemalloc enabled
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 the XLA just-in-time compiler (experimental)? [y/N] n
No XLA JIT support will be enabled for TensorFlow
Found possible Python library paths:
  /usr/lib/python2.7/site-packages
  /usr/lib64/python2.7/site-packages
Please input the desired Python library path to use.  Default is [/usr/lib/python2.7/site-packages]
 
Using python library path: /usr/lib/python2.7/site-packages
Do you wish to build TensorFlow with OpenCL support? [y/N] n
No OpenCL 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 which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: 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-8.0

 4创建pip,此步骤是从源码编译生成tensorflow的whl文件,可选

在tensorflow的根目录下,在terminal中输入以下命令:
1
2
34
bazel build -c opt //tensorflow/tools/pip_package:build_pip_package
bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg   可以在/tmp/tensorflow_pkg目录中找到生成的whl文件

5、从源码安装tensorflow环境

1
2
3
4
5
6
7
8
bazel build -c opt //tensorflow/tools/pip_package:build_pip_package
 # To build with GPU support:
bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
mkdir _python_build
cd _python_build
ln -s ../bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/org_tensorflow/* .
ln -s ../tensorflow/tools/pip_package/* .
python setup.py develop

 6、tensorflow测试

 

1
2
3
4
5
6
7
8
9
10
11
12
$ python
 
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print sess.run(hello)
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print sess.run(a+b)
42
>>>

 大功告成

出现的错误
操作

1
bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer

 报错

1
2
3
4
5
6
7
8
9
10
ERROR: /home/yaroslavvb/tensorflow.git/tensorflow/tensorflow/core/kernels/BUILD:1080:1: undeclared inclusion(s) in rule '//tensorflow/core/kernels:cwise_op_gpu':
this is missing dependency dependency for following files included by 'tensorflow/core/kernels/cwise_op_gpu_floor.cu.cc':
  '/usr/local/cuda-8.0/include/cuda_runtime.h'
  '/usr/local/cuda-8.0/include/host_config.h'
  '/usr/local/cuda-8.0/include/builtin_types.h'
  '/usr/local/cuda-8.0/include/device_types.h'
  '/usr/local/cuda-8.0/include/host_defines.h'
  '/usr/local/cuda-8.0/include/driver_types.h'
  '/usr/local/cuda-8.0/include/surface_types.h'
  '/usr/local/cuda-8.0/include/texture_types.h'

 可以进入tensorflow/third_party/gpus/crosstool/目录,打开CROSSTOOL文件,搜索cxx_builtin_include_directory,应该有三行,在下面添加行如下 
cxx_builtin_include_directory: "/usr/local/cuda-8.0/include" 

如果出现的错误是类似的,只要将cxx_builtin_include_directory: "/usr/local/cuda-8.0/include"的文件路径改一下就可以了,亲测有效

再次运行上一步的命令,应该就没问题了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值