Skip to content

Commit 503b0f4

Browse files
committed
Remove set_backend() from all notebooks
1 parent 54335cf commit 503b0f4

16 files changed

+436
-315
lines changed

chapter02_mathematical-building-blocks.ipynb

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,31 @@
3232
"os.environ[\"KERAS_BACKEND\"] = \"jax\""
3333
]
3434
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 0,
38+
"metadata": {
39+
"cellView": "form",
40+
"colab_type": "code"
41+
},
42+
"outputs": [],
43+
"source": [
44+
"# @title\n",
45+
"import os\n",
46+
"from IPython.core.magic import register_cell_magic\n",
47+
"\n",
48+
"@register_cell_magic\n",
49+
"def backend(line, cell):\n",
50+
" current, required = os.environ.get(\"KERAS_BACKEND\", \"\"), line.split()[-1]\n",
51+
" if current == required:\n",
52+
" get_ipython().run_cell(cell)\n",
53+
" else:\n",
54+
" print(\n",
55+
" f\"This cell requires the {required} backend. To run it, change KERAS_BACKEND to \"\n",
56+
" f\"\\\"{required}\\\" at the top of the notebook, restart the runtime, and rerun the notebook.\"\n",
57+
" )"
58+
]
59+
},
3560
{
3661
"cell_type": "markdown",
3762
"metadata": {
@@ -1113,17 +1138,6 @@
11131138
"##### A simple Dense class"
11141139
]
11151140
},
1116-
{
1117-
"cell_type": "code",
1118-
"execution_count": 0,
1119-
"metadata": {
1120-
"colab_type": "code"
1121-
},
1122-
"outputs": [],
1123-
"source": [
1124-
"keras.config.set_backend(\"tensorflow\")"
1125-
]
1126-
},
11271141
{
11281142
"cell_type": "code",
11291143
"execution_count": 0,
@@ -1308,6 +1322,7 @@
13081322
},
13091323
"outputs": [],
13101324
"source": [
1325+
"%%backend tensorflow\n",
13111326
"import tensorflow as tf\n",
13121327
"\n",
13131328
"x = tf.zeros(shape=())\n",
@@ -1324,6 +1339,7 @@
13241339
},
13251340
"outputs": [],
13261341
"source": [
1342+
"%%backend tensorflow\n",
13271343
"def one_training_step(model, images_batch, labels_batch):\n",
13281344
" with tf.GradientTape() as tape:\n",
13291345
" predictions = model(images_batch)\n",
@@ -1351,6 +1367,7 @@
13511367
},
13521368
"outputs": [],
13531369
"source": [
1370+
"%%backend tensorflow\n",
13541371
"def fit(model, images, labels, epochs, batch_size=128):\n",
13551372
" for epoch_counter in range(epochs):\n",
13561373
" print(f\"Epoch {epoch_counter}\")\n",
@@ -1370,6 +1387,7 @@
13701387
},
13711388
"outputs": [],
13721389
"source": [
1390+
"%%backend tensorflow\n",
13731391
"from keras.datasets import mnist\n",
13741392
"\n",
13751393
"(train_images, train_labels), (test_images, test_labels) = mnist.load_data()\n",
@@ -1399,6 +1417,7 @@
13991417
},
14001418
"outputs": [],
14011419
"source": [
1420+
"%%backend tensorflow\n",
14021421
"predictions = model(test_images)\n",
14031422
"predicted_labels = ops.argmax(predictions, axis=1)\n",
14041423
"matches = predicted_labels == test_labels\n",

chapter03_introduction-to-ml-frameworks.ipynb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,31 @@
3232
"os.environ[\"KERAS_BACKEND\"] = \"jax\""
3333
]
3434
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 0,
38+
"metadata": {
39+
"cellView": "form",
40+
"colab_type": "code"
41+
},
42+
"outputs": [],
43+
"source": [
44+
"# @title\n",
45+
"import os\n",
46+
"from IPython.core.magic import register_cell_magic\n",
47+
"\n",
48+
"@register_cell_magic\n",
49+
"def backend(line, cell):\n",
50+
" current, required = os.environ.get(\"KERAS_BACKEND\", \"\"), line.split()[-1]\n",
51+
" if current == required:\n",
52+
" get_ipython().run_cell(cell)\n",
53+
" else:\n",
54+
" print(\n",
55+
" f\"This cell requires the {required} backend. To run it, change KERAS_BACKEND to \"\n",
56+
" f\"\\\"{required}\\\" at the top of the notebook, restart the runtime, and rerun the notebook.\"\n",
57+
" )"
58+
]
59+
},
3560
{
3661
"cell_type": "markdown",
3762
"metadata": {

chapter04_classification-and-regression.ipynb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,31 @@
3232
"os.environ[\"KERAS_BACKEND\"] = \"jax\""
3333
]
3434
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 0,
38+
"metadata": {
39+
"cellView": "form",
40+
"colab_type": "code"
41+
},
42+
"outputs": [],
43+
"source": [
44+
"# @title\n",
45+
"import os\n",
46+
"from IPython.core.magic import register_cell_magic\n",
47+
"\n",
48+
"@register_cell_magic\n",
49+
"def backend(line, cell):\n",
50+
" current, required = os.environ.get(\"KERAS_BACKEND\", \"\"), line.split()[-1]\n",
51+
" if current == required:\n",
52+
" get_ipython().run_cell(cell)\n",
53+
" else:\n",
54+
" print(\n",
55+
" f\"This cell requires the {required} backend. To run it, change KERAS_BACKEND to \"\n",
56+
" f\"\\\"{required}\\\" at the top of the notebook, restart the runtime, and rerun the notebook.\"\n",
57+
" )"
58+
]
59+
},
3560
{
3661
"cell_type": "markdown",
3762
"metadata": {

chapter05_fundamentals-of-ml.ipynb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,31 @@
3232
"os.environ[\"KERAS_BACKEND\"] = \"jax\""
3333
]
3434
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 0,
38+
"metadata": {
39+
"cellView": "form",
40+
"colab_type": "code"
41+
},
42+
"outputs": [],
43+
"source": [
44+
"# @title\n",
45+
"import os\n",
46+
"from IPython.core.magic import register_cell_magic\n",
47+
"\n",
48+
"@register_cell_magic\n",
49+
"def backend(line, cell):\n",
50+
" current, required = os.environ.get(\"KERAS_BACKEND\", \"\"), line.split()[-1]\n",
51+
" if current == required:\n",
52+
" get_ipython().run_cell(cell)\n",
53+
" else:\n",
54+
" print(\n",
55+
" f\"This cell requires the {required} backend. To run it, change KERAS_BACKEND to \"\n",
56+
" f\"\\\"{required}\\\" at the top of the notebook, restart the runtime, and rerun the notebook.\"\n",
57+
" )"
58+
]
59+
},
3560
{
3661
"cell_type": "markdown",
3762
"metadata": {

0 commit comments

Comments
 (0)