【theano】win10+anaconda+python3.6+theano1.0/Tensorflow/CNTK安裝

網(wǎng)絡(luò)上的教程很多,但有很多不符合情況,更甚者有很多錯(cuò)誤。比如一窩蜂都要安裝MinGW, 甚至有重新安裝GCC編譯器的,要求.theanorc.txt沒有空格的,設(shè)置PYTHONPATH為theano目錄的。。。
偶然看到一篇非常完善的教程,但里面也有一些包太老,借此翻新總結(jié)下,為后面進(jìn)坑者指路。
參考https://github.com/philferriere/dlwinhttps://zhuanlan.zhihu.com/p/29903472?utm_source=wechat_session&utm_medium=social(本文有部分圖摘自該博客)

配置了 VS 2015、Anaconda 3.4、CUDA 8.0 和 cuDNN v5.1 等基本環(huán)境,然后再從 Keras 出發(fā)安裝 Theano、TensorFlow 和 CNTK 以作為其后端,并在MNIST實(shí)驗(yàn)中實(shí)驗(yàn)了不同后端的性能。

1. VS2015,CUDA, cuDNN, Anaconda安裝見前篇

2. 建立依賴環(huán)境,通過安裝keras順便安裝theano

conda create  -n py36 python=3.6 numpy scipy mkl-service m2w64-toolchain libpython jupyter
activate py36
conda install -c conda-forge pygpu #They could be more up to date
conda install nose
pip install keras==2.0.5

注意: 原文要求pygpu和libgpuarray均為0.6.2版本,但事實(shí)theano不再更新了,推薦安裝theano 1.0版本,而該版本的theano要pygpu版本在0.7以上,0.8以下。而一般的pip或conda安裝均為0.69,所以推薦用conda-forge安裝最新版本。安裝完pygpu,libgpuarray也會(huì)順便安裝好。

3. 安裝CNTK

pip install https://cntk.ai/PythonWheel/GPU/cntk-2.0-cp36-cp36m-win_amd64.whl

而這會(huì)在對(duì)應(yīng)環(huán)境下安裝額外的CUDA和cuDNN,需要移除對(duì)應(yīng)的cu*.dll文件(我的環(huán)境目錄為D:\ProgramData\Anaconda3\envs\py36)

4. 安裝Tensorflow

pip install tensorflow-gpu==1.2.0

5. 檢查libraries

image.png

image.png

image.png

image.png

6. 檢查是否3個(gè)后端都安裝成功

python -c "import theano; print('theano: %s, %s' % (theano.__version__, theano.__file__))"
python -c "import pygpu; print('pygpu: %s, %s' % (pygpu.__version__, pygpu.__file__))"
python -c "import tensorflow; print('tensorflow: %s, %s' % (tensorflow.__version__, tensorflow.__file__))"
python -c "import cntk; print('cntk: %s, %s' % (cntk.__version__, cntk.__file__))"

7. 為方便啟動(dòng),設(shè)置三個(gè)環(huán)境變量。

THEANO_FLAGS_CPU=floatX=float32,device=cpu
THEANO_FLAGS_GPU=floatX=float32,device=cuda0,dnn.enabled=False,gpuarray.preallocate=0.8
THEANO_FLAGS_GPU_DNN=floatX=float32,device=cuda0,optimizer_including=cudnn,gpuarray.preallocate=0.7,dnn.conv.algo_bwd_filter=deterministic,dnn.conv.algo_bwd_data=deterministic,dnn.include_path=D:/Program Files/GPU computing Toolkit/v8.0/include,dnn.library_path=D:/Program Files/GPU computing Toolkit/v8.0/lib/x64/

由于theano只識(shí)別THEANO_FLAGS,所以通過set THEANO_FLAGS=%THEANO_FLAGS_CPU%或者其余兩個(gè)GPU/GPU_DNN來決定運(yùn)行方式,通過set KERAS_BACKEND=theano/tensorflow/cntk來決定后段

8. PATH 環(huán)境變量

image.png

9. 測(cè)試

見參考鏈接

10. 遇到的問題

  1. cpu和gpu運(yùn)行正常,但cuDNN加速不能用,報(bào)錯(cuò)
Can not use cuDNN on context None: cannot compile with cuDNN. We got this error:

In file included from C:\\CUDA\\v8.0\\include/driver_types.h:53:0,      
from C:\\CUDA\\v8.0\\include/cudnn.h:63,
from C:\\Users\\jtc92\\AppData\\Local\\Temp\\try_flags_733_4ik6.c:4:
C:\\CUDA\\v8.0\\include/host_defines.h:84:0: warning: "__cdecl" redefined
#define __cdecl <built-in>: 
note: this is the location of the previous definition

C:/Anaconda3/Library/mingw-w64/bin/../lib/gcc/x86_64-w64-mingw32/5.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: 
cannot find -lcudnn\r\ncollect2.exe: error: ld returned 1 exit status

參考issues,再次查看THEANO_FLAGS_GPU_DNN,分隔符是/,不是\

  1. 報(bào)錯(cuò)configparser.py的,看看是否PYTHON_HOME這樣的變量寫錯(cuò)了
  2. VS的主要作用是提供cl.exe,如果涉及cl.exe,可以考慮這個(gè)角度
  3. 還有theano編譯器不固定,需要制定cxx,參考這個(gè)
  4. 一直提示需要指定一個(gè)DEVICE,查看theano源碼,發(fā)現(xiàn)os.environ中并沒有傳DEVICE=cuda, 雖然.theanorc.txt有寫,所以目前是通過環(huán)境變量來啟動(dòng)的。
  5. 還有WARNING: Using NumPy C-API based implementation for BLAS functions,這個(gè)也待解決
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容