首次在windows上搭建 theano ,為了實現在 gpu 加速運算,在配置運行環境上多次踩坑,特記錄下來。
一、GPU驅動安裝
1.1 進入NVIDIA驅動下載進行符合條件的下載安裝。
二、CUDA的下載安裝
2.1 從NVIDIA官網選擇一個CUDA版本進行下載。
2.2 使用如下命令查看是否安裝成功:
nvcc -V
2.3 配置path環境
我安裝的是 10.1 所以為:
三、CUDNN的下載安裝
3.1 這里是下載地址。根據自己安裝的CUDA版本選擇相應的CUDNN的版本。
3.2 我解壓后放到目錄 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1 目錄下了。
四、Anacond安裝
4.1 下載地址:https://www.anaconda.com/download/
4.2 安裝 anacond
4.3 系統 path 變量增加
C:\Anaconda3
C:\Anaconda3\Scripts
C:\Anaconda3\Library\bin
4.4 新建conda環境
conda create -n env_name37 anaconda python=3.7
4.5 激活conda環境
conda activate env_name37
4.6 在windows 菜單上更換 anacond 運行環境
Anaconda Prompt (env_name37)
4.7 安裝 theano
conda install theano
4.8 在目錄 C:\Users\ [Your_User] 下創建配置文件 .theanorc
該文件內容如下:
[global] device = cuda floatX=float32 [nvcc] flags=--machine=64 [cuda] root = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1 [dnn] library_path = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64 include_path = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include [lib] cnmem=100
五、Theano 1.0.3 issue with pygpu: ERROR (theano.gpuarray): Could not initialize pygpu, support disabled 出錯處理
5.1 在 python 下運行 import theano 會出錯:
ERROR (theano.gpuarray): Could not initialize pygpu, support disabled Traceback (most recent call last): File "C:\ProgramData\Anaconda3\lib\site-packages\theano\gpuarray\__init__.py", line 227, in use(config.device) File "C:\ProgramData\Anaconda3\lib\site-packages\theano\gpuarray\__init__.py", line 214, in use init_dev(device, preallocate=preallocate) File "C:\ProgramData\Anaconda3\lib\site-packages\theano\gpuarray\__init__.py", line 99, in init_dev **args) File "pygpu\gpuarray.pyx", line 658, in pygpu.gpuarray.init File "pygpu\gpuarray.pyx", line 587, in pygpu.gpuarray.pygpu_init pygpu.gpuarray.GpuArrayException: b'Could not load "nvrtc64_70.dll": \xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xc4\xa3\xbf\xe9\xa1\xa3\r\n'
這是因為我使用的 cuda是10.1 而 pygpu 0.7.6 並不支持。雖然 git 上的代碼已經修改,但並未生成新版本。所以需要自己手動編譯來解決這個問題。
5.2 git 獲得源碼
conda install cmake git clone https://github.com/Theano/libgpuarray.git cd libgpuarray mkdir build cd build cmake .. -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 15 Win64"
5.3 手動編譯
用VS編譯gpuarray.vcxproj 工程生成新的 gpuarray.dll (我編譯好一個,下載請解壓。)
復蓋 C:/Anaconda3/Library/bin 目錄下的同名文件
這時重新運行 import theano 會獲得如下錯誤:
pygpu.gpuarray.GpuArrayException: b'Could not load "nvrtc64_101.dll": The specified module could not be found.\r\n'
5.4 手動生成 nvrtc64_101.dll
將 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin\目錄下的 nvrtc64_101_0.dll 文件 copy 到C:/Anaconda3/Library/bin 目錄下,並更名為 nvrtc64_101.dll
這時重新運行 import theano 會獲得如下錯誤:
pygpu.gpuarray.GpuArrayException: (b'Missing Blas library', 5)
5.5 手動生成 cublas64_101.dll
將 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin\目錄下的 cublas64_10.dll 文件 copy 到C:/Anaconda3/Library/bin 目錄下,並更名為 cublas64_101.dll
這時重新運行 import theano 成功。
*如果還有其它什么錯,通常是 path 變量中沒有包含需要執行的文件的目錄。
5.6 下載 check_blas.py
下載地址:https://raw.githubusercontent.com/Theano/Theano/master/theano/misc/check_blas.py
運行:
python check_blas.py
結果終於成功了: