近期在學習深度學習,需要在本機上安裝keras框架,好上手。上網查了一些資料,弄了幾天今天終於完全搞好了。本次是使用GPU進行加速,使用cpu處理的請查看之前的隨筆keras在win7下環境搭建
本機配置:win7 64位的,4G內存,gtx970顯卡
安裝條件:
vs2010(不一定非要是vs2010,恰好我有vs2010,應該是配置GPU編程時需要用到vs的編譯器)
cuda如果系統是64位的就下載64位,至於cuda的版本,有的說要和對應的顯卡版本匹配,我就安裝了8.0,實驗來看,cuda版本和顯卡型號貌似關系不是很大。
cudnn是深度學習進行加速的。不是必選,但是有的話以后運行效率會高很多。版本什么的一定要配套。
前面的過程和使用cpu計算是相同的。請參考之前隨筆。keras在win7下環境搭建
之前的步驟處理完之后,
1 安裝VS2010,只選擇裝C++語言就夠。
2 安裝cuda 安裝Cuda8,安裝的時候,選擇“自定義安裝”,安裝全部功能,還有要安裝到默認位置最好,安裝很簡單,可能需要點時間。
安裝完后,打開環境變量應該會多出來2個變量,CUDA_PATH_V6_5和CUDA_PATH.
打開cmd控制台命令行,輸入命令nvcc –V回車(注意是大寫V)就可以查看版本信息,如果安裝正確會顯示Cuda的版本號。
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Sat_Sep__3_19:05:48_CDT_2016
Cuda compilation tools, release 8.0, V8.0.44
3 修改配置.theanorc.txt,如下:
[global]
openmp=False
device = gpu0
floatX = float32
allow_input_downcast=True
[lib]
cnmem = 1
[blas]
ldflags=
[gcc]
cxxflags=-ID:\Anaconda2\MinGW #此處是gcc的路徑
[nvcc]
flags = -LD:\Anaconda2\libs #此處是Anaconda的路徑
compiler_bindir = D:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin #此處一定要和你安裝的VS的路徑保持一致,如果是默認安裝的,應該是C:\Program Files(x86)\Microsoft Visual Studio 10.0\VC\bin
fastmath = True
注意:網上有的配置文件中沒有[lib]這個塊,后面導入theano時會出現CNMeM is disabled提示。
4 安裝cudnn
將下載來的文件解壓,解壓出cuda文件夾,里面包含3個文件夾。將設三個文件夾替換掉系統里面的對應文件,進行覆蓋替換即可。C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0
注意:如果沒有覆蓋掉后面導入theano時會出現CuDNN not available提示。
5 切換后端,因為我用的是theano,而keras默認使用tensorflow。切換方法有英文資料
Switching from one backend to another
If you have run Keras at least once, you will find the Keras configuration file at:
~/.keras/keras.json
If it isn't there, you can create it.
The default configuration file looks like this:
{
"image_dim_ordering": "tf",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
Simply change the field backend to either "theano" or "tensorflow", and Keras will use the new configuration next time you run any Keras code.
照着做就行了。
6 此時正常來說應該就可以了,進行一下測試。測試代碼如下
測試1,在cmd命令窗口下輸入
>>> import theano DEBUG: nvcc STDOUT nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' archite ctures are deprecated, and may be removed in a future release (Use -Wno-deprecat ed-gpu-targets to suppress warning). nvcc warning : nvcc support for Microsoft Visual Studio 2010 and earlier has bee n deprecated and is no longer being maintained mod.cu support for Microsoft Visual Studio 2010 has been deprecated! 正在創建庫 C:/Users/allen/AppData/Local/Theano/compiledir_Windows-7-6.1.7601- SP1-Intel64_Family_6_Model_60_Stepping_3_GenuineIntel-2.7.12-64/tmp1wscvx/265abc 51f7c376c224983485238ff1a5.lib 和對象 C:/Users/allen/AppData/Local/Theano/compil edir_Windows-7-6.1.7601-SP1-Intel64_Family_6_Model_60_Stepping_3_GenuineIntel-2. 7.12-64/tmp1wscvx/265abc51f7c376c224983485238ff1a5.exp Using gpu device 0: GeForce GTX 970 (CNMeM is enabled with initial size: 95.0% o f memory, cuDNN 5005)
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 %f seconds" % (iters, t1 - t0)) print("Result is %s" % (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')