mirror of
https://github.com/ArthurDanjou/handson-ml3.git
synced 2026-01-14 12:14:36 +01:00
Update notebook to latest Colab library versions
This commit is contained in:
@@ -1147,6 +1147,13 @@
|
|||||||
"model = tf.keras.applications.ResNet50(weights=\"imagenet\")"
|
"model = tf.keras.applications.ResNet50(weights=\"imagenet\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"**Warning**: The expression `load_sample_images()[\"images\"]` returns a Python list of images. However, in the latest versions, Keras does not accept Python lists anymore, so we must convert this list to a tensor. We can do this using `tf.constant()`, but here I have used `K.constant()` instead: it simply calls `tf.constant()` if you are using TensorFlow as the backend (as is the case here), but if you ever decide to use JAX or PyTorch as the backend instead, `K.constant()` will call the appropriate function from the chosen backend."
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 35,
|
"execution_count": 35,
|
||||||
@@ -1155,7 +1162,8 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"images = load_sample_images()[\"images\"]\n",
|
"K = tf.keras.backend\n",
|
||||||
|
"images = K.constant(load_sample_images()[\"images\"])\n",
|
||||||
"images_resized = tf.keras.layers.Resizing(height=224, width=224,\n",
|
"images_resized = tf.keras.layers.Resizing(height=224, width=224,\n",
|
||||||
" crop_to_aspect_ratio=True)(images)"
|
" crop_to_aspect_ratio=True)(images)"
|
||||||
]
|
]
|
||||||
@@ -1852,7 +1860,7 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"**Note**: the code below used to reuse the optimizer from the previous model. This was fine in earlier versions of TensorFlow, but in more recent versions it can cause some issues, so I added a line to create a new optimizer here."
|
"**Note**: the code below used to reuse the optimizer from the previous model. This was fine in earlier versions of TensorFlow, but in more recent versions it can cause some issues, so I added a line to create a new optimizer here. Also, recent versions of Keras expect one metric per output, so I added the `\"mse\"` metric to the list."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -1874,7 +1882,7 @@
|
|||||||
"optimizer = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9) # added this line\n",
|
"optimizer = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9) # added this line\n",
|
||||||
"model.compile(loss=[\"sparse_categorical_crossentropy\", \"mse\"],\n",
|
"model.compile(loss=[\"sparse_categorical_crossentropy\", \"mse\"],\n",
|
||||||
" loss_weights=[0.8, 0.2], # depends on what you care most about\n",
|
" loss_weights=[0.8, 0.2], # depends on what you care most about\n",
|
||||||
" optimizer=optimizer, metrics=[\"accuracy\"])"
|
" optimizer=optimizer, metrics=[\"accuracy\", \"mse\"])"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -2215,7 +2223,7 @@
|
|||||||
"provenance": []
|
"provenance": []
|
||||||
},
|
},
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 3 (ipykernel)",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
@@ -2229,7 +2237,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.10.13"
|
"version": "3.9.10"
|
||||||
},
|
},
|
||||||
"nav_menu": {},
|
"nav_menu": {},
|
||||||
"toc": {
|
"toc": {
|
||||||
|
|||||||
Reference in New Issue
Block a user