Mac下CUDA開啟及Tensorflow-gpu 1.4 安裝
在之前的文章中,筆者介紹了在Mac下安裝Tensorflow及開發環境搭建。但是感覺用CPU跑機器學習還是太慢了,所以便研究研究如何使用GPU來跑。
1.執行環境
軟體環境:
macOS Sierra 10.12.6
Xcode 8.2.1
Python 2.7
Homebrew
GPU Driver: WebDriver-378.05.05.25f03
CUDA Driver: cudadriver-8.0.90-macos
CudaToolkit: cuda_8.0.61_mac
CUDNN: cudnn-8.0-osx-x64-v6.0
硬體環境:
CPU:2.3 GHz Intel Core i7
Memory:16 GB 1600 MHz DDR3
GPU:NVIDIA GeForce GT 750M 2048 MB
首先需要注意的是,新版的Macbook現在搭載的都是A卡,這類機器的話除了用OpenCL的方法之外,還可以通過外接顯示卡的方法跑機器學習。
2.CUDA安裝
首先需要說明的是:CUDA Driver與NVIDIA GPU Driver的版本必須一致,才能讓CUDA找到顯示卡。
在mac上安裝CUDA最麻煩的事情就是版本匹配問題,這個版本匹配指的是MacOS版本和CUDA Driver、GPU Driver的版本都要匹配。這個網站提供了詳細的版本對照:http://www.macvidcards.com/drivers.html
在以上①②③個東西都安裝好之後,配置 CUDA 環境,編輯 ~/.bash_profile 檔案,開啟終端:
open - e .bash_profile |
然後在彈出的檔案中新增:
export CUDA_HOME = / usr / local / cuda export DYLD_LIBRARY_PATH = "$CUDA_HOME/lib:$CUDA_HOME/extras/CUPTI/lib" export LD_LIBRARY_PATH = $DYLD_LIBRARY_PATH export PATH = $DYLD_LIBRARY_PATH:$PATH export flags = "--config=cuda --config=opt" |
執行命令重啟bash_profile
$ . ~ / .bash_profile |
檢測CUDA能否正常執行:
$ cd / usr / local / cuda / samples $ sudo make - C 1_Utlities / deviceQuery $ . / bin / x86_64 / darwin / release / deviceQuery |
如果最後顯示 Result = PASS,那麼CUDA就工作正常
隨後再安裝CUDNN
下好後直接把cudnn-8.0-osx-x64-v6.0.tar壓縮包丟到這下面,這是在安裝CUDA的時候他就幫你建好的一個路徑。
/Developer
然後解壓它。
sudo tar xvf cudnn - 8.0 - osx - x64 - v6. 0.tar |
官方解壓出來叫做cuda應該意思就是讓你放到cuda這個資料夾下和cuda自己的檔案合併。
在瞭解cuda的前情後,就知道怎麼裝這個cudnn了。
sudo mv include / cudnn.h / Developer / NVIDIA / CUDA - 8.0 / include / sudo mv lib / libcudnn * / Developer / NVIDIA / CUDA - 8.0 / lib ln - s / Developer / NVIDIA / cudnn / include / * / usr / local / cuda / include / ln - s / Developer / NVIDIA / cudnn / lib / * / usr / local / cuda / lib / |
至此CUDA部分安裝結束。
3.Tensorflow_gpu安裝
3.1 開啟終端,輸入以下命令建立環境:
$ conda create - n tensorflow_gpu python = 2.7 |
3.2 啟用環境:
$ source activate tensorflow_gpu |
3.3 安裝tensorflow:
安裝tensorflow的時候特別的需要小心,因為tensorflow團隊宣佈停止支援1.2以後mac 版的tensorflow了。
如果你直接用pip install 的方法的話,那麼應該會裝成1.1版的tensorflow:
1 | pip install tensorflow - gpu |
我來詳細的介紹一下安裝tensorflow-gpu 1.4版的步驟:
你可以選擇自己從原始碼編譯tensorflow進行安裝,或者使用別人編譯好的進行安裝。
筆者這裡提供一個環境為python=2.7,CUDA=8.0,CUDNN=6,Compute Capability=3.0,3.5,5.2,6.1,tensorflow=1.4.0的版本給大家。如果你裝的版本和筆者在上文中提到的一樣的話,那麼下載下來這個包之後,
(連結:https://pan.baidu.com/s/1hsgY5Kg 密碼:suq7)
cat
到下載目錄下,
然後直接
pip install tensorflow - 1.4 . 0 - cp27 - cp27m - macosx_10_12_intel.whl |
即可
其中cp27代表的是python的版本為2.7版的,如果是cp35,cp36,那麼就代表是python3.5或者python3.6的;macosx_10_12代表的是作業系統的版本號
3.4 測試tensorflow
執行python環境
執行如下測試指令碼:
import tensorflow as tf hello = tf.constant( 'Hello, TensorFlow!' ) sess = tf.Session() print (sess.run(hello)) |
3 . 測試結果:
2017 - 12 - 14 23 : 26 : 26.352229 : I tensorflow / core / platform / cpu_feature_guard.cc: 137 ] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4. 2 AVX AVX2 FMA 2017 - 12 - 14 23 : 26 : 27.049231 : I tensorflow / stream_executor / cuda / cuda_gpu_executor.cc: 859 ] OS X does not support NUMA - returning NUMA node zero 2017 - 12 - 14 23 : 26 : 27.050014 : I tensorflow / core / common_runtime / gpu / gpu_device.cc: 1062 ] Found device 0 with properties: name: GeForce GT 750M major: 3 minor: 0 memoryClockRate(GHz): 0.9255 pciBusID: 0000 : 01 : 00.0 totalMemory: 2.00GiB freeMemory: 1.71GiB 2017 - 12 - 14 23 : 26 : 27.050043 : I tensorflow / core / common_runtime / gpu / gpu_device.cc: 1152 ] Creating TensorFlow device ( / device:GPU: 0 ) - > (device: 0 , name: GeForce GT 750M , pci bus id : 0000 : 01 : 00.0 , compute capability: 3.0 ) Hello, TensorFlow! |