赞
踩
在之前的文章中,笔者介绍了在Mac下安装Tensorflow及开发环境搭建。但是感觉用CPU跑机器学习还是太慢了,所以便研究研究如何使用GPU来跑。
软件环境:
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的方法之外,还可以通过外置显卡的方法跑机器学习。
首先需要说明的是:CUDA Driver与NVIDIA GPU Driver的版本必须一致,才能让CUDA找到显卡。
在mac上安装CUDA最麻烦的事情就是版本匹配问题,这个版本匹配指的是MacOS版本和CUDA Driver、GPU Driver的版本都要匹配。这个网站提供了详细的版本对照:http://www.macvidcards.com/drivers.html
1.先根据macOS 10.12.6版本,在这个网站http://www.macvidcards.com/drivers.html找到相应的GPU Driver-378.05.05.25f03(笔者的是这个版本),下载安装。
(链接:https://pan.baidu.com/s/1dFB6RzN 密码:rbcv)
2.再进入CUDA Driver页面http://www.nvidia.com/object/mac-driver-archive.html,找到和GPU driver匹配的版本(cudadriver-8.0.90-macos笔者下的是这个),然后下载安装。
3.再进入CUDA Toolkit页面https://developer.nvidia.com/cuda-toolkit-archive,下载、安装CUDA Toolkit(笔者安装的是cuda_8.0.61_mac)
(链接:https://pan.baidu.com/s/1bppWjt9 密码:pthw)
4.CUDNN,笔者这里下的是 cudnn-8.0-osx-x64-v6.0 https://developer.nvidia.com/rdp/cudnn-download
(链接:https://pan.baidu.com/s/1c2Nee1E 密码:rpgq)
在以上①②③个东西都安装好之后,配置 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部分安装结束。
附上一张用CUDA-Z查看的图:
(链接:https://pan.baidu.com/s/1slxBoPf 密码:d43e)
这里依然是基于之前文章中提到的Anaconda环境进行安装,不知道的同学可以看这里http://blog.csdn.net/wz22881916/article/details/78495543
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!
|
https://www.tensorflow.org/install/install_mac#the_url_of_the_tensorflow_python_package
http://blog.csdn.net/eengel/article/details/73245079
http://blog.csdn.net/eengel/article/details/73291190?utm_source=itdadao&utm_medium=referral
https://zhuanlan.zhihu.com/p/25045282
https://gist.github.com/smitshilu/53cf9ff0fd6cdb64cca69a7e2827ed0f
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。