Windows環境下python中 Keras深度學習環境搭建

基礎介紹

Keras是一個模型級的庫,提供了快速構建深度學習網絡的模塊。Keras並不處理如張量乘法、卷積等底層操作。這些操作依賴於某種特定的、優化良好的張量操作庫。Keras依賴於處理張量的庫就稱爲「後端引擎」。Keras提供了三種後端引擎Theano/Tensorflow/CNTK,並將其函數統一封裝,使得用戶可以以同一個接口調用不同後端引擎的函數

Theano是一個開源的符號主義張量操作框架,由蒙特利爾大學LISA/MILA實驗室開發。

TensorFlow是一個符號主義的張量操作框架,由Google開發。
CNTK是一個由微軟開發的商業級工具包。

Keras使用了下面的基礎依賴包,
numpy,scipy
pyyaml
HDF5, h5py(可選,僅在模型的save/load函數中使用)
三種後端必須至少選擇一種,tensorflow , keras , CNTK。

本人是在windows 10 64位系統下 anaconda 對應python3.6版:Anaconda3-5.2.0-Windows-x86_64.exe。
在anaconda包裝後python環境下已集成大多常用的基礎開發包,因此推薦anaconda python安裝後,再安裝其它深度學習需要的擴展包。
anaconda下載地址:
清華鏡像 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
中科大鏡像 https://mirrors.ustc.edu.cn/anaconda/archive/
https://repo.continuum.io/archive/
官網 https://www.anaconda.com/distribution/ (最新版)

安裝依賴庫mingw和libpython

直接在windows命令行中conda安裝,採用如下命令:
conda install mingw libpython
由於網速過慢,則 ctrl + C 中斷,決定採用下載安裝包安裝。

安裝依賴庫 mingw

下載MinGW綠色安裝包到本地。然後解壓安裝。解壓後將其中的所有文件夾拷貝到Anaconda目錄下。
百度或谷歌都能夠下載到,下載時需要注意操作系統是32位還是64位。安裝完MinGW後需要配置環境變量:
可通過以下地址下載mingw windows 4位綠色安裝包:
https://repo.anaconda.com/pkgs/free/win-64/mingw-4.7-1.tar.bz2

環境變量配置:(要與個人的anaconda目錄匹配)
path=D:\ProgramData\Anaconda3\MinGW\bin;D:\ProgramData\Anaconda3\MinGW\x86_64-w64-mingw32\lib;

(環境變量配置方法爲:點擊」此電腦「-》「屬性」->」高級系統設置「->點擊最下方的」環境變量「->在下方的」系統變量「框中雙擊」Path「彈出變量設置頁,如下:)
在這裏插入圖片描述

至此,MinGW安裝結束。如果安裝配置正確,則可以直接使用MinGW測試編譯C++。

MinGW安裝情況測試:
在命令提示符中輸入

>gcc --version
gcc.exe (GCC) 4.7.0 20111220 (experimental)
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

編寫C++源文件
源文件test1.cpp,代碼如下(下面爲C代碼,只是使用G++來編譯而已),建立test1.cpp文件

#include <stdio.h>
int main() 
{
    printf("Hello World!");
    return 0;
}

命令提示符運行如下:

D:\code\C>gcc -o test test1.cpp
D:\code\C>test
Hello World!
D:\code\C>

安裝libpython

如通過CMD命令行conda install mingw libpython將MinGW和libpython都安裝成功,則不需要進行該步單獨安裝libpython。

單獨安裝libpython時,首先需要從網上去下載libpython安裝文件,這裏給出一個Anaconda官方提供的libpython安裝文件下載地址。
https://anaconda.org/anaconda/libpython/files

選擇自己對應的版本下載即可,下載完畢後直接在CMD中通過命令:

conda install file-name(如:conda install libpython-2.0-py27_0.tar.bz2)
powershell中運行代碼如下:

D:\download\install_src\WinPythonLib> conda install libpython-2.1-py36_0.tar.bz2
Downloading and Extracting Packages
############################################################################################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

安裝完可以通過conda list查看包安裝情況:

PS D:\download\install_src\WinPythonLib> conda list libpython
# packages in environment at d:\ProgramData\Anaconda3:
#
# Name                    Version                   Build  Channel
libpython                 2.1                      py36_0    <unknown>
PS D:\download\install_src\WinPythonLib>

安裝Theano

Theano直接通過命令行pip install theano進行在線安裝;也可以離線下載安裝包,將安裝包解壓後將theano文件拷貝到安裝包目錄,再配置環境變量。

下面是離線安裝的方法:
Theano下載地址:https://pypi.org/project/Theano/#files
解壓到任意目錄,進入安裝目錄,命令行運行 python setup.py install 安裝文件。具體如下:

PS D:\download\install_src\WinPythonLib> cd .\Theano-1.0.4\
PS D:\download\install_src\WinPythonLib\Theano-1.0.4> python setup.py install
running install
running bdist_egg

到此,在Windows上theano安裝結束。
查看安裝情況:

PS D:\download\install_src\WinPythonLib\Theano-1.0.4> conda list theano
# packages in environment at d:\ProgramData\Anaconda3:
#
# Name                    Version                   Build  Channel
theano                    1.0.4                    pypi_0    pypi

在python中運行import theano,如果沒有提示則表明安裝成功,否則失敗。 如下:

PS D:\download\install_src\WinPythonLib\Theano-1.0.4> python
Python 3.6.5 |Anaconda custom (64-bit)| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import theano
>>> exit();
PS D:\download\install_src\WinPythonLib\Theano-1.0.4>

安裝Keras

網絡支持的話直接 conda install keras 。不便上網的話,先離線下載安裝包再安裝。可參考下面是相關下載鏈接和安裝過程:

keras 安裝依賴的包有 Keras-Preprocessing, Keras-Applications 等需要先安裝。
keras下載地址:https://pypi.org/project/Keras/#files
Keras-Preprocessing 下載地址:https://pypi.org/project/Keras-Preprocessing/#files
Keras-Applications 下載地址:https://pypi.org/project/Keras-Applications/#files

解壓到任意目錄,進入安裝目錄,命令行運行 python setup.py install 安裝文件。具體如下:

PS D:\download\install_src\WinPythonLib> cd .\Keras-2.2.4\
PS D:\download\install_src\WinPythonLib\Keras-2.2.4> python setup.py install
running install
running bdist_egg

.......

查看keras安裝情況

PS D:\download\install_src\WinPythonLib\Keras-2.2.4> conda list Keras
# packages in environment at d:\ProgramData\Anaconda3:
#
# Name                    Version                   Build  Channel
keras-applications        1.0.7                    pypi_0    pypi
keras-preprocessing       1.0.8                    pypi_0    pypi
PS D:\download\install_src\WinPythonLib\Keras-2.2.4> python
Python 3.6.5 |Anaconda custom (64-bit)| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
>>>