深度學習(二)theano環境搭建

theano環境搭建

原文地址http://blog.csdn.net/hjimce/article/details/46654229

作者:hjimce

搭建theano實屬不易,因爲每個人的電腦不一樣,所以安裝過程會有所區別,特別是安裝cuda的時候,很容易驅動衝突。網上教程一大堆,但是我都沒搭建成功,最後根據官網的教程,一步一步的琢磨,總算功夫不負有心人。因此寫一下艱辛的theano安裝歷程

環境:win7+64位系統 

硬件:筆記本電腦, 顯卡型號:GTX 850M

現在假設電腦啥都沒裝,開始從頭到尾進行環境搭建。

1、安裝Anconda

因爲如果安裝純淨版Python,還需要自己安裝其它的numpy、matpolt等庫,挺麻煩的。所以我直接安裝集成的Anaconda ,這個軟件包含了Ipython,還有許多python的計算庫。下載地址爲:http://www.continuum.io/downloads


因爲我是64位系統,因此選擇64位的版本,下載後文件爲:Anaconda-2.2.0-Windows-x86_64.exe,下載完成後,就直接雙擊開始安裝,全部都選取默認的就可以了,默認會安裝到:C:\Anconda。

安裝完成後,在window開始菜單下,的所有程序中找到已安裝的Anaconda,如下:

打開Anaconda的命令窗口:Anaconda Command Prompt,然後輸入命令:conda list      可以查看Anaconda爲我們安裝的python相關的包:


裏面有非常多的包,如:numpy, nose, pip, python, scipy。

2、安裝mingw、theano

(1)mingw 安裝

有的Anaconda 是有包含mingw的,不過我下載到的版本安裝完以後上面的包列表中並沒有mingw,也就是C:\Anconda文件下沒有MinGW文件夾,因此需要自己在線安裝。這個如果沒有裝好,後面使用theano的時候會提示:g++ no detect,還有g++不是內部命令什麼的。總之如果錯誤提示g++問題,就代表mingw沒有安裝或配置好。

我們在Anaconda命令窗口中,輸入mingw的安裝命令: conda install mingw。

聲明修改:這一步用命令conda install mingw錯了,最後theano安裝完後,輸入命令“import theano”會出現:no module named gof 的錯誤 。需要把mingw的安裝命令改爲: conda install mingw libpython。纔不會出現後面的no module named gof 錯誤



因爲我已經安裝過了,所以輸入安裝命令後,提示的是:All requested packages already installed。也就是已經安裝完了,如果還沒有安裝的,它會自動鏈接在線安裝。

mingw安裝完後,在C:\Anconda文件下會出現:名爲MinGW的文件夾。

(2)theano 安裝

與mingw的安裝類似,直接在anaconda的命令窗口中輸入命令:pip install theano。接着會自動進行在線安裝,如下所示:


最後安裝成功了會提示:successfully installed theano。

(3)配置環境變量

在桌面上我的電腦右鍵-》屬性-》高級系統設置-》環境變量。即可進入環境變量設置界面如下:


步驟一、在系統環境變量中選擇“變量path”,在後面加入值:“c:\Anaconda\MinGW\bin;c:\Anaconda\MinGW\x86_64-mingw32\lib;”(如果操作系統爲32位的變量值輸入爲“c:\Anaconda\MinGW\bin;c:\Anaconda\MinGW\i686_w64-mingw32\lib;”)(注意要帶分號)

步驟二、新建環境變量。變量名爲“PYTHONPATH”,變量值爲“C:\Anaconda\Lib\site-packages\theano;”(同樣注意要帶分號)

步驟三、打開C盤-》用戶-》當前用戶(根據你的電腦用戶而定)。因爲我的電腦現在所用的是超級管理員用戶Adminstrator,因此打開Adminstrator用戶


用戶目錄


在用戶Adminstrator下面創建文件名爲:“.theanorc.txt”,文件內容爲:

“[blas] 
ldflags=


[gcc] 
cxxflags = -IC:\Anaconda\MinGW\include”

即:


ok,到了這裏我們已經完成了theano配置的上半部分,這個時候theano已經可以用了,接着需要做個測試,測試一下自己上面的配置有沒有問題。

(4)測試配置是否有誤

測試開始前,需要重啓電腦,因爲我們上面配置了環境變量,系統的環境變量設置完了需要重啓電腦纔能有效果。

測試方法一、

測試代碼:

