- 手順 https://www.tensorflow.org/install/install_sources#prepare_environment_for_macos
- Clone the TensorFlow? repository
[~]$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow
[~]$ cd tensorflow
[~]$ git checkout -b v1.8.0
Switched to a new branch 'v1.8.0'
- Prepare environment for macOS
[~]$ brew install bazel coreutils
[~]$ conda create -n jupyter-env python=3.6
[~]$ conda activate jupyter-env
(jupyter-env) [~]$ conda install six numpy wheel mock mkl cython jupyter
- 本家のインストールガイドでインストールが推奨されている enum34 パッケージは python2 系で 3系の enum を使うため。3系の場合は不要
- mkl は、Intel Math Kernel Library。numpy が mkl があったらもっと速く計算できると警告を出すので導入。python から mkl を使うのに cython が必要
- ただし mkl は tensorflow では使わない。 tensorflow は、mkl を OpenMP から使っているんだけど、macOS の clang は OpenMP に対応していないため
- こいつらは jupyter-env 環境向けにインストールされたもので OS は汚さない viva anaconda
- Build the pip package (./configure は 全部デフォルト設定)
(jupyter-env) [~]$ cd Downloads/tensorflow/
(jupyter-env) [~/Downloads/tensorflow]$ bazel clean
Starting local Bazel server and connecting to it...
............
INFO: Starting clean.
(jupyter-env) [~/Downloads/tensorflow]$ ./configure
You have bazel 0.13.0-homebrew installed.
Please specify the location of python. [Default is /Users/atsushi/.pyenv/versions/anaconda3-5.1.0/envs/jupyter-env/bin/python]:
Please input the desired Python library path to use. Default is [/Users/atsushi/.pyenv/versions/anaconda3-5.1.0/envs/jupyter-env/lib/python3.6/site-packages]
Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]:
Google Cloud Platform support will be enabled for TensorFlow.
Do you wish to build TensorFlow with Hadoop File System support? [Y/n]:
Hadoop File System support will be enabled for TensorFlow.
Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]:
Amazon S3 File System support will be enabled for TensorFlow.
Do you wish to build TensorFlow with Apache Kafka Platform support? [Y/n]:
Apache Kafka Platform support will be enabled for TensorFlow.
Do you wish to build TensorFlow with XLA JIT support? [y/N]:
No XLA JIT support will be enabled for TensorFlow.
Do you wish to build TensorFlow with GDR support? [y/N]:
No GDR support will be enabled for TensorFlow.
Do you wish to build TensorFlow with VERBS support? [y/N]:
No VERBS support will be enabled for TensorFlow.
Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]:
No OpenCL SYCL 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.
Do you wish to download a fresh release of clang? (Experimental) [y/N]:
Clang will not be downloaded.
Do you wish to build TensorFlow with MPI support? [y/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]:
Would you like to interactively configure ./WORKSPACE for Android builds? [y/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
(jupyter-env) [~/Downloads/tensorflow]$ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
...
INFO: Elapsed time: 3206.736s, Critical Path: 139.14s
INFO: 8015 processes, local.
INFO: Build completed successfully, 8421 total actions
(jupyter-env) [~/Downloads/tensorflow]$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
- --config=mkl は使わない omp.h がないとエラーが出てビルドが止まる。無理矢理 gcc を使ってビルドしても良いんだけど... そこ頑張るとこじゃないんで...
- Install the pip package
(jupyter-env) [~/Downloads/tensorflow]$ pip install /tmp/tensorflow_pkg/tensorflow-1.8.0-cp36-cp36m-macosx_10_7_x86_64.whl
- Validate your installation
(jupyter-env) [~/Downloads/tensorflow]$ cd ~
(jupyter-env) [~]$ python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello Tensorflow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
b'Hello Tensorflow!'
>>>
(jupyter-env) [~]$
/Downloads/tensorflow 下で実行すると
ImportError: Could not import tensorflow. Do not import tensorflow from its source directory;
change directory to outside the TensorFlow source tree, and relaunch your Python interpreter from there.
と言うエラーがでる
- 実行時にソースコードが必要というわけではなくて、カレントディレクトリ配下の python コードを見に行っちゃうから。
(jupyter-env) [~]$ rm -rf ~/Downloads/tensorflow/
しても、ちゃんと import tensorflow as tf は動く