PlaidML项目地址:https://github.com/plaidml/plaidml
目前 PlaidML 已经支持 Keras、ONNX 和 nGraph 等工具,直接用 Keras 建个模,MacBook 轻轻松松调用 GPU。
通过这款名为 PlaidML 的工具,不论英伟达、AMD 还是英特尔显卡都可以轻松搞定深度学习训练了。
下面进入正题👇
如何用自己笔记本电脑的 GPU 运行一个简单的 CNN(建议在虚拟环境中操作)
- Install PlaidML with Keras:pip install plaidml-keras
记住一点,标准 TensorFlow 框架下的 Keras 无法使用 PlaidML,需要安装 PlaidML 定制的 Keras。 - Now setup PlaidML to use the right device:plaidml-setup
- 我们首先会看到一个欢迎页面,并跳出一个问题,即是否要使用实验性设备。根据提示,允许实验设备会导致系统运行不佳、崩溃和其他故障。我们可以键入「n」或「nothing」,然后返回选择使用默认设备。
现在得到了自己选择的设备列表。以作者的电脑 Macbook Pro 15’2018 为例,设备列表如下:
(1)CPU
(2)英特尔 UHD Graphics 630 显卡
(3)AMD Radeon pro 560x 显卡
最后,键入「y」或「nothing」,返回保存设置。这样以来,我们就已安装所有设备,并且可以使用 GPU 来运行深度学习项目了。 - 按顺序运行以下代码,将 PlaidML 用作 Keras 后端,否则会默认使用 TensorFlow。
# Importing PlaidML. Make sure you follow this order
import plaidml.keras
plaidml.keras.install_backend()
import os
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
# then you can write your codes
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
if __name__ == "__main__":
pass