[python] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. import numpy as np  
  2. import time  
  3. import theano  
  4. A = np.random.rand(1000,10000).astype(theano.config.floatX)  
  5. B = np.random.rand(10000,1000).astype(theano.config.floatX)  
  6. np_start = time.time()  
  7. AB = A.dot(B)  
  8. np_end = time.time()  
  9. X,Y = theano.tensor.matrices('XY')  
  10. mf = theano.function([X,Y],X.dot(Y))  
  11. t_start = time.time()  
  12. tAB = mf(A,B)  
  13. t_end = time.time()  
  14. print "NP time: %f[s], theano time: %f[s] (times should be close when run on CPU!)" %(  
  15.                                            np_end-np_start, t_end-t_start)  
  16. print "Result difference: %f" % (np.abs(AB-tAB).max(), )  

把上面的代碼拷貝複製一下,然後用python運行一下結果如下:


如果上面的np time 和theano time 差不多,那就代表你上面的配置沒有問題了,這個有的時候電腦還有其他的任務,也有可能導致運行的時間不一致。

測試方案二、

在python命令窗口中輸入:

[python] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. Import theano  
  2. print theano.config.blas.ldflags  

沒有出錯(沒有返回值)則說明已經配置成功,如下圖所示,就代表成功了:

測試方案三、驗證BLAS是否安裝成功。由於numpy是依賴BLAS的,如果BLAS沒有安裝成功,雖然numpy亦可以安裝,但是無法使用BLAS的加速。驗證numpy是否真的成功依賴BLAS編譯,用以下代碼試驗: 

>>> import numpy 
>>> id(numpy.dot) == id(numpy.core.multiarray.dot) 
False 
結果爲False表示成功依賴了BLAS加速,如果是Ture則表示用的是python自己的實現並沒有加速。
結果如下代表成功:

3、安裝CUDA

上面的theano配置只是完成了上半部分,這個時候還不能進行gpu加速。這個時候我們可以用如下命令:

>>import  theano

>>theano.test()

測試看一下結果如下,這個時候會跳出PyCUDA的相關錯誤信息,因爲我們還沒有安裝CUDA。


OK,接着我們要做的就是安裝CUDA了。具體步驟如下:

(1)安裝vs根據theano官網的安裝教程,到網站:http://go.microsoft.com/?linkid=9709969  下載到:VS2010Express1.iso,然後用虛擬光驅打開,然後在打開VCExpress文件,雙擊安裝文件:VCExpress\setup.exe  

(2)安裝CUDA。

a、下載合適的cuda版本。這一步很操蛋,因爲我一開始是根據官網教程,到這個網站:https://developer.nvidia.com/cuda-toolkit-55-archive  根據我是筆記本電腦同時是win7 64位系統,最後下載了cuda_5.5.20_winvista_win7_win8_notebook_64.exe。等了一個小時終於下載完了,下載完後進行安裝,結果一安裝就出現如下錯誤:


告訴我說圖形驅動與顯卡不兼容,如果繼續安裝,即使安裝成功了,也不能使用cuda。於是我就去下載了個高一點的版本:cuda6.5,等了一個小時終於下載完了,結果一運行還是同樣的錯誤。

最後我下載了最新的版本:cuda_7.0.28_windows.exe   終於沒有錯誤了。因此安裝cuda需要根據電腦的顯卡型號確定,因爲我的電腦是剛買不久的,所以顯卡比較先進。

根據上面的步驟,我的電腦找到了合適版本爲cuda7.0版本。接着就需要安裝cuda_7.0.28_windows.exe這玩意了 

b、安裝cuda。下載完後,直接雙擊安裝,選擇自定義安裝,然後把所有包的都勾選上,省的後面出現什麼錯誤。這一步有可能會遇到驅動衝突,導致某些包安裝失敗,比如我第一次安裝的時候,結果圖形驅動包就安裝失敗了。


如果某些包安裝失敗,後面使用theano的時候,會跳出錯誤。像我圖形驅動安裝失敗,運行theano的時候就出現錯誤提示爲cuda版本與驅動版本不一致。因此如果你安裝cuda的過程中,有出現安裝失敗的,那麼請你接着往下看。安裝失敗一般是驅動衝突的問題,這個時候我的方法是用驅動精靈卸載掉顯卡驅動,然後在進行安裝。如果是筆記本電腦,因爲是雙顯卡的,那麼就先卸載掉NVIDIA的,另外一個Intel的驅動保留的着。


