Tensorflow:Mac下的环境安装和helloworld

Tensorflow安装

(主要参考官网和他人blog)
官网上建议virtualenv安装
我们建议使用 virtualenv 安装。virtualenv 是一个和其它 Python 项目开发隔离的虚拟 Python 环境,在同一台机器上不会干扰也不会被其它程序影响。virtualenv 安装过程中,你不仅仅安装了 TensorFlow 还有它的所有依赖包。(事实上这很简单)要开始使用 TensorFlow,你需要“启动” virtualenv 环境。
安装步骤

通过以下命令安装 pip 和 virtualenv:
sudo easy_install pip
$ sudo pip install --upgrade virtualenv

执行以下任一命令创建虚拟环境:
$ virtualenv --system-site-packages targetDirectory # for Python 2.7
$ virtualenv --system-site-packages -p python3 targetDirectory # for Python 3.n

执行任一命令**虚拟环境:
$ source ~/tensorflow/bin/activate      # If using bash, sh, ksh, or zsh
$ source ~/tensorflow/bin/activate.csh  # If using csh or tcsh 

当虚拟环境**后,你可以在这个 shell 中运行 TensorFlow 程序。如果你不再使用 TensorFlow,可以通过下面命令退出环境:
(tensorflow)$ deactivate 

上面的 source 命令应该将提示符改成了下面这样:
(tensorflow)$ 
如果已经安装了 pip 8.1 或者更新的版本,执行以下任一命令在**的虚拟环境中安装 TensorFlow 及其所有依赖:
 $ pip install --upgrade tensorflow      # for Python 2.7
 $ pip3 install --upgrade tensorflow     # for Python 3.n

Hello world

import tensorflow as tf

hello = tf.constant('Hello,world!')

sess = tf.Session()

result = sess.run(hello)

sess.close()

print(result)

我使用的PyCharm,需要配置python路径为安装tensorflow下的python路径,这样才能引用到tensorflow的包,如下图所示。

Tensorflow:Mac下的环境安装和helloworld
运行代码的时候出现错误如下,网上查找了一下是因为protobuf版本不一致导致序列化的时候出错。

import tensorflow_hub错误: __new__() got an unexpected keyword argument 'serialized_options'

因此需要更新protobuf版本,运行命令如下:

pip install -U protobuf

这样helloworld demo就可以运行啦!