Source : 博客來
下午得空便在鄉下的 Win10 電腦上實際安裝 TensorFlow+Keras 框架, 以下是安裝紀錄 :
一. 安裝 TensorFlow (底層) :
TensorFlow 是 Google Brain 團隊所開發的低階深度學習框架, 可在各平台上進行高效能之矩陣運算, 原先僅使用於 Gmail 信件過濾, Google 語音辨識, Google 翻譯等 Google 自家產品上, 為了透過開源社群之力量使其成為市場標準, Google 於 2015 年 11 月將其原始碼開放. 參考 :
# https://www.tensorflow.org
在 Windows 上安裝 TensorFlow 書中建議先安裝 Anaconda, 但我一直覺得 Anaconda 太龐大, 所以我還是傾向用命令提示字元視窗進行原生安裝 (native installation). 注意, 根據上面官網說明, 執行 TensorFlow 需要 Python 64 位元版本, 參考 :
# https://www.tensorflow.org/install/install_windows
因此下載 Python 時不要按下載頁的那個大按鈕 (那是下載 32 位元版本), 而是要按 Python for "Windows" 超連結, 找尋 x86-64 executable installer 版下載安裝, 我下載的是目前的穩定版 v3.6.4 (python-3.6.4-amd64.exe) :
Python 64 安裝好後, 可參考下列文章安裝 Python 機器學習套件 :
# 在 Windows 中安裝 Python 機器學習套件
# Windows 安裝 Python 機器學習工具
包括下列 4 個指令 :
- pip3 install scipy (會同時安裝 Numpy)
- pip3 install matplotlib
- pip3 install pandas
- pip3 install scikit-learn
# 把「命令提示字元」視窗的輸出結果,直接複製到剪貼簿
D:\> pip3 install --upgrade tensorflow > tensorflow.txt
Collecting tensorflow
Downloading tensorflow-1.5.0-cp36-cp36m-win_amd64.whl (31.1MB)
Collecting absl-py>=0.1.6 (from tensorflow)
Downloading absl-py-0.1.10.tar.gz (79kB)
Collecting six>=1.10.0 (from tensorflow)
Downloading six-1.11.0-py2.py3-none-any.whl
Collecting wheel>=0.26 (from tensorflow)
Downloading wheel-0.30.0-py2.py3-none-any.whl (49kB)
Collecting protobuf>=3.4.0 (from tensorflow)
Downloading protobuf-3.5.1-py2.py3-none-any.whl (388kB)
Collecting numpy>=1.12.1 (from tensorflow)
Downloading numpy-1.14.1-cp36-none-win_amd64.whl (13.4MB)
Collecting tensorflow-tensorboard<1.6.0,>=1.5.0 (from tensorflow)
Downloading tensorflow_tensorboard-1.5.1-py3-none-any.whl (3.0MB)
Collecting setuptools (from protobuf>=3.4.0->tensorflow)
Downloading setuptools-38.5.1-py2.py3-none-any.whl (489kB)
Collecting html5lib==0.9999999 (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow)
Downloading html5lib-0.9999999.tar.gz (889kB)
Collecting bleach==1.5.0 (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow)
Downloading bleach-1.5.0-py2.py3-none-any.whl
Collecting markdown>=2.6.8 (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow)
Downloading Markdown-2.6.11-py2.py3-none-any.whl (78kB)
Collecting werkzeug>=0.11.10 (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow)
Downloading Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
Installing collected packages: six, absl-py, wheel, setuptools, protobuf, numpy, html5lib, bleach, markdown, werkzeug, tensorflow-tensorboard, tensorflow
Found existing installation: six 1.10.0
Uninstalling six-1.10.0:
Successfully uninstalled six-1.10.0
Running setup.py install for absl-py: started
Running setup.py install for absl-py: finished with status 'done'
Found existing installation: setuptools 28.8.0
Uninstalling setuptools-28.8.0:
Successfully uninstalled setuptools-28.8.0
Found existing installation: numpy 1.12.1+mkl
Uninstalling numpy-1.12.1+mkl:
Successfully uninstalled numpy-1.12.1+mkl
Running setup.py install for html5lib: started
Running setup.py install for html5lib: finished with status 'done'
Successfully installed absl-py-0.1.10 bleach-1.5.0 html5lib-0.9999999 markdown-2.6.11 numpy-1.14.1 protobuf-3.5.1 setuptools-38.5.1 six-1.11.0 tensorflow-1.5.0 tensorflow-tensorboard-1.5.1 werkzeug-0.14.1 wheel-0.30.0
D:\test>python
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf (匯入 tensorflow 套件並取名為 tf)
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\tensorflow\python\platform\self_check.py", line 47, in preload_check
ctypes.WinDLL(build_info.msvcp_dll_name)
File "C:\Python36\lib\ctypes\__init__.py", line 348, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] 找不到指定的模組。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python36\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
from tensorflow.python import *
File "C:\Python36\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
File "C:\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 30, in <module>
self_check.preload_check()
File "C:\Python36\lib\site-packages\tensorflow\python\platform\self_check.py", line 55, in preload_check
% build_info.msvcp_dll_name)
ImportError: Could not find 'msvcp140.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. You may install this DLL by downloading Visual C++ 2015 Redistributable Update 3 from this URL: https://www.microsoft.com/en-us/download/details.aspx?id=53587
錯誤原因是找不到 msvcp.dll 這個動態連結檔 (DLL), 並建議到微軟網站下載 VC++ 2015 更新檔, 我下載的是名為 vc_redist.x64.exe 的 64 位元版本 :
下載後直接點擊安裝, 完成後即可順利匯入 tensorflow 套件了 :
>>> import tensorflow as tf
>>> tf.__version__ #查詢 tebsorflow 版本
'1.5.0'
參考 :
# Error importing tensorflow on windows 10
如果因防火牆無法直接用 pip3 安裝, 參考上面安裝過程所示正確版本, 依序下載安裝下列相依模組安裝 :
# https://pypi.python.org/pypi/absl-py
# https://pypi.python.org/pypi/six#downloads
# https://pypi.python.org/pypi/wheel#downloads
# https://pypi.python.org/pypi/protobuf/3.5.1
# https://pypi.python.org/pypi/numpy#downloads
# https://pypi.python.org/pypi/html5lib/0.9999999
# https://pypi.python.org/pypi/bleach/1.5.0
# https://pypi.python.org/pypi/bleach/1.5.0#downloads
# https://pypi.python.org/pypi/Markdown
# https://pypi.python.org/pypi/Werkzeug
# https://pypi.python.org/pypi/tensorflow-tensorboard
# https://pypi.python.org/pypi/tensorflow/1.5.0
手動安裝過程如下 (必須依序, TensorFlow 的兩個檔案要最後安裝) :
D:\Python\tensorflow>pip3 install absl-py-0.1.10.tar.gz
Processing d:\python\tensorflow\absl-py-0.1.10.tar.gz
Requirement already satisfied: six in c:\python36\lib\site-packages (from absl-p
y==0.1.10)
Installing collected packages: absl-py
Running setup.py install for absl-py ... done
Successfully installed absl-py-0.1.10
D:\Python\tensorflow>pip3 install six-1.11.0-py2.py3-none-any.whl
Processing d:\python\tensorflow\six-1.11.0-py2.py3-none-any.whl
Installing collected packages: six
Found existing installation: six 1.10.0
Uninstalling six-1.10.0:
Successfully uninstalled six-1.10.0
Successfully installed six-1.11.0
D:\Python\tensorflow>pip3 install wheel-0.30.0-py2.py3-none-any.whl
Processing d:\python\tensorflow\wheel-0.30.0-py2.py3-none-any.whl
Installing collected packages: wheel
Successfully installed wheel-0.30.0
D:\Python\tensorflow>pip3 install protobuf-3.5.1-py2.py3-none-any.whl
Processing d:\python\tensorflow\protobuf-3.5.1-py2.py3-none-any.whl
Requirement already satisfied: setuptools in c:\python36\lib\site-packages (from
protobuf==3.5.1)
Requirement already satisfied: six>=1.9 in c:\python36\lib\site-packages (from p
rotobuf==3.5.1)
Installing collected packages: protobuf
Successfully installed protobuf-3.5.1
D:\Python\tensorflow>pip3 install numpy-1.14.1-cp36-none-win_amd64.whl
Processing d:\python\tensorflow\numpy-1.14.1-cp36-none-win_amd64.whl
Installing collected packages: numpy
Found existing installation: numpy 1.12.1+mkl
Uninstalling numpy-1.12.1+mkl:
Successfully uninstalled numpy-1.12.1+mkl
Successfully installed numpy-1.14.1
D:\Python\tensorflow>pip3 install html5lib-0.9999999.tar.gz
Processing d:\python\tensorflow\html5lib-0.9999999.tar.gz
Requirement already satisfied: six in c:\python36\lib\site-packages (from html5l
ib==0.9999999)
Building wheels for collected packages: html5lib
Running setup.py bdist_wheel for html5lib ... done
Stored in directory: C:\Users\cht\AppData\Local\pip\Cache\wheels\c7\97\52\e819
f90e3405b8364664de7d3da830ea032434cf57eb9975f1
Successfully built html5lib
Installing collected packages: html5lib
Successfully installed html5lib-0.9999999
D:\Python\tensorflow>pip3 install bleach-1.5.0-py2.py3-none-any.whl
Processing d:\python\tensorflow\bleach-1.5.0-py2.py3-none-any.whl
Requirement already satisfied: six in c:\python36\lib\site-packages (from bleach
==1.5.0)
Requirement already satisfied: html5lib!=0.9999,!=0.99999,<0.99999999,>=0.999 in
c:\python36\lib\site-packages (from bleach==1.5.0)
Installing collected packages: bleach
Successfully installed bleach-1.5.0
D:\Python\tensorflow>pip3 install Markdown-2.6.11-py2.py3-none-any.whl
Processing d:\python\tensorflow\markdown-2.6.11-py2.py3-none-any.whl
Installing collected packages: Markdown
Successfully installed Markdown-2.6.11
D:\Python\tensorflow>pip3 install Werkzeug-0.14.1-py2.py3-none-any.whl
Processing d:\python\tensorflow\werkzeug-0.14.1-py2.py3-none-any.whl
Installing collected packages: Werkzeug
Successfully installed Werkzeug-0.14.1
D:\Python\tensorflow>pip3 install tensorflow_tensorboard-1.5.1-py3-none-any.whl
Processing d:\python\tensorflow\tensorflow_tensorboard-1.5.1-py3-none-any.whl
Requirement already satisfied: six>=1.10.0 in c:\python36\lib\site-packages (fro
m tensorflow-tensorboard==1.5.1)
Requirement already satisfied: werkzeug>=0.11.10 in c:\python36\lib\site-package
s (from tensorflow-tensorboard==1.5.1)
Requirement already satisfied: wheel>=0.26; python_version >= "3" in c:\python36
\lib\site-packages (from tensorflow-tensorboard==1.5.1)
Requirement already satisfied: html5lib==0.9999999 in c:\python36\lib\site-packa
ges (from tensorflow-tensorboard==1.5.1)
Requirement already satisfied: bleach==1.5.0 in c:\python36\lib\site-packages (f
rom tensorflow-tensorboard==1.5.1)
Requirement already satisfied: protobuf>=3.4.0 in c:\python36\lib\site-packages
(from tensorflow-tensorboard==1.5.1)
Requirement already satisfied: numpy>=1.12.0 in c:\python36\lib\site-packages (f
rom tensorflow-tensorboard==1.5.1)
Requirement already satisfied: markdown>=2.6.8 in c:\python36\lib\site-packages
(from tensorflow-tensorboard==1.5.1)
Requirement already satisfied: setuptools in c:\python36\lib\site-packages (from
protobuf>=3.4.0->tensorflow-tensorboard==1.5.1)
Installing collected packages: tensorflow-tensorboard
Successfully installed tensorflow-tensorboard-1.5.1
D:\Python\tensorflow>pip3 install tensorflow-1.5.0-cp36-cp36m-win_amd64.whl
Processing d:\python\tensorflow\tensorflow-1.5.0-cp36-cp36m-win_amd64.whl
Requirement already satisfied: protobuf>=3.4.0 in c:\python36\lib\site-packages
(from tensorflow==1.5.0)
Requirement already satisfied: wheel>=0.26 in c:\python36\lib\site-packages (fro
m tensorflow==1.5.0)
Requirement already satisfied: absl-py>=0.1.6 in c:\python36\lib\site-packages (
from tensorflow==1.5.0)
Requirement already satisfied: tensorflow-tensorboard<1.6.0,>=1.5.0 in c:\python
36\lib\site-packages (from tensorflow==1.5.0)
Requirement already satisfied: numpy>=1.12.1 in c:\python36\lib\site-packages (f
rom tensorflow==1.5.0)
Requirement already satisfied: six>=1.10.0 in c:\python36\lib\site-packages (fro
m tensorflow==1.5.0)
Requirement already satisfied: setuptools in c:\python36\lib\site-packages (from
protobuf>=3.4.0->tensorflow==1.5.0)
Requirement already satisfied: bleach==1.5.0 in c:\python36\lib\site-packages (f
rom tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow==1.5.0)
Requirement already satisfied: werkzeug>=0.11.10 in c:\python36\lib\site-package
s (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow==1.5.0)
Requirement already satisfied: html5lib==0.9999999 in c:\python36\lib\site-packa
ges (from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow==1.5.0)
Requirement already satisfied: markdown>=2.6.8 in c:\python36\lib\site-packages
(from tensorflow-tensorboard<1.6.0,>=1.5.0->tensorflow==1.5.0)
Installing collected packages: tensorflow
Successfully installed tensorflow-1.5.0
接下來是可以安裝 Keras 框架.
二. 安裝 Keras (上層) :
Keras 是 Python 深度學習的一種上層框架 (模型層 model level) , 只負責機器學習模型之建立, 訓練, 與預測, 其底層的張量 (矩陣) 運算是依賴後端引擎 (backend engine) 來支援. 目前 Keras 提供兩種後端引擎 : Theano 與 TensorFlow, 後者具有跨平台與高效能之優點. 參考 :
# https://keras.io
Keras 字源來自希臘語, 原意為 horn (角), 最早出現於史詩奧德賽 Odyssey 中, 參考 :
# https://keras.io/#why-this-name-keras
Keras 的原始作者是 Francois Chollet, 他也是 Manning 出版的 "Deep Learning with Python" 一書的作者. Keras 早期版本於 2015 年三月底釋出, 經過社群力量改進, 目前最新版本是 2.1.4. Keras 於 2017 年被 Google TensorFlow 團隊相中納入核心函式庫; 微軟的 CNTK 也在其 2.0 版支援 Keras 為上層 ML 抽象框架, 這使得 Keras 使用者在 2017 年底突破 20 萬, 參考 :
# https://en.wikipedia.org/wiki/Keras
使用 pip3 安裝 keras 並將執行結果導至 keras.txt 檔案中 :
D:\> pip3 install keras > keras.txt
# https://keras.io
Keras 字源來自希臘語, 原意為 horn (角), 最早出現於史詩奧德賽 Odyssey 中, 參考 :
# https://keras.io/#why-this-name-keras
Keras 的原始作者是 Francois Chollet, 他也是 Manning 出版的 "Deep Learning with Python" 一書的作者. Keras 早期版本於 2015 年三月底釋出, 經過社群力量改進, 目前最新版本是 2.1.4. Keras 於 2017 年被 Google TensorFlow 團隊相中納入核心函式庫; 微軟的 CNTK 也在其 2.0 版支援 Keras 為上層 ML 抽象框架, 這使得 Keras 使用者在 2017 年底突破 20 萬, 參考 :
# https://en.wikipedia.org/wiki/Keras
Source : Amazon
使用 pip3 安裝 keras 並將執行結果導至 keras.txt 檔案中 :
D:\> pip3 install keras > keras.txt
Collecting keras
Downloading Keras-2.1.4-py2.py3-none-any.whl (322kB)
Requirement already satisfied: scipy>=0.14 in c:\python36\lib\site-packages (from keras)
Requirement already satisfied: six>=1.9.0 in c:\python36\lib\site-packages (from keras)
Collecting pyyaml (from keras)
Downloading PyYAML-3.12.tar.gz (253kB)
Requirement already satisfied: numpy>=1.9.1 in c:\python36\lib\site-packages (from keras)
Building wheels for collected packages: pyyaml
Running setup.py bdist_wheel for pyyaml: started
Running setup.py bdist_wheel for pyyaml: finished with status 'done'
Stored in directory: C:\Users\user\AppData\Local\pip\Cache\wheels\2c\f7\79\13f3a12cd723892437c0cfbde1230ab4d82947ff7b3839a4fc
Successfully built pyyaml
Installing collected packages: pyyaml, keras
Successfully installed keras-2.1.4 pyyaml-3.12
如果因防火牆無法直接用 pip3 安裝, 參考上面安裝過程, Keras 只有一個相依套件 PyYAML, 先下載安裝 PyYAML 再安裝 Keras :
# https://pypi.python.org/pypi/PyYAML
# https://pypi.python.org/pypi/Keras
手動安裝過程如下 :
D:\Python\tensorflow>pip3 install PyYAML-3.12.tar.gz
Processing d:\python\tensorflow\pyyaml-3.12.tar.gz
Building wheels for collected packages: PyYAML
Running setup.py bdist_wheel for PyYAML ... done
Stored in directory: C:\Users\cht\AppData\Local\pip\Cache\wheels\7b\34\c5\bf29
ad6ffcf25face23150f29caabc7c8bde70e0bb921b325a
Successfully built PyYAML
Installing collected packages: PyYAML
Successfully installed PyYAML-3.12
D:\Python\tensorflow>pip3 install Keras-2.1.4-py2.py3-none-any.whl
Processing d:\python\tensorflow\keras-2.1.4-py2.py3-none-any.whl
Requirement already satisfied: numpy>=1.9.1 in c:\python36\lib\site-packages (fr
om Keras==2.1.4)
Requirement already satisfied: scipy>=0.14 in c:\python36\lib\site-packages (fro
m Keras==2.1.4)
Requirement already satisfied: pyyaml in c:\python36\lib\site-packages (from Ker
as==2.1.4)
Requirement already satisfied: six>=1.9.0 in c:\python36\lib\site-packages (from
Keras==2.1.4)
Installing collected packages: Keras
Successfully installed Keras-2.1.4
安裝完成後, 進入 Python IDLE 匯入 keras 套件並檢視其版本 :
>>> import keras
Using TensorFlow backend.
>>> keras.__version__ #檢視 keras 版本
'2.1.4'
匯入套件時會顯示 "Using TensorFlow backend", 表示 Keras 預設以 TensorFlow 作為後端 (即底層) 深度學習引擎進行張量 (矩陣) 運算, 而這本書就是以 TensorFlow 為後端.
完成 TensorFlow + Keras 框架安裝後, 接下來就可以按圖索驥, 開始 ML 的實驗了.
參考 :
# Windows 安裝 Tensorflow
# 如何在Windows上安裝Keras、Theano、Tensorflow,並將Keras的後端由Theano切換為Tensorflow
2018-02-25 補充 :
晚上在高雄的電腦上安裝 Python 32 位元版本 (因系統回復更新), 結果無法安裝 TensorFlow (找不到符合之版本) :
D:\>pip3 install --upgrade tensorflow > tensorflow.txt
Could not find a version that satisfies the requirement tensorflow (from versions: )
No matching distribution found for tensorflow
D:\>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
回頭看 TensorFlow 官網說明, 原來需安裝 Python 64 位元版本. 已移除 32 位元版本改安裝 64 位元版本.
2018-02-26 補充 :
今天翻到第六章開始測試 MNIST 資料集, 但自 Keras 匯入 np_utils 模組時出現錯誤 :
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import pandas as pd
>>> from keras.utils import np_utils
Using TensorFlow backend.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python36\lib\site-packages\keras\__init__.py", line 3, in <module>
from . import utils
File "C:\Python36\lib\site-packages\keras\utils\__init__.py", line 25, in <mod
ule>
from .training_utils import multi_gpu_model
File "C:\Python36\lib\site-packages\keras\utils\training_utils.py", line 7, in
<module>
from ..layers.merge import concatenate
File "C:\Python36\lib\site-packages\keras\layers\__init__.py", line 4, in <mod
ule>
from ..engine import Layer
File "C:\Python36\lib\site-packages\keras\engine\__init__.py", line 8, in <mod
ule>
from .training import Model
File "C:\Python36\lib\site-packages\keras\engine\training.py", line 11, in <mo
dule>
from scipy.sparse import issparse
File "C:\Python36\lib\site-packages\scipy\__init__.py", line 61, in <module>
from numpy._distributor_init import NUMPY_MKL # requires numpy+mkl
ImportError: cannot import name 'NUMPY_MKL'
原來光是安裝 Numpy 還不夠, Keras 還用到了 Intel 的 MKL 數學函式庫, 我在以前安裝 ML 套件時就遇到過此問題, 參考 :
# 在 Windows 中安裝 Python 機器學習套件
Numpy + MKL 套件可在下列網址下載 :
# https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
然後以系統管理員身分開啟命令提示字元視窗以 pip3 指令安裝 Numpy + MKL :
D:\>pip3 install numpy-1.14.1+mkl-cp36-cp36m-win_amd64.whl
注意, 若不是以系統管理員身分開啟命令提示字元視窗, 可能會出現如下存取權限被拒問題 :
PermissionError: [WinError 5] 存取被拒。: 'C:\\Users\\tony\\AppData\\Local\\Temp\
\pip-o1p89kef-uninstall\\python36\\lib\\site-packages\\numpy\\.libs\\libopenblas
.bnvrk7633hsx7yvo2tadgr4a5kekxjaw.gfortran-win_amd64.dll'
安裝完畢後就可以順利匯入 np_utils 模組了 :
D:\Python\tensorflow>python
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import pandas as pd
>>> from keras.utils import np_utils
Using TensorFlow backend.
>>>
可見 keras 已可正常匯入, 並以 TensorFlow 為後端運算引擎.
註 : 後來發現其實這是 Python 3.6.1 版才會有此問題, 用最新的 3.6.4 版就不會.
2018-02-28 補充 :
高雄家中的電腦還原更新後重新安裝 Python 時沒注意到官網預設是下載 32-bit 版的, 結果到安裝 TensorFlow 時出現 "找不到符合之版本" 訊息, 仔細閱讀 TensorFlow 官網說明才知道必須安裝 Python 64-bit 版才行, 於是刪除 32-bit 版重新安裝 64-bit 版後安裝 TensorFlow 與 Keras 都沒問題了, 但是在匯入 keras 時卻出現了下面 "cannot import name '_ccallback_c'" 錯誤訊息 :
C:\Users\Tony>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python36\lib\site-packages\keras\__init__.py", line 3, in <module>
from . import utils
File "C:\Python36\lib\site-packages\keras\utils\__init__.py", line 25, in <module>
from .training_utils import multi_gpu_model
File "C:\Python36\lib\site-packages\keras\utils\training_utils.py", line 7, in <module>
from ..layers.merge import concatenate
File "C:\Python36\lib\site-packages\keras\layers\__init__.py", line 4, in <module>
from ..engine import Layer
File "C:\Python36\lib\site-packages\keras\engine\__init__.py", line 8, in <module>
from .training import Model
File "C:\Python36\lib\site-packages\keras\engine\training.py", line 11, in <module>
from scipy.sparse import issparse
File "C:\Python36\lib\site-packages\scipy\__init__.py", line 118, in <module>
from scipy._lib._ccallback import LowLevelCallable
File "C:\Python36\lib\site-packages\scipy\_lib\_ccallback.py", line 1, in <module>
from . import _ccallback_c
ImportError: cannot import name '_ccallback_c'
>>>
來回檢查 Numpy, Scipy, TemsorFlow, Keras 都是最新版, 怎會出問題? 難道是之前的 Python 32-bit 版殘餘勢力作祟? 因為在刪除 32-bit 版後我有注意到原安裝目錄 C:\Python36 仍然存在, 當時想應該沒關係吧! 真的, 沒關係就是有關係, 無計可施之餘只好刪除已安裝的 Python 64-bit, 然後將安裝目錄 C:\Python36 也全部刪除乾淨後再重新安裝 Python 64-bit, Scipy, matplotlib, pandas, TensorFlow, 與 Keras, 結果 OK!
結論 : 刪除舊版 Python 時一定要將原安裝目錄刪除掉. 剪不斷理還亂!
Downloading Keras-2.1.4-py2.py3-none-any.whl (322kB)
Requirement already satisfied: scipy>=0.14 in c:\python36\lib\site-packages (from keras)
Requirement already satisfied: six>=1.9.0 in c:\python36\lib\site-packages (from keras)
Collecting pyyaml (from keras)
Downloading PyYAML-3.12.tar.gz (253kB)
Requirement already satisfied: numpy>=1.9.1 in c:\python36\lib\site-packages (from keras)
Building wheels for collected packages: pyyaml
Running setup.py bdist_wheel for pyyaml: started
Running setup.py bdist_wheel for pyyaml: finished with status 'done'
Stored in directory: C:\Users\user\AppData\Local\pip\Cache\wheels\2c\f7\79\13f3a12cd723892437c0cfbde1230ab4d82947ff7b3839a4fc
Successfully built pyyaml
Installing collected packages: pyyaml, keras
Successfully installed keras-2.1.4 pyyaml-3.12
如果因防火牆無法直接用 pip3 安裝, 參考上面安裝過程, Keras 只有一個相依套件 PyYAML, 先下載安裝 PyYAML 再安裝 Keras :
# https://pypi.python.org/pypi/PyYAML
# https://pypi.python.org/pypi/Keras
手動安裝過程如下 :
D:\Python\tensorflow>pip3 install PyYAML-3.12.tar.gz
Processing d:\python\tensorflow\pyyaml-3.12.tar.gz
Building wheels for collected packages: PyYAML
Running setup.py bdist_wheel for PyYAML ... done
Stored in directory: C:\Users\cht\AppData\Local\pip\Cache\wheels\7b\34\c5\bf29
ad6ffcf25face23150f29caabc7c8bde70e0bb921b325a
Successfully built PyYAML
Installing collected packages: PyYAML
Successfully installed PyYAML-3.12
D:\Python\tensorflow>pip3 install Keras-2.1.4-py2.py3-none-any.whl
Processing d:\python\tensorflow\keras-2.1.4-py2.py3-none-any.whl
Requirement already satisfied: numpy>=1.9.1 in c:\python36\lib\site-packages (fr
om Keras==2.1.4)
Requirement already satisfied: scipy>=0.14 in c:\python36\lib\site-packages (fro
m Keras==2.1.4)
Requirement already satisfied: pyyaml in c:\python36\lib\site-packages (from Ker
as==2.1.4)
Requirement already satisfied: six>=1.9.0 in c:\python36\lib\site-packages (from
Keras==2.1.4)
Installing collected packages: Keras
Successfully installed Keras-2.1.4
安裝完成後, 進入 Python IDLE 匯入 keras 套件並檢視其版本 :
>>> import keras
Using TensorFlow backend.
>>> keras.__version__ #檢視 keras 版本
'2.1.4'
匯入套件時會顯示 "Using TensorFlow backend", 表示 Keras 預設以 TensorFlow 作為後端 (即底層) 深度學習引擎進行張量 (矩陣) 運算, 而這本書就是以 TensorFlow 為後端.
完成 TensorFlow + Keras 框架安裝後, 接下來就可以按圖索驥, 開始 ML 的實驗了.
參考 :
# Windows 安裝 Tensorflow
# 如何在Windows上安裝Keras、Theano、Tensorflow,並將Keras的後端由Theano切換為Tensorflow
2018-02-25 補充 :
晚上在高雄的電腦上安裝 Python 32 位元版本 (因系統回復更新), 結果無法安裝 TensorFlow (找不到符合之版本) :
D:\>pip3 install --upgrade tensorflow > tensorflow.txt
Could not find a version that satisfies the requirement tensorflow (from versions: )
No matching distribution found for tensorflow
D:\>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
回頭看 TensorFlow 官網說明, 原來需安裝 Python 64 位元版本. 已移除 32 位元版本改安裝 64 位元版本.
2018-02-26 補充 :
今天翻到第六章開始測試 MNIST 資料集, 但自 Keras 匯入 np_utils 模組時出現錯誤 :
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import pandas as pd
>>> from keras.utils import np_utils
Using TensorFlow backend.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python36\lib\site-packages\keras\__init__.py", line 3, in <module>
from . import utils
File "C:\Python36\lib\site-packages\keras\utils\__init__.py", line 25, in <mod
ule>
from .training_utils import multi_gpu_model
File "C:\Python36\lib\site-packages\keras\utils\training_utils.py", line 7, in
<module>
from ..layers.merge import concatenate
File "C:\Python36\lib\site-packages\keras\layers\__init__.py", line 4, in <mod
ule>
from ..engine import Layer
File "C:\Python36\lib\site-packages\keras\engine\__init__.py", line 8, in <mod
ule>
from .training import Model
File "C:\Python36\lib\site-packages\keras\engine\training.py", line 11, in <mo
dule>
from scipy.sparse import issparse
File "C:\Python36\lib\site-packages\scipy\__init__.py", line 61, in <module>
from numpy._distributor_init import NUMPY_MKL # requires numpy+mkl
ImportError: cannot import name 'NUMPY_MKL'
原來光是安裝 Numpy 還不夠, Keras 還用到了 Intel 的 MKL 數學函式庫, 我在以前安裝 ML 套件時就遇到過此問題, 參考 :
# 在 Windows 中安裝 Python 機器學習套件
Numpy + MKL 套件可在下列網址下載 :
# https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
然後以系統管理員身分開啟命令提示字元視窗以 pip3 指令安裝 Numpy + MKL :
D:\>pip3 install numpy-1.14.1+mkl-cp36-cp36m-win_amd64.whl
注意, 若不是以系統管理員身分開啟命令提示字元視窗, 可能會出現如下存取權限被拒問題 :
PermissionError: [WinError 5] 存取被拒。: 'C:\\Users\\tony\\AppData\\Local\\Temp\
\pip-o1p89kef-uninstall\\python36\\lib\\site-packages\\numpy\\.libs\\libopenblas
.bnvrk7633hsx7yvo2tadgr4a5kekxjaw.gfortran-win_amd64.dll'
安裝完畢後就可以順利匯入 np_utils 模組了 :
D:\Python\tensorflow>python
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import pandas as pd
>>> from keras.utils import np_utils
Using TensorFlow backend.
>>>
可見 keras 已可正常匯入, 並以 TensorFlow 為後端運算引擎.
註 : 後來發現其實這是 Python 3.6.1 版才會有此問題, 用最新的 3.6.4 版就不會.
2018-02-28 補充 :
高雄家中的電腦還原更新後重新安裝 Python 時沒注意到官網預設是下載 32-bit 版的, 結果到安裝 TensorFlow 時出現 "找不到符合之版本" 訊息, 仔細閱讀 TensorFlow 官網說明才知道必須安裝 Python 64-bit 版才行, 於是刪除 32-bit 版重新安裝 64-bit 版後安裝 TensorFlow 與 Keras 都沒問題了, 但是在匯入 keras 時卻出現了下面 "cannot import name '_ccallback_c'" 錯誤訊息 :
C:\Users\Tony>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python36\lib\site-packages\keras\__init__.py", line 3, in <module>
from . import utils
File "C:\Python36\lib\site-packages\keras\utils\__init__.py", line 25, in <module>
from .training_utils import multi_gpu_model
File "C:\Python36\lib\site-packages\keras\utils\training_utils.py", line 7, in <module>
from ..layers.merge import concatenate
File "C:\Python36\lib\site-packages\keras\layers\__init__.py", line 4, in <module>
from ..engine import Layer
File "C:\Python36\lib\site-packages\keras\engine\__init__.py", line 8, in <module>
from .training import Model
File "C:\Python36\lib\site-packages\keras\engine\training.py", line 11, in <module>
from scipy.sparse import issparse
File "C:\Python36\lib\site-packages\scipy\__init__.py", line 118, in <module>
from scipy._lib._ccallback import LowLevelCallable
File "C:\Python36\lib\site-packages\scipy\_lib\_ccallback.py", line 1, in <module>
from . import _ccallback_c
ImportError: cannot import name '_ccallback_c'
>>>
來回檢查 Numpy, Scipy, TemsorFlow, Keras 都是最新版, 怎會出問題? 難道是之前的 Python 32-bit 版殘餘勢力作祟? 因為在刪除 32-bit 版後我有注意到原安裝目錄 C:\Python36 仍然存在, 當時想應該沒關係吧! 真的, 沒關係就是有關係, 無計可施之餘只好刪除已安裝的 Python 64-bit, 然後將安裝目錄 C:\Python36 也全部刪除乾淨後再重新安裝 Python 64-bit, Scipy, matplotlib, pandas, TensorFlow, 與 Keras, 結果 OK!
結論 : 刪除舊版 Python 時一定要將原安裝目錄刪除掉. 剪不斷理還亂!
2 則留言 :
Hi Tony:
我是菜鳥, 今年初開始學習Python的。 最近一周就是無法安裝Tensorflow(Windows10);還好您的大作與熱心救了我。 我一找您的步驟, 總算安裝成功了;真誠感謝,老天賜福您 !
Hi, 欽容兄, 我也是菜鳥, 我只是花了點時間記下作了甚麼以對治自己的健忘症. 能解決您的問題我也很開心! 多交流!
張貼留言