然後在進行安裝CUDA ,還有intel驅動最好是官方驅動,不然也有可能衝突,導致安裝失敗。

ok,安裝完後測試一下是否安裝正確。

在命令提示符窗口中輸入:nvcc -V,回車查看是否有版本信息。若出現版本信息,則證明nvcc安裝成功,如下圖所示:


接着我們運行一個cuda自帶的測試例子,名字爲:deviceQuery_vs2012.sln  ,這個例子目錄爲:C:\Program Files\NVIDIA Corporation\Installer2\CUDASamples_7.0.{E78AE18E-ED3C-4168-AF5B-561BDF7F2BBB}\1_Utilities\deviceQuery 。我用vs2012打開了deviceQuery_vs2012.sln,並編譯運行得到如下結果,代表安裝成功:


如果有問題,這個例子運行後,會有錯誤提示信息。

4、下載並安裝Microsoft Visual C++ Compiler for Python 2.7下載到的文件爲:VCForPython27.msi。

接着在dos命令窗口中,cd到VCForPython27.msi所在的目錄,然後輸入安裝命令: msiexec /i VCForPython27.msi ALLUSERS=1

接着會進行安裝VCForPython27.msi。其將被安裝到: C:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0.目錄下

安裝完後,可以到這個目錄下看看有沒有上面這個目錄。

安裝完了以後,我們新建一個文件名爲:stdint.h,其文件內容如下: 

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. // ISO C9x  compliant stdint.h for Microsoft Visual Studio  
  2. // Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124   
  3. //   
  4. //  Copyright (c) 2006-2013 Alexander Chemeris  
  5. //   
  6. // Redistribution and use in source and binary forms, with or without  
  7. // modification, are permitted provided that the following conditions are met:  
  8. //   
  9. //   1. Redistributions of source code must retain the above copyright notice,  
  10. //      this list of conditions and the following disclaimer.  
  11. //   
  12. //   2. Redistributions in binary form must reproduce the above copyright  
  13. //      notice, this list of conditions and the following disclaimer in the  
  14. //      documentation and/or other materials provided with the distribution.  
  15. //   
  16. //   3. Neither the name of the product nor the names of its contributors may  
  17. //      be used to endorse or promote products derived from this software  
  18. //      without specific prior written permission.  
  19. //   
  20. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED  
  21. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF  
  22. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO  
  23. // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
  25. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
  26. // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,   
  27. // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
  28. // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
  29. // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
  30. //   
  31. ///////////////////////////////////////////////////////////////////////////////  
  32.   
  33. #ifndef _MSC_VER // [  
  34. #error "Use this header only with Microsoft Visual C++ compilers!"  
  35. #endif // _MSC_VER ]  
  36.   
  37. #ifndef _MSC_STDINT_H_ // [  
  38. #define _MSC_STDINT_H_  
  39.   
  40. #if _MSC_VER > 1000  
  41. #pragma once  
  42. #endif  
  43.   
  44. #if _MSC_VER >= 1600 // [  
  45. #include <stdint.h>  
  46. #else // ] _MSC_VER >= 1600 [  
  47.   
  48. #include <limits.h>  
  49.   
  50. // For Visual Studio 6 in C++ mode and for many Visual Studio versions when  
  51. // compiling for ARM we should wrap <wchar.h> include with 'extern "C++" {}'  
  52. // or compiler give many errors like this:  
  53. //   error C2733: second C linkage of overloaded function 'wmemchr' not allowed  
  54. #ifdef __cplusplus  
  55. extern "C" {  
  56. #endif  
  57. #  include <wchar.h>  
  58. #ifdef __cplusplus  
  59. }  
  60. #endif  
  61.   
  62. // Define _W64 macros to mark types changing their size, like intptr_t.  
  63. #ifndef _W64  
  64. #  if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300  
  65. #     define _W64 __w64  
  66. #  else  
  67. #     define _W64  
  68. #  endif  
  69. #endif  
  70.   
  71.   
  72. // 7.18.1 Integer types  
  73.   
  74. // 7.18.1.1 Exact-width integer types  
  75.   
  76. // Visual Studio 6 and Embedded Visual C++ 4 doesn't  
  77. // realize that, e.g. char has the same size as __int8  
  78. // so we give up on __intX for them.  
  79. #if (_MSC_VER < 1300)  
  80.    typedef signed char       int8_t;  
  81.    typedef signed short      int16_t;  
  82.    typedef signed int        int32_t;  
  83.    typedef unsigned char     uint8_t;  
  84.    typedef unsigned short    uint16_t;  
  85.    typedef unsigned int      uint32_t;  
  86. #else  
  87.    typedef signed __int8     int8_t;  
  88.    typedef signed __int16    int16_t;  
  89.    typedef signed __int32    int32_t;  
  90.    typedef unsigned __int8   uint8_t;  
  91.    typedef unsigned __int16  uint16_t;  
  92.    typedef unsigned __int32  uint32_t;  
  93. #endif  
  94. typedef signed __int64       int64_t;  
  95. typedef unsigned __int64     uint64_t;  
  96.   
  97.   
  98. // 7.18.1.2 Minimum-width integer types  
  99. typedef int8_t    int_least8_t;  
  100. typedef int16_t   int_least16_t;  
  101. typedef int32_t   int_least32_t;  
  102. typedef int64_t   int_least64_t;  
  103. typedef uint8_t   uint_least8_t;  
  104. typedef uint16_t  uint_least16_t;  
  105. typedef uint32_t  uint_least32_t;  
  106. typedef uint64_t  uint_least64_t;  
  107.   
  108. // 7.18.1.3 Fastest minimum-width integer types  
  109. typedef int8_t    int_fast8_t;  
  110. typedef int16_t   int_fast16_t;  
  111. typedef int32_t   int_fast32_t;  
  112. typedef int64_t   int_fast64_t;  
  113. typedef uint8_t   uint_fast8_t;  
  114. typedef uint16_t  uint_fast16_t;  
  115. typedef uint32_t  uint_fast32_t;  
  116. typedef uint64_t  uint_fast64_t;  
  117.   
  118. // 7.18.1.4 Integer types capable of holding object pointers  
  119. #ifdef _WIN64 // [  
  120.    typedef signed __int64    intptr_t;  
  121.    typedef unsigned __int64  uintptr_t;  
  122. #else // _WIN64 ][  
  123.    typedef _W64 signed int   intptr_t;  
  124.    typedef _W64 unsigned int uintptr_t;  
  125. #endif // _WIN64 ]  
  126.   
  127. // 7.18.1.5 Greatest-width integer types  
  128. typedef int64_t   intmax_t;  
  129. typedef uint64_t  uintmax_t;  
  130.   
  131.   
  132. // 7.18.2 Limits of specified-width integer types  
  133.   
  134. #if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [   See footnote 220 at page 257 and footnote 221 at page 259  
  135.   
  136. // 7.18.2.1 Limits of exact-width integer types  
  137. #define INT8_MIN     ((int8_t)_I8_MIN)  
  138. #define INT8_MAX     _I8_MAX  
  139. #define INT16_MIN    ((int16_t)_I16_MIN)  
  140. #define INT16_MAX    _I16_MAX  
  141. #define INT32_MIN    ((int32_t)_I32_MIN)  
  142. #define INT32_MAX    _I32_MAX  
  143. #define INT64_MIN    ((int64_t)_I64_MIN)  
  144. #define INT64_MAX    _I64_MAX  
  145. #define UINT8_MAX    _UI8_MAX  
  146. #define UINT16_MAX   _UI16_MAX  
  147. #define UINT32_MAX   _UI32_MAX  
  148. #define UINT64_MAX   _UI64_MAX  
  149.   
  150. // 7.18.2.2 Limits of minimum-width integer types  
  151. #define INT_LEAST8_MIN    INT8_MIN  
  152. #define INT_LEAST8_MAX    INT8_MAX  
  153. #define INT_LEAST16_MIN   INT16_MIN  
  154. #define INT_LEAST16_MAX   INT16_MAX  
  155. #define INT_LEAST32_MIN   INT32_MIN  
  156. #define INT_LEAST32_MAX   INT32_MAX  
  157. #define INT_LEAST64_MIN   INT64_MIN  
  158. #define INT_LEAST64_MAX   INT64_MAX  
  159. #define UINT_LEAST8_MAX   UINT8_MAX  
  160. #define UINT_LEAST16_MAX  UINT16_MAX  
  161. #define UINT_LEAST32_MAX  UINT32_MAX  
  162. #define UINT_LEAST64_MAX  UINT64_MAX  
  163.   
  164. // 7.18.2.3 Limits of fastest minimum-width integer types  
  165. #define INT_FAST8_MIN    INT8_MIN  
  166. #define INT_FAST8_MAX    INT8_MAX  
  167. #define INT_FAST16_MIN   INT16_MIN  
  168. #define INT_FAST16_MAX   INT16_MAX  
  169. #define INT_FAST32_MIN   INT32_MIN  
  170. #define INT_FAST32_MAX   INT32_MAX  
  171. #define INT_FAST64_MIN   INT64_MIN  
  172. #define INT_FAST64_MAX   INT64_MAX  
  173. #define UINT_FAST8_MAX   UINT8_MAX  
  174. #define UINT_FAST16_MAX  UINT16_MAX  
  175. #define UINT_FAST32_MAX  UINT32_MAX  
  176. #define UINT_FAST64_MAX  UINT64_MAX  
  177.   
  178. // 7.18.2.4 Limits of integer types capable of holding object pointers  
  179. #ifdef _WIN64 // [  
  180. #  define INTPTR_MIN   INT64_MIN  
  181. #  define INTPTR_MAX   INT64_MAX  
  182. #  define UINTPTR_MAX  UINT64_MAX  
  183. #else // _WIN64 ][  
  184. #  define INTPTR_MIN   INT32_MIN  
  185. #  define INTPTR_MAX   INT32_MAX  
  186. #  define UINTPTR_MAX  UINT32_MAX  
  187. #endif // _WIN64 ]  
  188.   
  189. // 7.18.2.5 Limits of greatest-width integer types  
  190. #define INTMAX_MIN   INT64_MIN  
  191. #define INTMAX_MAX   INT64_MAX  
  192. #define UINTMAX_MAX  UINT64_MAX  
  193.   
  194. // 7.18.3 Limits of other integer types  
  195.   
  196. #ifdef _WIN64 // [  
  197. #  define PTRDIFF_MIN  _I64_MIN  
  198. #  define PTRDIFF_MAX  _I64_MAX  
  199. #else  // _WIN64 ][  
  200. #  define PTRDIFF_MIN  _I32_MIN  
  201. #  define PTRDIFF_MAX  _I32_MAX  
  202. #endif  // _WIN64 ]  
  203.   
  204. #define SIG_ATOMIC_MIN  INT_MIN  
  205. #define SIG_ATOMIC_MAX  INT_MAX  
  206.   
  207. #ifndef SIZE_MAX // [  
  208. #  ifdef _WIN64 // [  
  209. #     define SIZE_MAX  _UI64_MAX  
  210. #  else // _WIN64 ][  
  211. #     define SIZE_MAX  _UI32_MAX  
  212. #  endif // _WIN64 ]  
  213. #endif // SIZE_MAX ]  
  214.   
  215. // WCHAR_MIN and WCHAR_MAX are also defined in <wchar.h>  
  216. #ifndef WCHAR_MIN // [  
  217. #  define WCHAR_MIN  0  
  218. #endif  // WCHAR_MIN ]  
  219. #ifndef WCHAR_MAX // [  
  220. #  define WCHAR_MAX  _UI16_MAX  
  221. #endif  // WCHAR_MAX ]  
  222.   
  223. #define WINT_MIN  0  
  224. #define WINT_MAX  _UI16_MAX  
  225.   
  226. #endif // __STDC_LIMIT_MACROS ]  
  227.   
  228.   
  229. // 7.18.4 Limits of other integer types  
  230.   
  231. #if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [   See footnote 224 at page 260  
  232.   
  233. // 7.18.4.1 Macros for minimum-width integer constants  
  234.   
  235. #define INT8_C(val)  val##i8  
  236. #define INT16_C(val) val##i16  
  237. #define INT32_C(val) val##i32  
  238. #define INT64_C(val) val##i64  
  239.   
  240. #define UINT8_C(val)  val##ui8  
  241. #define UINT16_C(val) val##ui16  
  242. #define UINT32_C(val) val##ui32  
  243. #define UINT64_C(val) val##ui64  
  244.   
  245. // 7.18.4.2 Macros for greatest-width integer constants  
  246. // These #ifndef's are needed to prevent collisions with <boost/cstdint.hpp>.  
  247. // Check out Issue 9 for the details.  
  248. #ifndef INTMAX_C //   [  
  249. #  define INTMAX_C   INT64_C  
  250. #endif // INTMAX_C    ]  
  251. #ifndef UINTMAX_C //  [  
  252. #  define UINTMAX_C  UINT64_C  
  253. #endif // UINTMAX_C   ]  
  254.   
  255. #endif // __STDC_CONSTANT_MACROS ]  
  256.   
  257. #endif // _MSC_VER >= 1600 ]  
  258.   
  259. #endif // _MSC_STDINT_H_ ]  

然後把它放到目錄

C:\Program Files (x86)\CommonFiles\Microsoft\Visual C++ for Python\9.0\VC\include\stdint.h 下面。

5、重新配置文件.theanorc.txt。把步驟2(3)中建立的文件:.theanorc.txt 內容改爲如下內容:

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. [blas]   
  2. ldflags=  
  3.   
  4. [global]  
  5. device = gpu  
  6. floatX = float32  
  7.   
  8.   
  9. [nvcc]  
  10. fastmath=True  
  11. flags =-LC:\Anaconda\libs  
  12. compiler_bindir=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin  
  13.   
  14. [gcc]   
  15. cxxflags = -IC:\Anaconda\MinGW\include  

這樣就完成了theano的GPU配置了。上面的:

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. compiler_bindir=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin  
這個如果你的電腦是裝vs2012,那麼就把10.0改爲11.0。也就是說上面的路徑就是你安裝的vs所在的目錄

6、完整測試。

測試的python代碼如下:

[python] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. from theano import function, config, shared, sandbox  
  2. import theano.tensor as T  
  3. import numpy  
  4. import time  
  5.   
  6. vlen = 10 * 30 * 768  # 10 x #cores x # threads per core  
  7. iters = 1000  
  8.   
  9. rng = numpy.random.RandomState(22)  
  10. x = shared(numpy.asarray(rng.rand(vlen), config.floatX))  
  11. f = function([], T.exp(x))  
  12. print f.maker.fgraph.toposort()  
  13. t0 = time.time()  
  14. for i in xrange(iters):  
  15.     r = f()  
  16. t1 = time.time()  
  17. print 'Looping %d times took' % iters, t1 - t0, 'seconds'  
  18. print 'Result is', r  
  19. if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):  
  20.     print 'Used the cpu'  
  21. else:  
  22.     print 'Used the gpu'  

