2017/6/23

安裝TensorFlow

主要有以下幾種安裝方法:
  • virtualenv 
  • "native" pip 
  • Docker 
  • installing from sources, which is for experts and is documented in a separate guide.
官方推薦用第一種,那時第一次裝沒看仔細竟然第一到第三種方式都用了...
這裡先用地一種方法,先下以下這行指令安裝virtualenv:

  ~ sudo pip3 install --upgrade virtualenv

再下這一行:
  ~ virtualenv --system-site-packages -p python3 ~/tensorflow
這樣會在家目錄下創造一個tensorflow的路徑

啟動virtualenv環境
  ~ source ~/tensorflow/bin/activate
前面的部分會變成:
(tensorflow)   ~

結束tensorflow模式,輸入deactivate
(tensorflow)   ~ deactivate

升級tensorflow版本:
(tensorflow)   ~ pip3 install --upgrade tensorflow
以下是升級過程的訊息:
Collecting tensorflow
  Downloading tensorflow-1.2.0-cp36-cp36m-macosx_10_11_x86_64.whl (34.1MB)
    100% |████████████████████████████████| 34.1MB 44kB/s
Collecting backports.weakref==1.0rc1 (from tensorflow)
  Downloading backports.weakref-1.0rc1-py3-none-any.whl
Requirement already up-to-date: numpy>=1.11.0 in /usr/local/lib/python3.6/site-packages (from tensorflow)
Collecting protobuf>=3.2.0 (from tensorflow)
  Downloading protobuf-3.3.0.tar.gz (271kB)
    100% |████████████████████████████████| 276kB 4.5MB/s
Collecting werkzeug>=0.11.10 (from tensorflow)
  Downloading Werkzeug-0.12.2-py2.py3-none-any.whl (312kB)
    100% |████████████████████████████████| 317kB 4.0MB/s
Requirement already up-to-date: wheel>=0.26 in ./tensorflow/lib/python3.6/site-packages (from tensorflow)
Collecting six>=1.10.0 (from tensorflow)
  Using cached six-1.10.0-py2.py3-none-any.whl
Collecting bleach==1.5.0 (from tensorflow)
  Downloading bleach-1.5.0-py2.py3-none-any.whl
Collecting markdown==2.2.0 (from tensorflow)
  Downloading Markdown-2.2.0.tar.gz (236kB)
    100% |████████████████████████████████| 245kB 5.0MB/s
Collecting html5lib==0.9999999 (from tensorflow)
  Downloading html5lib-0.9999999.tar.gz (889kB)
    100% |████████████████████████████████| 890kB 1.5MB/s
Requirement already up-to-date: setuptools in ./tensorflow/lib/python3.6/site-packages (from protobuf>=3.2.0->tensorflow)
Building wheels for collected packages: protobuf, markdown, html5lib
  Running setup.py bdist_wheel for protobuf ... done
  Stored in directory: /Users/xanxus/Library/Caches/pip/wheels/1b/42/a0/4c7343df5b629ec9c75655468dce7652b28026896b0209ba55
  Running setup.py bdist_wheel for markdown ... done
  Stored in directory: /Users/xanxus/Library/Caches/pip/wheels/b9/4f/6c/f4c1c5207c1d0eeaaf7005f7f736620c6ded6617c9d9b94096
  Running setup.py bdist_wheel for html5lib ... done
  Stored in directory: /Users/xanxus/Library/Caches/pip/wheels/6f/85/6c/56b8e1292c6214c4eb73b9dda50f53e8e977bf65989373c962
Successfully built protobuf markdown html5lib
Installing collected packages: backports.weakref, six, protobuf, werkzeug, html5lib, bleach, markdown, tensorflow
Successfully installed backports.weakref-1.0rc1 bleach-1.5.0 html5lib-0.9999999 markdown-2.2.0 protobuf-3.3.0 six-1.10.0 tensorflow-1.2.0 werkzeug-0.12.2
其實到這裡就已經安裝好了,可以run了

直接來試試看,打開終端機,輸入python3:
Python 3.6.1 (default, Apr  4 2017, 09:40:21)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
再依序輸入以下程式碼:
# Python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
輸入到第4行他會回一串訊息,最後結果如果是Hello, TensorFlow!,表示成功,可以開始玩TensorFlow了。

其他安裝的部分我就直接貼上來當參考就好
然後才要安裝TensorFlow
pip3 install tensorflow
這部分沒問題跑完$ pip3 list 檢查一下就好

