Mac OS 使用virtualenv部署python tensorflow


Tensorflow部署

Tensorflow这么强大,直到今天我才开始尝试使用,记录在mac上部署Tensorflow。 Mac平台:Mac os 10.12.6 Python版本: Python 3.6.3 Python虚拟环境: virtualenv

安装virtualenv http://www.alpface.com/article/2018/1/23/14.html 安装pip: sudo easy_install pip

创建基于python3的虚拟环境,

mkvirtualenv -p /usr/local/bin/python3 tensorflow

如果提示:The path /usr/bin/python3 (from --python=/usr/bin/python3) does not exist 请在终端使用which查找python3的路径:which python3

虚拟环境创建完成后,会显示:

swaedeMBP:~ swae$ mkvirtualenv -p /usr/local/bin/python3 tensorflow
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/swae/Workspaces/tensorflow/bin/python3.6
Also creating executable in /Users/swae/Workspaces/tensorflow/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /Users/swae/workspaces/tensorflow/bin/predeactivate
virtualenvwrapper.user_scripts creating /Users/swae/workspaces/tensorflow/bin/postdeactivate
virtualenvwrapper.user_scripts creating /Users/swae/workspaces/tensorflow/bin/preactivate
virtualenvwrapper.user_scripts creating /Users/swae/workspaces/tensorflow/bin/postactivate
virtualenvwrapper.user_scripts creating /Users/swae/workspaces/tensorflow/bin/get_env_details

创建完成后会自动进入新建的虚拟环境: 比如我的终端每行前面显示(tensorflow)

(tensorflow) swaedeMBP:~ swae$

安装tensorflow,在虚拟环境下执行以下,注意不要带sudo:

pip install tensorflow

mark:安装需要时间,请耐心等待

Tensorflow例子:

本实例摘自:http://www.tensorfly.cn/tfdoc/get_started/introduction.html, 这段很短的 Python 程序生成了一些三维数据, 然后用一个平面拟合它.

>>> import tensorflow as tf
>>> import numpy as np
# 使用 NumPy 生成假数据(phony data), 总共 100 个点.
>>> x_data = np.random.rand(100).astype(np.float32)
>>> y_data = x_data*0.1 + 0.3

# create tensorflow structure begin #
# 构造一个线性模型
>>> Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
>>> biases = tf.Variable(tf.zeros([1]))
>>> y = Weights*x_data + biases
# 最小化方差
>>> loss = tf.reduce_mean(tf.square(y-y_data))
>>> optimizer = tf.train.GradientDescentOptimizer(0.5)
>>> train = optimizer.minimize(loss)
# 初始化变量
>>> init = tf.initialize_all_variables()
# create tensorflow structure end #
# 启动图 (graph)
>>> sess = tf.Session()
>>> sess.run(init)
# 拟合平面
>>> for step in range(201):
...     sess.run(train)
...     if step % 20 == 0:
...             print(step, sess.run(Weights), sess.run(biases))
...

运行后,得到最佳拟合结果:

0 [0.02237] [0.45752484]
20 [0.06749882] [0.3168238]
40 [0.09158534] [0.30435574]
60 [0.09782141] [0.30112773]
80 [0.09943596] [0.300292]
100 [0.09985398] [0.3000756]
120 [0.09996219] [0.3000196]
140 [0.09999021] [0.30000508]
160 [0.09999746] [0.30000132]
180 [0.09999935] [0.30000034]
200 [0.09999984] [0.3000001]

最终训练结果:Weights无限接近0.1 ,biases 无限接近0.3

执行过程中可能会遇到下面问题,可以忽略:

  1. 当执行到 init = tf.initialize_all_variables()时
    WARNING:tensorflow:From /Users/swae/Workspaces/tensorflow/lib/python3.6/site-packages/tensorflow/python/util/tf_should_use.py:118: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
  2. 当执行到sess = tf.Session()
    2018-02-26 00:07:35.147961: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA

附件: