前言 自从Ryzen发布以后,AMD的CPU部门慢慢赶上了Intel。但A家的显卡这几年却还是慢悠悠的发展着,老黄在游戏卡领域叱咤风云,还借着深度学习的热潮,又在云计算这种专业领域也分到了不少蛋糕。诸如TensorFlow、Pytorch等深度学习框架均只支持N卡,作为AMD精神股东的我一直对此表示羡慕。今天终于可以不用羡慕了,因为我知道了一款支持AMD显卡的深度学习框架—>PlaidML! 地址为(https://github.com/plaidml/plaidml )
更让人激动的是,这款框架可以作为Keras的后端来使用,也就是说我之前略知一二的Keras知识可以直接用了。
安装 [Windows] 直接上pip即可
1 pip install plaidml-keras
安装好以后,还需要使用plaidml-setup命令进行后续安装工作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 C:\Users\xxx>plaidml-setup PlaidML Setup (0.6.3) Thanks for using PlaidML! Some Notes: * Bugs and other issues: https://github.com/plaidml/plaidml * Questions: https://stackoverflow.com/questions/tagged/plaidml * Say hello: https://groups.google.com/forum/ * PlaidML is licensed under the Apache License 2.0 Default Config Devices: opencl_amd_ellesmere.0 : Advanced Micro Devices, Inc. Ellesmere (OpenCL) Experimental Config Devices: opencl_amd_ellesmere.0 : Advanced Micro Devices, Inc. Ellesmere (OpenCL) llvm_cpu.0 : CPU (LLVM) Using experimental devices can cause poor performance, crashes, and other nastiness. Enable experimental device support? (y,n)[n]:y Multiple devices detected (You can override by setting PLAIDML_DEVICE_IDS). Please choose a default device: 1 : opencl_amd_ellesmere.0 2 : llvm_cpu.0 Default device? (1,2)[1]:1 Selected device: opencl_amd_ellesmere.0 Almost done . Multiplying some matrices... Tile code: function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); } Whew. That worked. Save settings to C:\Users\xxx\.plaidml? (y,n)[y]:y Success!
在执行过程中,会让用户选择默认硬件,选择gpu即可。
使用 手动指定keras的后端为plaidml
在import keras前,设置环境变量KERAS_BACKEND为plaidml.keras.backend
1 2 os.environ["KERAS_BACKEND" ] = "plaidml.keras.backend" import keras
也可以通过修改~/.keras文件夹内keras.json进行一劳永逸式修改
1 2 3 4 5 6 { "floatx" : "float32" , "epsilon" : 1e-07 , "backend" : "plaidml.keras.backend" , "image_data_format" : "channels_last" }
测试 那就让被誉为深度学习届的Hello World项目—MNIST手写数字集来做测试吧,哈哈。
测试代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import kerasfrom keras.datasets import mnistfrom keras.models import Sequentialfrom keras.layers import Densefrom keras.optimizers import SGD(x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train.reshape(60000 , 784 ) x_test = x_test.reshape(10000 , 784 ) x_train = x_train/255 x_test = x_test/255 y_train = keras.utils.to_categorical(y_train, 10 ) y_test = keras.utils.to_categorical(y_test, 10 ) model = Sequential() model.add(Dense(256 , activation='relu' ,input_shape=(784 ,))) model.add(Dense(256 , activation='relu' )) model.add(Dense(10 , activation='softmax' )) model.summary() model.compile (optimizer=SGD(),loss="categorical_crossentropy" ,metrics=['accuracy' ]) model.fit(x_train,y_train,batch_size=64 ,epochs=5 ,validation_data=(x_test,y_test))
运行结果 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 G:\>python mnist.py Using plaidml.keras.backend backend. INFO:plaidml:Opening device "opencl_amd_ellesmere.0" _________________________________________________________________ Layer (type ) Output Shape Param ================================================================= dense_1 (Dense) (None, 256) 200960 _________________________________________________________________ dense_2 (Dense) (None, 256) 65792 _________________________________________________________________ dense_3 (Dense) (None, 10) 2570 ================================================================= Total params: 269,322 Trainable params: 269,322 Non-trainable params: 0 _________________________________________________________________ Train on 60000 samples, validate on 10000 samples Epoch 1/5 60000/60000 [==============================] - 20s 341us/step - loss: 0.8436 - acc: 0.7939 - val_loss: 0.3978 - val_acc: 0.8943 Epoch 2/5 60000/60000 [==============================] - 13s 213us/step - loss: 0.3603 - acc: 0.8990 - val_loss: 0.3082 - val_acc: 0.9136 Epoch 3/5 60000/60000 [==============================] - 11s 192us/step - loss: 0.3015 - acc: 0.9138 - val_loss: 0.2727 - val_acc: 0.9235 Epoch 4/5 60000/60000 [==============================] - 10s 166us/step - loss: 0.2677 - acc: 0.9232 - val_loss: 0.2466 - val_acc: 0.9312 Epoch 5/5 60000/60000 [==============================] - 10s 165us/step - loss: 0.2431 - acc: 0.9307 - val_loss: 0.2264 - val_acc: 0.9359
附: 本来我是打算在MacOS上安装的,但是遇到了找不到experimental.json的问题。但是等我解决了找不到experimental.json的问题后,又开始报找不到libplaidml.dylib错误,在网上几乎找不到什么资料。唯一找到的资料居然是重装MacOS,这个代价对我来说太大了,于是就转到了Windows下安装,然后一帆风顺。 参考资料
阅读量: