1.安裝必要的軟件
1.1.安裝JDK 8
(1)JDK 8 can be downloaded from
Oracle's JDK Page:
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
(2)Look for "Mac OS X" under "Java SE Development Kit". This will download a DMG image with an install wizard.
1.2. 安裝Homebrew
Install Homebrew on macOS (OS X),Installing Homebrew is a one-time setup:
1 $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
1.3.安裝bazel
安裝Google自家的編譯工具bazel,mac上最好的辦法就是直接使用brew安裝了( Install Bazel Homebrew Package)
1 $ brew install bazel
You are all set. You can confirm Bazel is installed successfully by running bazel version.
You can later upgrade to newer version of Bazel with brew upgrade bazel.
2.安裝TensorFlow
2.1.下載官方的TensorFlow源碼
1 $ git clone https://github.com/tensorflow/tensorflow
2.2.配置
下載完成后會在你的用戶名的目錄下生成一個tensorflow的文件夾,使用
1 $ cd tensorflow
進入這個文件夾,然后輸入:
1 $ ./configure
進行配置,在配置過程中會出現一系列的問題,通常情況下沒有特殊的要求,一般全部都選擇n:


2.3.編譯
配置完成后就是編譯過程了,輸入下面命令(輸入此命令后你會發現你的電腦呼呼作響,並且會花較長時間來編譯):
1 $ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
2.4.生成pip安裝包
經過漫長的編譯后,任務就基本上完成了,接下來就是輸入下面的命令來生成pip安裝包了:
1 $ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
2.5.安裝TensorFlow
最后就是用pip命令來安裝TensorFlow了:
1 $ sudo pip install /tmp/tensorflow_pkg/tensorflow-1.3.0-cp27-cp27m-macosx_10_7_x86_64.whl
至此就完成了。
3.常見問題
3.1 常見錯誤
出現下面的錯誤:
>>> import tensorflow as tf Traceback (most recent call last): File "<stdin>", line 1, in <module> File "tensorflow/__init__.py", line 24, in <module> from tensorflow.python import * File "tensorflow/python/__init__.py", line 49, in <module> from tensorflow.python import pywrap_tensorflow File "tensorflow/python/pywrap_tensorflow.py", line 52, in <module> raise ImportError(msg) ImportError: Traceback (most recent call last): File "tensorflow/python/pywrap_tensorflow.py", line 41, in <module> from tensorflow.python.pywrap_tensorflow_internal import * ImportError: No module named pywrap_tensorflow_internal Failed to load the native TensorFlow runtime. See https://www.tensorflow.org/install/install_sources#common_installation_problems for some common reasons and solutions. Include the entire stack trace above this error message when asking for help.
問題原因:python誤以為tensorflow目錄中的tensorflow就是要導入的模塊。
解決辦法:不要在tensorflow中運行python或者ipython,到上一級目錄或切換到其他目錄運行即可。
3.2其他問題
暫略