作为一只小白,在安装tensorflow的时候出现了各种问题……希望有一些和我出现同样问题的人可以少走一点弯路……鉴于我是用pip安装(即python2.7,如果是更高版本用pip3),在这里只提供pip在OS X上安装的方法。
系统:OS X 10.13,安装的python2.7,tensorflow1.3.0,bazel0.7.0
安装tensorflow的步骤可以参考中文社区,当然如果能翻墙去官网那是最好的,里面的步骤真的很简单……
官网:https://www.tensorflow.org/install/install_mac
1、安装python
由于tensorflow需要python环境,所以第一步是安装python,你可以选择安装2.7或者3.3+的版本
官网连接pip中的安装:
Most distributions of Python come with pip preinstalled. If pip is missing, it can be installed through the system package manager or by invoking cURL, a client-side data transfer tool:
curl https://bootstrap.pypa.io/get-pip.py | python
One major advantage of pip is the ease of its command-line interface, which makes installing Python software packages as easy as issuing one command:
pip install some-package-name
Users can also easily remove the package:
pip uninstall some-package-name
Most importantly pip has a feature to manage full lists of packages and corresponding version numbers, possible through a "requirements" file.[4] This permits the efficient re-creation of an entire group of packages in a separate environment (e.g. another computer) or virtual environment. This can be achieved with a properly formatted requirements.txt
file and the following command:
pip install -r requirements.txt
Install some package for a specific version python, where ${version}
is replaced for 2, 3, 3.4, etc.:
pip${version} install some-package-name
检测你是否安装成功可以在终端输入:
$ pip -V # for Python 2.7 $ pip3 -V # for Python 3.n如果正常则表示安装成功。
如果你想升级,(tensorflow比较推荐8.1以上的版本)
$ sudo easy_install --upgrade pip $ sudo easy_install --upgrade six
2、tensorflow
关于安装tensorflow的版本,最好是最新的,否则你在github上很多project可能对于版本有要求,如果安装比较低运行时会出现问题。现在最新的1.3.0,只要使用官网的安装流程,就一定会给你安装最新版本
$ pip install tensorflow # Python 2.7; CPU support $ pip3 install tensorflow # Python 3.n; CPU support如果上面这个版本失败了,可以使用下面的方法:
$ sudo pip install --upgrade tfBinaryURL # Python 2.7 $ sudo pip3 install --upgrade tfBinaryURL # Python 3.n其中tfBinaryURL取决于你的安装版本:
Python 2.7
https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.3.0-py2-none-any.whl
Python 3.4, 3.5, or 3.6
e.g.(pyhton 2.7) https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.3.0-py3-none-any.whl
$ sudo pip3 install --upgrade \
https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.3.0-py2-none-any.whl
安装完以后进行试验看是否安装成功:
# Python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
如果出现:
Hello, TensorFlow!则表示安装成功。
关于protobuf pip package:这里会影响运行速度,最好升到最高版(现在最高版为3.1)
-
or Python 2.7:
pip install --upgrade \ https://storage.googleapis.com/tensorflow/mac/cpu/protobuf-3.1.0-cp27-none-macosx_10_11_x86_64.whl
$ -
for Python 3.n:
pip3 install --upgrade \ https://storage.googleapis.com/tensorflow/mac/cpu/protobuf-3.1.0-cp35-none-macosx_10_11_x86
$
3、bazel
应该说安装最头疼的就是bezel……大家可以直接参考官网(https://docs.bazel.build/versions/master/install.html)的安装方法,其中需要注意几点,一个是jdk不能选择9,因为9里面的封装方式发生改变,bazel是不支持的。如果是使用homwbrew方法安装的bazel,卸载方法为brew uninstall bazel,即可。
检测是否安装好输入bazel version看是否成功即可。
我在安装过程中,当时折磨我很久的一个问题是说找不到rt.jar文件……安装过程明明是直接从官网下载的……应该说在9一下的版本,以8为例子,rt.jar是存在的,但是在另一个文件夹里,一般在资源库/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib里(记不太清了,肯定在home的几个文件夹里),找到以后拷贝到home文件夹里就好了。
还有一点,使用过程中需要within workspace,相当于在bazel运行前需要:cd /tmp/magenta(e.g.)里面需要有WORKSPACE文件,当然大佬们要自己写的话要先写一个BUILD文件,具体细节其他地方有写,我就不赘述了。