TensorFlow Python2.7环境下的源码编译,三编译

一、源代码编译

这里要为仅支持 CPU 的 TensorFlow 构建一个 pip 软件包,需要调用以下命令:

$ bazel build --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" --config=opt --verbose_failures //tensorflow/tools/pip_package:build_pip_package

提示:默认情况下,从源代码构建 TensorFlow 会消耗大量的 RAM。如果您系统中的 RAM 资源有限,则可以通过在调用 bazel 时指定 --local_resources 2048,.5,1.0 来限制 RAM 使用量。

编译期间因为缺少模块中断了几次,前后大概编译了几个小时,终于编译成功了!

Target //tensorflow/tools/pip_package:build_pip_package up-to-date:

bazel-bin/tensorflow/tools/pip_package/build_pip_package

INFO: Elapsed time: 1226.516s, Critical Path: 54.42s

INFO: 904 processes: 904 local.

INFO: Build completed successfully, 1126 total actions

二、生成whl文件(.whl 文件的名称取决于您的平台)

bazel build 命令会构建一个名为 build_pip_package 的脚本。按如下所示运行此脚本后,一个 .whl 文件将在 /tmp/tensorflow_pkg 目录内构建:

➜ tensorflow git:(master) bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

2018年 8月13日 星期一 02时25分45秒 CST : === Preparing sources in dir: /var/folders/6v/q8bvbf3n209f0_59508px3pr0000gn/T/tmp.XXXXXXXXXX.nuoSUsnJ

/Volumes/DATA/AI/tensorflow /Volumes/DATA/AI/tensorflow

/Volumes/DATA/AI/tensorflow

2018年 8月13日 星期一 02时26分09秒 CST : === Building wheel

warning: no files found matching '*.dll' under directory '*'

warning: no files found matching '*.lib' under directory '*'

warning: no files found matching '*.h' under directory 'tensorflow/include/tensorflow'

warning: no files found matching '*' under directory 'tensorflow/include/Eigen'

warning: no files found matching '*.h' under directory 'tensorflow/include/google'

warning: no files found matching '*' under directory 'tensorflow/include/third_party'

warning: no files found matching '*' under directory 'tensorflow/include/unsupported'

2018年 8月13日 星期一 02时26分42秒 CST : === Output wheel file is in: /tmp/tensorflow_pkg

三、安装

首先找到具体的文件名称

➜ tensorflow git:(master) ll /tmp/tensorflow_pkg

total 98136

-rw-r--r-- 1 mazhiyong wheel 48M 8 13 02:26 tensorflow-1.10.0-cp27-cp27m-macosx_10_13_x86_64.whl

然后启动安装

➜ tensorflow git:(master) sudo pip2 install /tmp/tensorflow_pkg/tensorflow-1.10.0-cp27-cp27m-macosx_10_13_x86_64.whl

四、验证

1、首先切换工作目录到tensorflow目录外,切记!

➜ tensorflow git:(master) cd ~

2、调用 Python2:

$ python2

3、验证

在 Python 交互式 shell 中输入以下几行简短的程序代码:

# Python

importtensorflow astf

hello =tf.constant('Hello, TensorFlow!')

sess =tf.Session()

print(sess.run(hello))

如果系统输出以下内容,就说明您可以开始编写 TensorFlow 程序了:

Hello, TensorFlow!

从二进制安装方式到源码编译方式前后折腾了两周,终于看到结果啦!