test tensorflow on Mac OS X

1 install xcode from App Store

2 install pyenv
$sudo xcodebuild -license accept
$brew install pyenv
$ pyenv versions
* system (set by /Users/xxxxxx/.pyenv/version)
$ pyenv install -l
latest version are 3.6.4 and 2.7.14

$ pyenv install 2.7.14
$ pyenv install 3.6.4

3 install pyenv-virtualenv
$ brew install pyenv-virtualenv

append

export PATH="$HOME/.pyenv/shims:$PATH"
eval "$(pyenv init -)"

to~/.bash_profile
$ source ~/.bash_profile

$ pyenv virtualenv 3.6.4 tensorflow
$ pyenv shell tensorflow
$ pyenv versions
system
2.7.14
3.6.4
3.6.4/envs/tensorflow
* tensorflow (set by PYENV_VERSION environment variable)



支 持 本 站: 捐赠服务器等运维费用,感谢您的支持!

4 install tensorflow
$ pip install tensorflow
Successfully installed absl-py-0.1.10 bleach-1.5.0 futures-3.1.1 html5lib-0.9999999 markdown-2.6.11 numpy-1.14.0 protobuf-3.5.1 six-1.11.0 tensorflow-1.5.0 tensorflow-tensorboard-1.5.0 werkzeug-0.14.1 wheel-0.30.0

$ python

Python 3.6.4 (default, Feb 5 2018, 21:11:04)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> a = tf.constant(1.2)
>>> b = tf.constant(3.4)
>>> add_node = tf.add(a, b)
>>> sess = tf.Session()
>>> print(sess.run(add_node))
4.6000004
>>> a = tf.constant([1.0,2.0],name="a")
>>> b = tf.constant([2.0,3.0],name="b")
>>> result = a + b
>>> sess = tf.Session()
>>> sess.run(result)
array([3., 5.], dtype=float32)
>>> quit()

5 test
https://www.tensorflow.org/tutorials/image_retraining


$ curl -O http://download.tensorflow.org/example_images/flower_photos.tgz
$ tar xzf flower_photos.tgz
$ git clone https://github.com/tensorflow/tensorflow
$ cd tensorflow/
$ python tensorflow/examples/image_retraining/retrain.py --image_dir ../flower_photos
$ tensorboard --logdir /tmp/retrain_logs

TensorBoard 1.5.0 at http://localhost:6006 (Press CTRL+C to quit)


$ python tensorflow/examples/label_image/label_image.py --graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt --input_layer=Mul --output_layer=final_result --input_mean=128 --input_std=128 --image=../flower_photos/daisy/21652746_cc379e0eea_m.jpg

Use tf.nn.batch_normalization().
daisy 0.997474
sunflowers 0.0018685834
dandelion 0.0005092218
tulips 0.00012285836
roses 2.5346002e-05

6 some samples
6.1
$ cat test.py

import tensorflow as tf

g1 = tf.Graph()
with g1.as_default():
v = tf.get_variable("v", shape=[1], initializer=tf.zeros_initializer)

g2 = tf.Graph()
with g2.as_default():
v = tf.get_variable("v", shape=[1], initializer=tf.ones_initializer)

with tf.Session(graph=g1) as sess:
tf.global_variables_initializer().run()
with tf.variable_scope("",reuse=True):
print(sess.run(tf.get_variable("v")))

with tf.Session(graph=g2) as sess:
tf.global_variables_initializer().run()
with tf.variable_scope("",reuse=True):
print(sess.run(tf.get_variable("v")))


$ python test.py
[0.]
[1.]

支 持 本 站: 捐赠服务器等运维费用,感谢您的支持!

发布时间: