深度學習(二)theano環境搭建
theano環境搭建
作者:hjimce
搭建theano實屬不易,因為每個人的電腦不一樣,所以安裝過程會有所區別,特別是安裝cuda的時候,很容易驅動衝突。網上教程一大堆,但是我都沒搭建成功,最後根據官網的教程,一步一步的琢磨,總算功夫不負有心人。因此寫一下艱辛的theano安裝歷程
環境:win7+64位系統
硬體:膝上型電腦, 顯示卡型號:GTX 850M
現在假設電腦啥都沒裝,開始從頭到尾進行環境搭建。
1、安裝Anconda。
因為如果安裝純淨版python,還需要自己安裝其它的numpy、matpolt等庫,挺麻煩的。所以我直接安裝整合的
因為我是64位系統,因此選擇64位的版本
安裝完成後,在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的安裝命令改為:
因為我已經安裝過了,所以輸入安裝命令後,提示的是: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)測試配置是否有誤
測試開始前,需要重啟電腦,因為我們上面配置了環境變數,系統的環境變數設定完了需要重啟電腦才能有效果。
測試方法一、
測試程式碼:
import numpy as np
import time
import theano
A = np.random.rand(1000,10000).astype(theano.config.floatX)
B = np.random.rand(10000,1000).astype(theano.config.floatX)
np_start = time.time()
AB = A.dot(B)
np_end = time.time()
X,Y = theano.tensor.matrices('XY')
mf = theano.function([X,Y],X.dot(Y))
t_start = time.time()
tAB = mf(A,B)
t_end = time.time()
print "NP time: %f[s], theano time: %f[s] (times should be close when run on CPU!)" %(
np_end-np_start, t_end-t_start)
print "Result difference: %f" % (np.abs(AB-tAB).max(), )
把上面的程式碼拷貝複製一下,然後用python執行一下結果如下:
測試方案二、
在python命令視窗中輸入:
Import theano
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,並編譯執行得到如下結果,代表安裝成功:
如果有問題,這個例子執行後,會有錯誤提示資訊。
接著在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,其檔案內容如下:
// ISO C9x compliant stdint.h for Microsoft Visual Studio
// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124
//
// Copyright (c) 2006-2013 Alexander Chemeris
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the product nor the names of its contributors may
// be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef _MSC_VER // [
#error "Use this header only with Microsoft Visual C++ compilers!"
#endif // _MSC_VER ]
#ifndef _MSC_STDINT_H_ // [
#define _MSC_STDINT_H_
#if _MSC_VER > 1000
#pragma once
#endif
#if _MSC_VER >= 1600 // [
#include <stdint.h>
#else // ] _MSC_VER >= 1600 [
#include <limits.h>
// For Visual Studio 6 in C++ mode and for many Visual Studio versions when
// compiling for ARM we should wrap <wchar.h> include with 'extern "C++" {}'
// or compiler give many errors like this:
// error C2733: second C linkage of overloaded function 'wmemchr' not allowed
#ifdef __cplusplus
extern "C" {
#endif
# include <wchar.h>
#ifdef __cplusplus
}
#endif
// Define _W64 macros to mark types changing their size, like intptr_t.
#ifndef _W64
# if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300
# define _W64 __w64
# else
# define _W64
# endif
#endif
// 7.18.1 Integer types
// 7.18.1.1 Exact-width integer types
// Visual Studio 6 and Embedded Visual C++ 4 doesn't
// realize that, e.g. char has the same size as __int8
// so we give up on __intX for them.
#if (_MSC_VER < 1300)
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
#else
typedef signed __int8 int8_t;
typedef signed __int16 int16_t;
typedef signed __int32 int32_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
#endif
typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
// 7.18.1.2 Minimum-width integer types
typedef int8_t int_least8_t;
typedef int16_t int_least16_t;
typedef int32_t int_least32_t;
typedef int64_t int_least64_t;
typedef uint8_t uint_least8_t;
typedef uint16_t uint_least16_t;
typedef uint32_t uint_least32_t;
typedef uint64_t uint_least64_t;
// 7.18.1.3 Fastest minimum-width integer types
typedef int8_t int_fast8_t;
typedef int16_t int_fast16_t;
typedef int32_t int_fast32_t;
typedef int64_t int_fast64_t;
typedef uint8_t uint_fast8_t;
typedef uint16_t uint_fast16_t;
typedef uint32_t uint_fast32_t;
typedef uint64_t uint_fast64_t;
// 7.18.1.4 Integer types capable of holding object pointers
#ifdef _WIN64 // [
typedef signed __int64 intptr_t;
typedef unsigned __int64 uintptr_t;
#else // _WIN64 ][
typedef _W64 signed int intptr_t;
typedef _W64 unsigned int uintptr_t;
#endif // _WIN64 ]
// 7.18.1.5 Greatest-width integer types
typedef int64_t intmax_t;
typedef uint64_t uintmax_t;
// 7.18.2 Limits of specified-width integer types
#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [ See footnote 220 at page 257 and footnote 221 at page 259
// 7.18.2.1 Limits of exact-width integer types
#define INT8_MIN ((int8_t)_I8_MIN)
#define INT8_MAX _I8_MAX
#define INT16_MIN ((int16_t)_I16_MIN)
#define INT16_MAX _I16_MAX
#define INT32_MIN ((int32_t)_I32_MIN)
#define INT32_MAX _I32_MAX
#define INT64_MIN ((int64_t)_I64_MIN)
#define INT64_MAX _I64_MAX
#define UINT8_MAX _UI8_MAX
#define UINT16_MAX _UI16_MAX
#define UINT32_MAX _UI32_MAX
#define UINT64_MAX _UI64_MAX
// 7.18.2.2 Limits of minimum-width integer types
#define INT_LEAST8_MIN INT8_MIN
#define INT_LEAST8_MAX INT8_MAX
#define INT_LEAST16_MIN INT16_MIN
#define INT_LEAST16_MAX INT16_MAX
#define INT_LEAST32_MIN INT32_MIN
#define INT_LEAST32_MAX INT32_MAX
#define INT_LEAST64_MIN INT64_MIN
#define INT_LEAST64_MAX INT64_MAX
#define UINT_LEAST8_MAX UINT8_MAX
#define UINT_LEAST16_MAX UINT16_MAX
#define UINT_LEAST32_MAX UINT32_MAX
#define UINT_LEAST64_MAX UINT64_MAX
// 7.18.2.3 Limits of fastest minimum-width integer types
#define INT_FAST8_MIN INT8_MIN
#define INT_FAST8_MAX INT8_MAX
#define INT_FAST16_MIN INT16_MIN
#define INT_FAST16_MAX INT16_MAX
#define INT_FAST32_MIN INT32_MIN
#define INT_FAST32_MAX INT32_MAX
#define INT_FAST64_MIN INT64_MIN
#define INT_FAST64_MAX INT64_MAX
#define UINT_FAST8_MAX UINT8_MAX
#define UINT_FAST16_MAX UINT16_MAX
#define UINT_FAST32_MAX UINT32_MAX
#define UINT_FAST64_MAX UINT64_MAX
// 7.18.2.4 Limits of integer types capable of holding object pointers
#ifdef _WIN64 // [
# define INTPTR_MIN INT64_MIN
# define INTPTR_MAX INT64_MAX
# define UINTPTR_MAX UINT64_MAX
#else // _WIN64 ][
# define INTPTR_MIN INT32_MIN
# define INTPTR_MAX INT32_MAX
# define UINTPTR_MAX UINT32_MAX
#endif // _WIN64 ]
// 7.18.2.5 Limits of greatest-width integer types
#define INTMAX_MIN INT64_MIN
#define INTMAX_MAX INT64_MAX
#define UINTMAX_MAX UINT64_MAX
// 7.18.3 Limits of other integer types
#ifdef _WIN64 // [
# define PTRDIFF_MIN _I64_MIN
# define PTRDIFF_MAX _I64_MAX
#else // _WIN64 ][
# define PTRDIFF_MIN _I32_MIN
# define PTRDIFF_MAX _I32_MAX
#endif // _WIN64 ]
#define SIG_ATOMIC_MIN INT_MIN
#define SIG_ATOMIC_MAX INT_MAX
#ifndef SIZE_MAX // [
# ifdef _WIN64 // [
# define SIZE_MAX _UI64_MAX
# else // _WIN64 ][
# define SIZE_MAX _UI32_MAX
# endif // _WIN64 ]
#endif // SIZE_MAX ]
// WCHAR_MIN and WCHAR_MAX are also defined in <wchar.h>
#ifndef WCHAR_MIN // [
# define WCHAR_MIN 0
#endif // WCHAR_MIN ]
#ifndef WCHAR_MAX // [
# define WCHAR_MAX _UI16_MAX
#endif // WCHAR_MAX ]
#define WINT_MIN 0
#define WINT_MAX _UI16_MAX
#endif // __STDC_LIMIT_MACROS ]
// 7.18.4 Limits of other integer types
#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260
// 7.18.4.1 Macros for minimum-width integer constants
#define INT8_C(val) val##i8
#define INT16_C(val) val##i16
#define INT32_C(val) val##i32
#define INT64_C(val) val##i64
#define UINT8_C(val) val##ui8
#define UINT16_C(val) val##ui16
#define UINT32_C(val) val##ui32
#define UINT64_C(val) val##ui64
// 7.18.4.2 Macros for greatest-width integer constants
// These #ifndef's are needed to prevent collisions with <boost/cstdint.hpp>.
// Check out Issue 9 for the details.
#ifndef INTMAX_C // [
# define INTMAX_C INT64_C
#endif // INTMAX_C ]
#ifndef UINTMAX_C // [
# define UINTMAX_C UINT64_C
#endif // UINTMAX_C ]
#endif // __STDC_CONSTANT_MACROS ]
#endif // _MSC_VER >= 1600 ]
#endif // _MSC_STDINT_H_ ]
然後把它放到目錄:
C:\ProgramFiles(x86)\CommonFiles\Microsoft\VisualC++forPython\9.0\VC\include\stdint.h 下面。
5、重新配置檔案.theanorc.txt。把步驟2(3)中建立的檔案:.theanorc.txt 內容改為如下內容:
[blas]
ldflags=
[global]
device = gpu
floatX = float32
[nvcc]
fastmath=True
flags =-LC:\Anaconda\libs
compiler_bindir=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin
[gcc]
cxxflags = -IC:\Anaconda\MinGW\include
這樣就完成了theano的GPU配置了。上面的:
compiler_bindir=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin
這個如果你的電腦是裝vs2012,那麼就把10.0改為11.0。也就是說上面的路徑就是你安裝的vs所在的目錄
6、完整測試。
測試的python程式碼如下:
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 xrange(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)GPU測試
下面是用GPU加速的執行結果:
如上執行結果可知,用gpu進行計算時間差不多是0.68秒左右。如果想切換成只用cpu的測試的話,我是通過更改檔案:.theanorc.txt的內容。如果開啟gpu,那麼.theanorc.txt的內容為:
[blas]
ldflags=
[global]
device = gpu
floatX = float32
[nvcc]
fastmath=True
flags =-LC:\Anaconda\libs
compiler_bindir=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin
[gcc]
cxxflags = -IC:\Anaconda\MinGW\include
(2)CPU測試。如果想關閉gpu,進行cpu測試那麼就把.theanorc.txt內容改為:
[blas]
ldflags=
[gcc]
cxxflags = -IC:\Anaconda\MinGW\include
還有需要重啟電腦。不知道有沒有更好的在gpu與cpu切換的方法,如果有請不吝指導,因為我這個方法每次都要重啟,下面是測試結果圖:測試了結果,只用cpu花了13秒的時間,也就是說對於我的電腦,使用gpu進行加速,這速度提高了近20倍。
參考文獻:
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,一直不知道錯在哪