《TensorFlow技术解析与实战》——2.4 从源代码安装

本节书摘来异步社区《TensorFlow技术解析与实战》一书中的第2章,第2.4节,作者:李嘉璇,更多章节内容可以访问云栖社区“异步社区”公众号查看。

2.4 从源代码安装

从源代码编译安装,需要使用Bazel编译工具。我们先安装Bazel工具。在需要依赖的JDK 8配好之后,在Mac笔记本上直接执行下面命令,安装版本是0.4.4:

brew install bazel```
其他操作系统(如Ubuntu)的计算机对Bazel的安装,可以采用apt-get等方式。

先进入tensorflow-1.1.0的源代码目录,运行./configure脚本会出现所采用的Python路径、是否用HDFS、是否用Google Cloud Platform等选项,读者可以根据自己的需要进行配置,或者直接按“回车”采用默认配置。

下面我们演示使用CPU版本的编译。具体如下:

tensorflow-1.1.0 ./configure
Please specify the location of python.[Default is /usr/local/bin/python]:
Please specify optimization flags to use during compilation [Default is -march=native]:
Do you wish to use jemalloc as the malloc implementation? (Linux only) [Y/n]
jemalloc enabled on Linux
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N]
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/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]
No XLA support will be enabled for TensorFlow
Found possible Python library paths:
/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Python/2.7/site-packages
Please input the desired Python library path to use. Default is [/usr/local/Cellar/ python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages]

Using python library path: /usr/local/Cellar/python/2.7.12_2/Frameworks/Python. framework/Versions/2.7/lib/python2.7/site-packages
Do you wish to build TensorFlow with OpenCL support? [y/N]
No OpenCL support will be enabled for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N]
No CUDA support will be enabled for TensorFlow
Configuration finished

随后,我们执行bazel编译命令,因为编译时需要耗费大量的内存,加入--local_resources 2048,4,1.0来限制内存大小。具体如下:

bazel build --local_resources 2048,4,1.0 -c opt //tensorflow/tools/pip_package:build_ pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
然后进入/tmp/tensorflow_pkg,可以看到生成的文件tensorflow-1.1.0-cp27-c

p27m-macosx_10_12_intel.whl,直接安装如下:

pip install /tmp/tensorflow_pkg/tensorflow-1.1.0-cp27-cp27m-macosx_10_12_intel.whl`
使用GPU版本的编译需要配置中选择使用CUDA,然后填写对应的CUDA SDK版本等,其他步骤均相同。

上一篇:三星9300刷机详解 (转载) 


下一篇:linux和unix常用快捷键