安裝系統: Ubuntu 17.04
按照官網 教程,有4種方法:
- virtualenv
- "native" pip
- Docker
- Anaconda
這里選擇virtunalenv,使用這種方法的好處就是在一個隔離的環境中安裝,不影響系統已有的python環境。
所以安裝會放在一個新創建的目錄中,刪除該目錄就相當于卸載tensorflow了。
命令如下:
sudo apt-get install python-pip python-dev python-virtualenv
virtualenv --system-site-packages tensorflow
cd ~/tensorflow
source ~/tensorflow/bin/activate
pip install --upgrade tensorflow # for Python 2.7 just CPU
到這一步會發現下載超慢,因為某些不可抗力的原因。解決方法是pip源使用國內鏡像,如:
清華:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中國科技大學 https://pypi.mirrors.ustc.edu.cn/simple/
華中理工大學:http://pypi.hustunique.com/
山東理工大學:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/
臨時使用可以在pip命令添加參數 -i https://pypi.tuna.tsinghua.edu.cn/simple
永久使用的話創建文件 ~/.pip/pip.conf,配置如下:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
這之后下載就飛快了。
安裝好之后,每次使用virtualenv環境都需要激活, 輸入命令:
source ~/tensorflow/bin/activate
退出則輸入:
deactivate
驗證是否安裝成功,試著輸出: hello world
Screenshot from 2017-05-12 22-01-37.png
測試MNIST數據
- 從Yann LeCun's website 上下載4個gz格式文件,放在新創建的目錄MNIST_DATA下
- 下載運行腳本 mnist_softmax.py
然后運行:
python mnist_softmax.py --data_dir ./MNIST_data/
輸出如下:
Extracting ./MNIST_data/train-images-idx3-ubyte.gz
Extracting ./MNIST_data/train-labels-idx1-ubyte.gz
Extracting ./MNIST_data/t10k-images-idx3-ubyte.gz
Extracting ./MNIST_data/t10k-labels-idx1-ubyte.gz
0.9194