-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I dont know what need to be mergedMerge branch 'master' of github.com…
…:Frank-Xu-Huaze/Medium
- Loading branch information
Showing
6 changed files
with
466 additions
and
0 deletions.
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
PlaidML_CNN/PlaidML_Fashion_mnist-NON-experimental-Plaid_ML-config.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Importing PlaidML. Make sure you follow this order\n", | ||
"import plaidml.keras\n", | ||
"plaidml.keras.install_backend()\n", | ||
"import os\n", | ||
"os.environ[\"KERAS_BACKEND\"] = \"plaidml.keras.backend\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import keras\n", | ||
"from keras.models import Sequential\n", | ||
"from keras.layers import Dense, Dropout, Flatten\n", | ||
"from keras.layers import Conv2D, MaxPooling2D\n", | ||
"from keras import backend as K\n", | ||
"\n", | ||
"# Download fashion dataset from Keras\n", | ||
"fashion_mnist = keras.datasets.fashion_mnist\n", | ||
"(x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data()\n", | ||
"\n", | ||
"# Reshape and normalize the data\n", | ||
"x_train = x_train.astype('float32').reshape(60000,28,28,1) / 255\n", | ||
"x_test = x_test.astype('float32').reshape(10000,28,28,1) / 255" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"INFO:plaidml:Opening device \"metal_amd_radeon_pro_vega_ii_duo.1\"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Build a CNN model. You should see \"INFO:plaidml:Opening device xxx\" after you run this chunk\n", | ||
"model = keras.Sequential()\n", | ||
"model.add(keras.layers.Conv2D(filters=64, kernel_size=2, padding='same', activation='relu', input_shape=(28,28,1))) \n", | ||
"model.add(keras.layers.MaxPooling2D(pool_size=2))\n", | ||
"model.add(keras.layers.Dropout(0.3))\n", | ||
"model.add(keras.layers.Conv2D(filters=32, kernel_size=2, padding='same', activation='relu'))\n", | ||
"model.add(keras.layers.MaxPooling2D(pool_size=2))\n", | ||
"model.add(keras.layers.Dropout(0.3))\n", | ||
"model.add(keras.layers.Flatten())\n", | ||
"model.add(keras.layers.Dense(256, activation='relu'))\n", | ||
"model.add(keras.layers.Dropout(0.5))\n", | ||
"model.add(keras.layers.Dense(10, activation='softmax'))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Compile the model\n", | ||
"model.compile(optimizer='adam',\n", | ||
" loss=keras.losses.sparse_categorical_crossentropy,\n", | ||
" metrics=['accuracy'])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Epoch 1/10\n", | ||
"60000/60000 [==============================] - 8s 129us/step - loss: 0.5875 - acc: 0.7845\n", | ||
"Epoch 2/10\n", | ||
"60000/60000 [==============================] - 7s 116us/step - loss: 0.4101 - acc: 0.8508\n", | ||
"Epoch 3/10\n", | ||
"60000/60000 [==============================] - 7s 117us/step - loss: 0.3624 - acc: 0.8694\n", | ||
"Epoch 4/10\n", | ||
"60000/60000 [==============================] - 7s 116us/step - loss: 0.3353 - acc: 0.8763\n", | ||
"Epoch 5/10\n", | ||
"60000/60000 [==============================] - 7s 116us/step - loss: 0.3160 - acc: 0.8847\n", | ||
"Epoch 6/10\n", | ||
"60000/60000 [==============================] - 7s 116us/step - loss: 0.3002 - acc: 0.8887\n", | ||
"Epoch 7/10\n", | ||
"60000/60000 [==============================] - 7s 117us/step - loss: 0.2891 - acc: 0.8938\n", | ||
"Epoch 8/10\n", | ||
"60000/60000 [==============================] - 7s 116us/step - loss: 0.2801 - acc: 0.8972\n", | ||
"Epoch 9/10\n", | ||
"60000/60000 [==============================] - 7s 116us/step - loss: 0.2720 - acc: 0.8999\n", | ||
"Epoch 10/10\n", | ||
"60000/60000 [==============================] - 7s 116us/step - loss: 0.2639 - acc: 0.9022\n", | ||
"\n", | ||
" Test accuracy: 0.9108\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Fit the model on training set\n", | ||
"model.fit(x_train, y_train,\n", | ||
" batch_size=64,\n", | ||
" epochs=10)\n", | ||
"\n", | ||
"# Evaluate the model on test set\n", | ||
"score = model.evaluate(x_test, y_test, verbose=0)\n", | ||
"# Print test accuracy\n", | ||
"print('\\n', 'Test accuracy:', score[1])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
146 changes: 146 additions & 0 deletions
146
PlaidML_CNN/PlaidML_Fashion_mnist-experimental-Plaid_ML-config.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Importing PlaidML. Make sure you follow this order\n", | ||
"import plaidml.keras\n", | ||
"plaidml.keras.install_backend()\n", | ||
"import os\n", | ||
"os.environ[\"KERAS_BACKEND\"] = \"plaidml.keras.backend\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import keras\n", | ||
"from keras.models import Sequential\n", | ||
"from keras.layers import Dense, Dropout, Flatten\n", | ||
"from keras.layers import Conv2D, MaxPooling2D\n", | ||
"from keras import backend as K\n", | ||
"\n", | ||
"# Download fashion dataset from Keras\n", | ||
"fashion_mnist = keras.datasets.fashion_mnist\n", | ||
"(x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data()\n", | ||
"\n", | ||
"# Reshape and normalize the data\n", | ||
"x_train = x_train.astype('float32').reshape(60000,28,28,1) / 255\n", | ||
"x_test = x_test.astype('float32').reshape(10000,28,28,1) / 255" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"INFO:plaidml:Opening device \"opencl_amd_radeon_pro_vega_ii_duo_compute_engine.1\"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Build a CNN model. You should see \"INFO:plaidml:Opening device xxx\" after you run this chunk\n", | ||
"model = keras.Sequential()\n", | ||
"model.add(keras.layers.Conv2D(filters=64, kernel_size=2, padding='same', activation='relu', input_shape=(28,28,1))) \n", | ||
"model.add(keras.layers.MaxPooling2D(pool_size=2))\n", | ||
"model.add(keras.layers.Dropout(0.3))\n", | ||
"model.add(keras.layers.Conv2D(filters=32, kernel_size=2, padding='same', activation='relu'))\n", | ||
"model.add(keras.layers.MaxPooling2D(pool_size=2))\n", | ||
"model.add(keras.layers.Dropout(0.3))\n", | ||
"model.add(keras.layers.Flatten())\n", | ||
"model.add(keras.layers.Dense(256, activation='relu'))\n", | ||
"model.add(keras.layers.Dropout(0.5))\n", | ||
"model.add(keras.layers.Dense(10, activation='softmax'))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Compile the model\n", | ||
"model.compile(optimizer='adam',\n", | ||
" loss=keras.losses.sparse_categorical_crossentropy,\n", | ||
" metrics=['accuracy'])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Epoch 1/10\n", | ||
"60000/60000 [==============================] - 18s 294us/step - loss: nan - acc: 0.0999\n", | ||
"Epoch 2/10\n", | ||
"60000/60000 [==============================] - 10s 167us/step - loss: nan - acc: 0.1000\n", | ||
"Epoch 3/10\n", | ||
"60000/60000 [==============================] - 10s 167us/step - loss: nan - acc: 0.1000\n", | ||
"Epoch 4/10\n", | ||
"60000/60000 [==============================] - 10s 167us/step - loss: nan - acc: 0.1000\n", | ||
"Epoch 5/10\n", | ||
"60000/60000 [==============================] - 10s 168us/step - loss: nan - acc: 0.1000\n", | ||
"Epoch 6/10\n", | ||
"60000/60000 [==============================] - 10s 168us/step - loss: nan - acc: 0.1000\n", | ||
"Epoch 7/10\n", | ||
"60000/60000 [==============================] - 10s 168us/step - loss: nan - acc: 0.1000\n", | ||
"Epoch 8/10\n", | ||
"34624/60000 [================>.............] - ETA: 4s - loss: nan - acc: 0.0997" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Fit the model on training set\n", | ||
"model.fit(x_train, y_train,\n", | ||
" batch_size=64,\n", | ||
" epochs=10)\n", | ||
"\n", | ||
"# Evaluate the model on test set\n", | ||
"score = model.evaluate(x_test, y_test, verbose=0)\n", | ||
"# Print test accuracy\n", | ||
"print('\\n', 'Test accuracy:', score[1])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# README.md for PlaidML_Fashion_mnist.ipydb | ||
1. Create a python virtual environment using `venv` or `virtualenv`. For example, `virtualenv --python=python3 venv_name` | ||
2. From within the PlaidML_CNN directory, run `pip install -r requirements.txt` | ||
3. Setup PlaidML: You have two choices, experimental hardward and non-experimental hardware/software support. Notes for each I below, and I saved the results in the notebooks because, on my new Mac Pro, the experimental configuration was 0.1, compared to 0.9x for the non-experimental hardware configuration. | ||
- [Experimental Configuration Notes](./plaidML_experimental.md) | ||
- [Non-Experimental Configuration Notes](./plaidML_non-experimental.md) | ||
4. Run `jupyter lab` | ||
5. Have fun! | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Notes on Experimental PlaidML Hardware Config | ||
|
||
Run this command to setup PlaidML: | ||
|
||
``` | ||
plaidml-setup | ||
``` | ||
You should see this output: | ||
``` | ||
PlaidML Setup (0.7.0) | ||
Thanks for using PlaidML! | ||
The feedback we have received from our users indicates an ever-increasing need | ||
for performance, programmability, and portability. During the past few months, | ||
we have been restructuring PlaidML to address those needs. To make all the | ||
changes we need to make while supporting our current user base, all development | ||
of PlaidML has moved to a branch — plaidml-v1. We will continue to maintain and | ||
support the master branch of PlaidML and the stable 0.7.0 release. | ||
Read more here: https://github.com/plaidml/plaidml | ||
Some Notes: | ||
* Bugs and other issues: https://github.com/plaidml/plaidml/issues | ||
* Questions: https://stackoverflow.com/questions/tagged/plaidml | ||
* Say hello: https://groups.google.com/forum/#!forum/plaidml-dev | ||
* PlaidML is licensed under the Apache License 2.0 | ||
Default Config Devices: | ||
llvm_cpu.0 : CPU (via LLVM) | ||
metal_amd_radeon_pro_vega_ii_duo.1 : AMD Radeon Pro Vega II Duo (Metal) | ||
metal_amd_radeon_pro_vega_ii_duo.0 : AMD Radeon Pro Vega II Duo (Metal) | ||
Experimental Config Devices: | ||
llvm_cpu.0 : CPU (via LLVM) | ||
opencl_amd_radeon_pro_vega_ii_duo_compute_engine.1 : AMD AMD Radeon Pro Vega II Duo Compute Engine (OpenCL) | ||
opencl_amd_radeon_pro_vega_ii_duo_compute_engine.0 : AMD AMD Radeon Pro Vega II Duo Compute Engine (OpenCL) | ||
metal_amd_radeon_pro_vega_ii_duo.1 : AMD Radeon Pro Vega II Duo (Metal) | ||
metal_amd_radeon_pro_vega_ii_duo.0 : AMD Radeon Pro Vega II Duo (Metal) | ||
Using experimental devices can cause poor performance, crashes, and other nastiness. | ||
``` | ||
I was offered this choice, and went **bold** with a `y` | ||
``` | ||
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 : llvm_cpu.0 | ||
2 : opencl_amd_radeon_pro_vega_ii_duo_compute_engine.1 | ||
3 : opencl_amd_radeon_pro_vega_ii_duo_compute_engine.0 | ||
4 : metal_amd_radeon_pro_vega_ii_duo.1 | ||
5 : metal_amd_radeon_pro_vega_ii_duo.0 | ||
``` | ||
|
||
You can see I chose one of the experimental devices on my new Mac Pro `CheeseGrater` | ||
|
||
``` | ||
Default device? (1,2,3,4,5)[1]:2 | ||
Selected device: | ||
opencl_amd_radeon_pro_vega_ii_duo_compute_engine.1 | ||
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 /Users/gogginsS/.plaidml? (y,n)[y]:y | ||
Success! | ||
``` |
Oops, something went wrong.