Skip to content

Commit

Permalink
Add batch dim for TF 2.1 (#131) (#132)
Browse files Browse the repository at this point in the history
* Add batch dim for TF 2.1

* Clean up

* Enable native keras support for notebook

Co-authored-by: Tuan Nguyen <tuan@neuralmagic.com>
  • Loading branch information
bfineran and natuan committed Mar 31, 2021
1 parent 0e5e9d3 commit 8890701
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions notebooks/keras_classification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"## Step 1 - Requirements\n",
"To run this notebook, you will need the following packages already installed:\n",
"* SparseML and SparseZoo;\n",
"* Tensorflow >=2.2, which includes Keras and TensorBoard;\n",
"* Tensorflow >=2.1, which includes Keras and TensorBoard;\n",
"* keras2onnx.\n",
"\n",
"You can install any package that is not already present via `pip`."
Expand Down Expand Up @@ -60,7 +60,7 @@
"outputs": [],
"source": [
"import os\n",
"from tensorflow import keras\n",
"from sparseml.keras.utils import keras\n",
"from sparsezoo.models import Zoo\n",
"\n",
"# Root directory for the notebook artifacts\n",
Expand Down Expand Up @@ -122,6 +122,8 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy\n",
"\n",
"# Number of classes\n",
"num_classes = 10\n",
"\n",
Expand All @@ -132,6 +134,10 @@
"x_train = x_train.astype('float32') / 255\n",
"x_test = x_test.astype('float32') / 255\n",
"\n",
"# Add batch dimension (for older TF versions)\n",
"x_train = numpy.expand_dims(x_train, -1)\n",
"x_test = numpy.expand_dims(x_test, -1)\n",
"\n",
"y_train = keras.utils.to_categorical(y_train, num_classes)\n",
"y_test = keras.utils.to_categorical(y_test, num_classes)\n",
"\n",
Expand Down Expand Up @@ -418,9 +424,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python (pypi_sparseml)",
"display_name": "Python (keras_pruning)",
"language": "python",
"name": "pypi_sparseml"
"name": "keras_pruning"
},
"language_info": {
"codemirror_mode": {
Expand Down

0 comments on commit 8890701

Please sign in to comment.