接著要安裝docker
去官網下載Stable版的就好了
載下來是一個.dmg檔案,無困難

docker啟動tensorflow
$ docker run -it gcr.io/tensorflow/tensorflow bash
  ~ docker run -it gcr.io/tensorflow/tensorflow bash
Unable to find image 'gcr.io/tensorflow/tensorflow:latest' locally
latest: Pulling from tensorflow/tensorflow
892cc5bfc051: Pull complete
f3eda43ea55a: Pull complete
646005d97ff4: Pull complete
44d770c1f7bd: Pull complete
1ce0c4bfe746: Pull complete
66d8ef4102f6: Pull complete
8c3ba45123da: Pull complete
7d2cd137e385: Pull complete
bf9cc7102450: Pull complete
b82f9e969c25: Pull complete
768a8cc85256: Pull complete
87dafd0cdfb4: Pull complete
Digest: sha256:2e8b4cd6b29e6ab704859f2131addc4a6f1230d6722750b4ca219f83e1f51217
Status: Downloaded newer image for gcr.io/tensorflow/tensorflow:latest
root@0d2ba1466cc9:/notebooks#
這樣容器就啟動了

中止容器:
+d

安裝Anaconda
他有分Graphical安裝與Command Line 安裝
我選後者
所以直接點Command Line INSTALLER載下來(380M
接下來他會跑一小陣子,耐心稍等

Welcome to Anaconda3 4.4.0 (by Continuum Analytics, Inc.)

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>> 
===================================
Anaconda End User License Agreement
===================================

Copyright 2017, Continuum Analytics, Inc.

All rights reserved under the 3-clause BSD License:

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Continuum Analytics, Inc. ("Continuum") nor the names
of its contributors may be used to endorse or promote products derived from

Do you approve the license terms? [yes|no]
>>> yes

Anaconda3 will now be installed into this location:
/Users/xanxus/anaconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/Users/xanxus/anaconda3] >>>
PREFIX=/Users/xanxus/anaconda3

installing: python-3.6.1-2 ...
installing: _license-1.1-py36_1 ...
installing: alabaster-0.7.10-py36_0 ...
installing: anaconda-client-1.6.3-py36_0 ...
installing: anaconda-navigator-1.6.2-py36_0 ...
installing: anaconda-project-0.6.0-py36_0 ...
installing: appnope-0.1.0-py36_0 ...
installing: appscript-1.0.1-py36_0 ...
installing: asn1crypto-0.22.0-py36_0 ...
installing: astroid-1.4.9-py36_0 ...
installing: astropy-1.3.2-np112py36_0 ...
installing: babel-2.4.0-py36_0 ...
installing: backports-1.0-py36_0 ...
installing: beautifulsoup4-4.6.0-py36_0 ...
installing: bitarray-0.8.1-py36_0 ...
installing: blaze-0.10.1-py36_0 ...
installing: bleach-1.5.0-py36_0 ...
installing: bokeh-0.12.5-py36_1 ...
installing: boto-2.46.1-py36_0 ...
installing: bottleneck-1.2.1-np112py36_0 ...
installing: cffi-1.10.0-py36_0 ...
installing: chardet-3.0.3-py36_0 ...
installing: click-6.7-py36_0 ...
installing: cloudpickle-0.2.2-py36_0 ...
installing: clyent-1.2.2-py36_0 ...
installing: colorama-0.3.9-py36_0 ...
installing: contextlib2-0.5.5-py36_0 ...
installing: cryptography-1.8.1-py36_0 ...
installing: curl-7.52.1-0 ...
installing: cycler-0.10.0-py36_0 ...
installing: cython-0.25.2-py36_0 ...
installing: cytoolz-0.8.2-py36_0 ...
installing: dask-0.14.3-py36_1 ...
installing: datashape-0.5.4-py36_0 ...
installing: decorator-4.0.11-py36_0 ...
installing: distributed-1.16.3-py36_0 ...
installing: docutils-0.13.1-py36_0 ...
installing: entrypoints-0.2.2-py36_1 ...
installing: et_xmlfile-1.0.1-py36_0 ...
installing: fastcache-1.0.2-py36_1 ...
installing: flask-0.12.2-py36_0 ...
installing: flask-cors-3.0.2-py36_0 ...
installing: freetype-2.5.5-2 ...
installing: get_terminal_size-1.0.0-py36_0 ...
installing: gevent-1.2.1-py36_0 ...
installing: greenlet-0.4.12-py36_0 ...
installing: h5py-2.7.0-np112py36_0 ...
installing: hdf5-1.8.17-1 ...
installing: heapdict-1.0.0-py36_1 ...
installing: html5lib-0.999-py36_0 ...
installing: icu-54.1-0 ...
installing: idna-2.5-py36_0 ...
installing: imagesize-0.7.1-py36_0 ...
installing: ipykernel-4.6.1-py36_0 ...
installing: ipython-5.3.0-py36_0 ...
installing: ipython_genutils-0.2.0-py36_0 ...
installing: ipywidgets-6.0.0-py36_0 ...
installing: isort-4.2.5-py36_0 ...
installing: itsdangerous-0.24-py36_0 ...
installing: jbig-2.1-0 ...
installing: jdcal-1.3-py36_0 ...
installing: jedi-0.10.2-py36_2 ...
installing: jinja2-2.9.6-py36_0 ...
installing: jpeg-9b-0 ...
installing: jsonschema-2.6.0-py36_0 ...
installing: jupyter-1.0.0-py36_3 ...
installing: jupyter_client-5.0.1-py36_0 ...
installing: jupyter_console-5.1.0-py36_0 ...
installing: jupyter_core-4.3.0-py36_0 ...
installing: lazy-object-proxy-1.2.2-py36_0 ...
installing: libiconv-1.14-0 ...
installing: libpng-1.6.27-0 ...
installing: libtiff-4.0.6-3 ...
installing: libxml2-2.9.4-0 ...
installing: libxslt-1.1.29-0 ...
installing: llvmlite-0.18.0-py36_0 ...
installing: locket-0.2.0-py36_1 ...
installing: lxml-3.7.3-py36_0 ...
installing: markupsafe-0.23-py36_2 ...
installing: matplotlib-2.0.2-np112py36_0 ...
installing: mistune-0.7.4-py36_0 ...
installing: mkl-2017.0.1-0 ...
installing: mkl-service-1.1.2-py36_3 ...
installing: mpmath-0.19-py36_1 ...
installing: msgpack-python-0.4.8-py36_0 ...
installing: multipledispatch-0.4.9-py36_0 ...
installing: navigator-updater-0.1.0-py36_0 ...
installing: nbconvert-5.1.1-py36_0 ...
installing: nbformat-4.3.0-py36_0 ...
installing: networkx-1.11-py36_0 ...
installing: nltk-3.2.3-py36_0 ...
installing: nose-1.3.7-py36_1 ...
installing: notebook-5.0.0-py36_0 ...
installing: numba-0.33.0-np112py36_0 ...
installing: numexpr-2.6.2-np112py36_0 ...
installing: numpy-1.12.1-py36_0 ...
installing: numpydoc-0.6.0-py36_0 ...
installing: odo-0.5.0-py36_1 ...
installing: olefile-0.44-py36_0 ...
installing: openpyxl-2.4.7-py36_0 ...
installing: openssl-1.0.2l-0 ...
installing: packaging-16.8-py36_0 ...
installing: pandas-0.20.1-np112py36_0 ...
installing: pandocfilters-1.4.1-py36_0 ...
installing: partd-0.3.8-py36_0 ...
installing: path.py-10.3.1-py36_0 ...
installing: pathlib2-2.2.1-py36_0 ...
installing: patsy-0.4.1-py36_0 ...
installing: pep8-1.7.0-py36_0 ...
installing: pexpect-4.2.1-py36_0 ...
installing: pickleshare-0.7.4-py36_0 ...
installing: pillow-4.1.1-py36_0 ...
installing: pip-9.0.1-py36_1 ...
installing: ply-3.10-py36_0 ...
installing: prompt_toolkit-1.0.14-py36_0 ...
installing: psutil-5.2.2-py36_0 ...
installing: ptyprocess-0.5.1-py36_0 ...
installing: py-1.4.33-py36_0 ...
installing: pycosat-0.6.2-py36_0 ...
installing: pycparser-2.17-py36_0 ...
installing: pycrypto-2.6.1-py36_6 ...
installing: pycurl-7.43.0-py36_2 ...
installing: pyflakes-1.5.0-py36_0 ...
installing: pygments-2.2.0-py36_0 ...
installing: pylint-1.6.4-py36_1 ...
installing: pyodbc-4.0.16-py36_0 ...
installing: pyopenssl-17.0.0-py36_0 ...
installing: pyparsing-2.1.4-py36_0 ...
installing: pyqt-5.6.0-py36_1 ...
installing: pytables-3.3.0-np112py36_0 ...
installing: pytest-3.0.7-py36_0 ...
installing: python-dateutil-2.6.0-py36_0 ...
installing: python.app-1.2-py36_4 ...
installing: pytz-2017.2-py36_0 ...
installing: pywavelets-0.5.2-np112py36_0 ...
installing: pyyaml-3.12-py36_0 ...
installing: pyzmq-16.0.2-py36_0 ...
installing: qt-5.6.2-2 ...
installing: qtawesome-0.4.4-py36_0 ...
installing: qtconsole-4.3.0-py36_0 ...
installing: qtpy-1.2.1-py36_0 ...
installing: readline-6.2-2 ...
installing: requests-2.14.2-py36_0 ...
installing: rope-0.9.4-py36_1 ...
installing: ruamel_yaml-0.11.14-py36_1 ...
installing: scikit-image-0.13.0-np112py36_0 ...
installing: scikit-learn-0.18.1-np112py36_1 ...
installing: scipy-0.19.0-np112py36_0 ...
installing: seaborn-0.7.1-py36_0 ...
installing: setuptools-27.2.0-py36_0 ...
installing: simplegeneric-0.8.1-py36_1 ...
installing: singledispatch-3.4.0.3-py36_0 ...
installing: sip-4.18-py36_0 ...
installing: six-1.10.0-py36_0 ...
installing: snowballstemmer-1.2.1-py36_0 ...
installing: sortedcollections-0.5.3-py36_0 ...
installing: sortedcontainers-1.5.7-py36_0 ...
installing: sphinx-1.5.6-py36_0 ...
installing: spyder-3.1.4-py36_0 ...
installing: sqlalchemy-1.1.9-py36_0 ...
installing: sqlite-3.13.0-0 ...
installing: statsmodels-0.8.0-np112py36_0 ...
installing: sympy-1.0-py36_0 ...
installing: tblib-1.3.2-py36_0 ...
installing: terminado-0.6-py36_0 ...
installing: testpath-0.3-py36_0 ...
installing: tk-8.5.18-0 ...
installing: toolz-0.8.2-py36_0 ...
installing: tornado-4.5.1-py36_0 ...
installing: traitlets-4.3.2-py36_0 ...
installing: unicodecsv-0.14.1-py36_0 ...
installing: unixodbc-2.3.4-0 ...
installing: wcwidth-0.1.7-py36_0 ...
installing: werkzeug-0.12.2-py36_0 ...
installing: wheel-0.29.0-py36_0 ...
installing: widgetsnbextension-2.0.0-py36_0 ...
installing: wrapt-1.10.10-py36_0 ...
installing: xlrd-1.0.0-py36_0 ...
installing: xlsxwriter-0.9.6-py36_0 ...
installing: xlwings-0.10.4-py36_0 ...
installing: xlwt-1.2.0-py36_0 ...
installing: xz-5.2.2-1 ...
installing: yaml-0.1.6-0 ...
installing: zict-0.1.2-py36_0 ...
installing: zlib-1.2.8-3 ...
installing: anaconda-4.4.0-np112py36_0 ...
installing: conda-4.3.21-py36_0 ...
installing: conda-env-2.6.0-0 ...
Python 3.6.1 :: Continuum Analytics, Inc.
creating default environment...
installation finished.
Do you wish the installer to prepend the Anaconda3 install location
to PATH in your /Users/xanxus/.bash_profile ? [yes|no]
[yes] >>>
Prepending PATH=/Users/xanxus/anaconda3/bin to PATH in
newly created /Users/xanxus/.bash_profile

For this change to become active, you have to open a new terminal.

Thank you for installing Anaconda3!

Share your notebooks and packages on Anaconda Cloud!
Sign up for free:https://anaconda.org
到這裡表示安裝完了,它會幫你裝在家目錄底下也就是/xanxus/anaconda3/bin,並且應用程式清單會多一個Anaconda-Navigator。現在要把它加到.zshrc裡面,打開.zshrc
  ~ open ~/.zshrc
第二行改成這樣:
export PATH=$HOME/bin:/usr/local/bin:anaconda3/bin:$PATH
存檔

讓它生效L
  ~ source ~/.zshrc

輸入:
  ~ which anaconda
這樣就會有東西了,回傳:
anaconda3/bin/anaconda

驗證安裝是否成功,這是官方的驗證方法:
$ conda list
會回傳一堆套件名稱?

更新condacondaAnaconda的套件管理工具:
$ conda update conda

更新Anaconda
conda update anaconda

確認conda版本:
  ~ conda --version
conda 4.3.22

確認anaconda版本:
  ~ anaconda --version
anaconda Command line client (version 1.6.3)

conda創造一個tensorflow的環境(不是在docker容器啟動的狀態下執行)
  ~ conda create -n tensorflow
Fetching package metadata .........
Solving package specifications:
Package plan for installation in environment /Users/xanxus/anaconda3/envs/tensorflow:

Proceed ([y]/n)? y

#
# To activate this environment, use:
# > source activate tensorflow
#
# To deactivate an active environment, use:
# > source deactivate
#

把剛建的tensorflow環境執行起來:
  ~ source activate tensorflow
(tensorflow)   ~

用以下指令在conda環境裡安裝tensorflow
  ~ source activate tensorflow
(tensorflow)   ~ pip3 install --ignore-installed --upgrade \ https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.0-py3-none-any.whl
Collecting tensorflow==1.2.0 from https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.0-py3-none-any.whl
  Downloading https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.0-py3-none-any.whl (33.6MB)
    43% |█████████████▉                  | 14.5MB 101.0MB/s eta 0:00:0    43% |█████████████▉                  | 14.5MB 100.7MB/s eta 0:00:0    43% |█████████████▉                  | 14.5MB 101.2MB/s eta 0:00:0    43% |█████████████▉                  | 14.6MB 100.6MB/s eta 0:00:0    43% |█████████████▉                  | 14.6MB 100.9MB/s eta 0:00:0    43% |█████████████▉                  | 14.6MB 100.9MB/s eta 0:00:0    43% |█████████████▉                  | 14.6MB 101.0MB/s eta 0:00:0    43% |██████████████                  | 14.6MB 100.2MB/s eta 0:00:0    100% |████████████████████████████████| 33.6MB 45kB/s
Collecting html5lib==0.9999999 (from tensorflow==1.2.0)
Collecting werkzeug>=0.11.10 (from tensorflow==1.2.0)
  Using cached Werkzeug-0.12.2-py2.py3-none-any.whl
Collecting numpy>=1.11.0 (from tensorflow==1.2.0)
  Using cached numpy-1.13.0-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Collecting wheel>=0.26 (from tensorflow==1.2.0)
  Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
    100% |████████████████████████████████| 71kB 1.4MB/s
Collecting markdown==2.2.0 (from tensorflow==1.2.0)
Collecting protobuf>=3.2.0 (from tensorflow==1.2.0)
Collecting backports.weakref==1.0rc1 (from tensorflow==1.2.0)
  Using cached backports.weakref-1.0rc1-py3-none-any.whl
Collecting bleach==1.5.0 (from tensorflow==1.2.0)
  Using cached bleach-1.5.0-py2.py3-none-any.whl
Collecting six>=1.10.0 (from tensorflow==1.2.0)
  Using cached six-1.10.0-py2.py3-none-any.whl
Collecting setuptools (from protobuf>=3.2.0->tensorflow==1.2.0)
  Using cached setuptools-36.0.1-py2.py3-none-any.whl
Installing collected packages: six, html5lib, werkzeug, numpy, wheel, markdown, setuptools, protobuf, backports.weakref, bleach, tensorflow
Successfully installed backports.weakref-1.0rc1 bleach-1.5.0 html5lib-0.9999999 markdown-2.2.0 numpy-1.13.0 protobuf-3.3.0 setuptools-36.0.1 six-1.10.0 tensorflow-1.2.0 werkzeug-0.12.2 wheel-0.29.0

驗證安裝是否正確,分為:
1.確保你的環境已經準備好跑TensorFlow的程式
啟動tensorflow的容器?因為剛剛是第一次,所以才會下載一些東西
(tensorflow)   ~ docker run -it gcr.io/tensorflow/tensorflow bash
root@6dac05a479c0:/notebooks#


2.跑一個簡單的TensorFlow程式
接續上一步
直接在這裡打root@6dac05a479c0:/notebooks# python
依序輸入Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> # Python
...
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2017-06-22 14:31:09.949071: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-22 14:31:09.949196: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-22 14:31:09.949294: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-06-22 14:31:09.949351: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-22 14:31:09.949675: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
>>> print(sess.run(hello))
Hello, TensorFlow!
如果結果是Hello, TensorFlow!,表示環境已經準備好了,可以開始玩TensorFlow


參考資料:

沒有留言:

張貼留言