Mac: Hello TensorFlow

TensorFlow可在如下三种环境下运行:

1. Python虚拟环境

1.1. 相关命令

$ python –version #查看python版本

$ pip –version #查看pip版本

$ virtualenv –version #查看virtualenv版本

$ pip list installed #查看已安装的包

$ pip install tensorflow==1.12 jupyter matplotlib pandas seaborn numpy #安装需要的包

1.2. 配置python2.7-tensorflow-venv虚拟环境

$ sudo pip install virtualenv #安装虚拟环境

$ virtualenv –system-site-packages -p python2.7 ./venv  #在当前目录创建venv虚拟环境

$ source ./venv/bin/activate #激活虚拟环境

$ pip install –upgrade tensorflow #安装tensorflow包

$ deactivate #退出虚拟环境

1.3. python输出hello tensorflow

编辑tf-test.py文件,内容为:

import tensorflow as tf

hello = tf.constant(“Hello TensorFlow”)

sess = tf.Session()

print(sess.run(hello))

在已激活的虚拟环境下运行python tf-test.py

2. Jupyter交互环境

(venv) $ pip install jupyter

(venv) $ python -m ipykernel install –user –name=venv

(venv) $ jupyter kernelspec list #查看kernel情况

(venv) $ jupyter notebook #打开notebook

演示案例参考:https://github.com/geektime-geekbang/tensorflow-101/

3. Docker容器

下载安装docker:https://hub.docker.com/editions/community/docker-ce-desktop-mac

$ docker pull tensorflow/tensorflow:nightly-jupyter

$ docker run -it -p 8888:8888 -v $PWD:/tf/notebooks tensorflow/tensorflow:nightly-jupyter

打开文件: http://127.0.0.1:8888/?token=fladskjflasdkjfaslfij

2019/1/11, Fri

Leave a Reply

Your email address will not be published. Required fields are marked *