Mac下使用源码编译安装TensorFlow CPU版本

1.安装必要的软件

1.1.安装JDK 8

(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其他问题

暂略
 

4.参考链接

 
posted @ 2017-09-27 11:20  gatherstars  阅读(3821)  评论(0编辑  收藏  举报