Win10+VS2015+cuda8.0+theano+keras的配置過程
本blog記錄了在windows10下配置theano的過程,以便以後有需要的情況下查詢。
首先說明配置的環境:
1.win10 64bit
2.VS2015
3.Anaconda2 64bit
一、安裝Anaconda
正常安裝即可,我安裝的是Anaconda2,對應的是python2,我自定義的路徑是D:\software\Anaconda2
二、安裝MinGw
1.開啟cmd,進入命令列
2.輸入conda install mingw libpython 。稍等片刻即可完成安裝。此時有D:\software\Anaconda2\MinGW 路徑。
注意:若下載較慢,且反覆多次不成功時,可以新增conda 映象,輸入下面的命令:
conda config --add channels 'https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/'
conda config --set show_channel_urls yes
三、配置環境變數
1.編輯使用者變數中的path變數(如果沒有就新建一個,一般會有的),在後邊追加D:\software\Anaconda2;D:\software\Anaconda2\Scripts; 不要漏掉分號,此處因為我的Anaconda的安裝目錄是C:\Anaconda,此處需要根據自己的安裝目錄填寫。
2.在使用者變數中新建變數pythonpath,變數值為C:\Anaconda\Lib\site-packages\theano; ,此處就是指明安裝的theano的目錄是哪,但是現在咱們還沒有安裝,所以不著急,先寫完再說。
3.開啟cmd,會看到窗口裡邊有個路徑,我的是C:\Users\Locked>,根據自己的路徑,找到對應的目錄,在該目錄下新建一個文字文件.theanorc.txt (注意有兩個“.”),編輯它,寫入以下內容:
[global]
openmp=False
[blas]
ldflags=
[gcc]
cxxflags=-ID:\software\Anaconda2\MinGW
其中紅體字部分是你安裝的Anaconda的路徑,一定不要弄錯。否則找不到MinGw。注意:凡是對環境變數進行修改的操作,都應該重啟。
四、安裝theano
1.開啟CMD,方法和安裝MinGw一樣,不要進入python。
2.輸入pip install theano,回車後就是賞心悅目的下載進度條,這個很小,所以安裝的比較快。
3.在cmd中,輸入python 進入到python環境下,然後先輸入import theano回車,需要等一段時間。
五、配置GPU加速
1.安裝VS2015
2.安裝cuda8.0,預設路徑
3.驗證cuda8.0是否安裝成功
參考:http://www.linuxidc.com/Linux/2016-12/138862.htm
4.配置.theanorc.txt,如下:
[global]
openmp=False
floatX=float32
device=gpu
allow_input_downcast=True
[gcc]
cxxflags=-ID:\software\Anaconda2\MinGW
[lib]
cnmem=0.8
[blas]
ldflas=-lopenblas
[nvcc]
--flags=-LD:\software\Anaconda2\libs
--compiler_bindir=D:\software\vs2015\VC\bin\amd64
fastmath=True
flags=-D_FORCE_INLINES
[cuda]
root = -LC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0
5.測試theano,用以下程式碼:
from theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time
vlen = 10 * 30 * 768 # 10 x #cores x # threads per core
iters = 1000
rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print (f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
r = f()
t1 = time.time()
print ('Looping %d times took' % iters, t1 - t0, 'seconds')
print ('Result is', r)
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
print ('Used the cpu')
else:
print ('Used the gpu')
注意:
1.在cmd下進入python,然後import theano後,跑出一大序列號及C++程式碼,且提示如下:
{
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\crtdefs.h(10):
fatal error C1083: Cannot open include file: 'corecrt.h': No such file or directory
}
必須新增底下到系統的環境變數裡,原因是他會用到Windows Kits裡的函式,不只VS2015
所也必須宣告進去。
INCLUDE=C:\Program Files (x86)\Windows Kits\10\Include\10.0.14393.0\ucrt
LIB=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.14393.0\um\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10240.0\ucrt\x64
參考http://www.gpuworld.cn/article/show/604.html