運行結果如下:

(1)GPU測試

下面是用GPU加速的運行結果:

如上運行結果可知,用gpu進行計算時間差不多是0.68秒左右。如果想切換成只用cpu的測試的話,我是通過更改文件:.theanorc.txt的內容。如果開啓gpu,那麼.theanorc.txt的內容爲:

[python] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. [blas]   
  2. ldflags=  
  3.   
  4. [global]  
  5. device = gpu  
  6. floatX = float32  
  7.   
  8.   
  9. [nvcc]  
  10. fastmath=True  
  11. flags =-LC:\Anaconda\libs  
  12. compiler_bindir=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin  
  13.   
  14. [gcc]   
  15. cxxflags = -IC:\Anaconda\MinGW\include  

(2)CPU測試。如果想關閉gpu,進行cpu測試那麼就把.theanorc.txt內容改爲:

[python] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. [blas]   
  2. ldflags=  
  3.   
  4.   
  5.   
  6. [gcc]   
  7. cxxflags = -IC:\Anaconda\MinGW\include  
還有需要重啓電腦。不知道有沒有更好的在gpu與cpu切換的方法,如果有請不吝指導,因爲我這個方法每次都要重啓,下面是測試結果圖:

測試了結果,只用cpu花了13秒的時間,也就是說對於我的電腦,使用gpu進行加速,這速度提高了近20倍。

本文地址:http://blog.csdn.net/hjimce/article/details/46654229     作者:hjimce     聯繫qq:1393852684   更多資源請關注我的博客:http://blog.csdn.net/hjimce                原創文章,轉載請保留本行信息。

參考文獻:

1、http://deeplearning.net/software/theano/install_windows.html#install-windows

2、http://blog.163.com/yuyang_tech/blog/static/216050083201469101518900/

3、http://ihsgnef.github.io/theano-cuda-windows/

附錄:

1、在調用theano.test()測試的時候,如果出現:no  module  name  theano 的錯誤,表明要麼沒有安裝,theano。如果確保已經安裝了,那麼就是你:高級-》環境變量 的路徑沒有設置好。

上面的環境變量設置:“C:\Anaconda\Lib\site-packages\theano;”查看一下是否有這個目錄。比如我另外一臺電腦安裝的時候,不知怎麼回事,theano的安裝目錄竟然是大寫的:Theano,一直不知道錯在哪

發佈了0 篇原創文章 · 獲贊 2 · 訪問量 5979
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章