tensorflow 源码安装(for MacOS)

install tensorflow from sources for MacOS

运行tensorflow时经常遇到如下问题:

The TensorFlow library wasn’t compiled to use SSE4.1/SSE4.2 /AVX/AVX2/FMA instructions, but these are available on your machine and could speed up CPU computations

于是好奇就想尝试通过tensorflow源码安装解决这个问题

先说一下我的电脑系统版本:macOS sierra version 10.12.6  不带独显所以我装的是cpu版本的tensorflow

下面就是叙述一下安装步骤和我在安装过程中遇到的一些问题:

1. 先把tensorflow源码下载(clone the tensorflow repository)

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

$ cd tensorflow

$ git checkout Branch # where Branch is the desired branch

2. 配置环境(prepare environment for MacOS)

在安装tensorflow之前,必须先安装bazel和python依赖包(six numpy wheel)

利用homebrew安装bazel,先安装JDK 8和homebrew, 这两个我很早就安装了就不叙述了

利用brew安装bazel如下:

brew install bazel 

bazel version

brew upgrade bazel

install python dependencies

pip install six numpy wheel


3. 配置安装tensorflow

前面下载的安装源里有一个bash 脚本文件:congigure

这个文件可以帮助你识别安装tensorflow所需包的路径等问题运行如下:在tensorflow路径,

tensorflow hfes$ ./configure


here is an example excution of the configure script:


You have bazel 0.10.0-homebrew installed.

Please specify the location of python. [Default is /Users/bingxueguo/anaconda/bin/python]: /Users/bingxueguo/anaconda/bin/python



Found possible Python library paths:

  /Users/bingxueguo/anaconda/lib/python3.5/site-packages

Please input the desired Python library path to use.  Default is [/Users/bingxueguo/anaconda/lib/python3.5/site-packages]

/Users/bingxueguo/anaconda/lib/python3.5/site-packages

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 Apache Kafka Platform support? [y/N]: Y

Apache Kafka Platform 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]: 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]: N

No CUDA support will be enabled for TensorFlow.


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]: -march=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.

--config=tensorrt    # Build with TensorRT support.

Configuration finished


前面的选择Y/N会影响到之后的安装是否出现问题,

一开始我在这里选择了一个y

Do you wish to build TensorFlow with VERBS support? [y/N]: n

No VERBS support will be enabled for TensorFlow.

后面出现了如下问题:

./tensorflow/contrib/verbs/rdma.h:21:10: fatal error: 'infiniband/verbs.h' file not found

#include      ^~~~~~~~~~~~~~~~~~~~

1 error generated.

也是搜索查询了好久很多给的是linux的解决办法,于是我又重新运行./configure选择了n才解决。


4.编译目标程序(这个只要前面配置没什么问题基本到这一步不会出什么问题,只是时间得一二十分钟)

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

运行这一步后也会产生一个tensorflow的安装包(.whl)在文件夹

/tmp/tensorflow_pkg/下面


5. 安装pip包

官网上给的是

sudo pip install /tmp/tensorflow_pkg/tensorflow-1.5.0-py2-none-any.whl

当然这个包的名称还是要根据自己前面运行生成的,由于我也是初接触这些,也是琢磨了一下,发现直接在网页里输入

///tmp/tensorflow_pkg/ 可以找到前面生成的安装包,下载下来放在对应的文件夹然后用pip 安装即可

pip install tensorflow-1.5.0-cp35-cp35m-macosx_10_6_x86_64.whl


现在运行这个就不会出现一开始提到的问题了:

>>> import tensorflow as tf

>>> hello = tf.constant("Hello,Tensorflow")

>>> sess = tf.Session()

>>> print(sess.run(hello))

b'Hello,Tensorflow'

运行速度也会加快










你可能感兴趣的:(个人,tensorflow,source,code)