Skip to content

Update guides for Keras V3 Saving Compatibility #1424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion guides/customizing_what_happens_in_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"""

import tensorflow as tf
from tensorflow import keras
import keras

"""
## A first simple example
Expand Down
2 changes: 1 addition & 1 deletion guides/distributed_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"""

import tensorflow as tf
from tensorflow import keras
import keras

"""
## Single-host, multi-device synchronous training
Expand Down
11 changes: 7 additions & 4 deletions guides/functional_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Title: The Functional API
Author: [fchollet](https://twitter.com/fchollet)
Date created: 2019/03/01
Last modified: 2020/04/12
Last modified: 2020/07/10
Description: Complete guide to the functional API.
Accelerator: GPU
"""
Expand All @@ -12,8 +12,8 @@

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import keras
from keras import layers

"""
## Introduction
Expand Down Expand Up @@ -607,6 +607,7 @@ def call(self, inputs):
"""


@keras.saving.register_keras_serializable()
class CustomDense(layers.Layer):
def __init__(self, units=32):
super().__init__()
Expand Down Expand Up @@ -635,7 +636,7 @@ def get_config(self):
model = keras.Model(inputs, outputs)
config = model.get_config()

new_model = keras.Model.from_config(config, custom_objects={"CustomDense": CustomDense})
new_model = keras.Model.from_config(config)

"""
Optionally, implement the class method `from_config(cls, config)` which is used
Expand Down Expand Up @@ -776,6 +777,7 @@ def call(self, inputs):
model = keras.Model(inputs, outputs)


@keras.saving.register_keras_serializable()
class CustomRNN(layers.Layer):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -830,6 +832,7 @@ def call(self, inputs):
batch_size = 16


@keras.saving.register_keras_serializable()
class CustomRNN(layers.Layer):
def __init__(self):
super().__init__()
Expand Down
2 changes: 1 addition & 1 deletion guides/intro_to_keras_for_engineers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import numpy as np
import tensorflow as tf
from tensorflow import keras
import keras

"""
## Introduction
Expand Down
2 changes: 1 addition & 1 deletion guides/intro_to_keras_for_researchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""

import tensorflow as tf
from tensorflow import keras
import keras

"""
## Introduction
Expand Down
2 changes: 1 addition & 1 deletion guides/ipynb/customizing_what_happens_in_fit.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow import keras"
"import keras"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion guides/ipynb/distributed_training.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow import keras"
"import keras"
]
},
{
Expand Down
11 changes: 7 additions & 4 deletions guides/ipynb/functional_api.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"\n",
"**Author:** [fchollet](https://twitter.com/fchollet)<br>\n",
"**Date created:** 2019/03/01<br>\n",
"**Last modified:** 2020/04/12<br>\n",
"**Last modified:** 2020/07/10<br>\n",
"**Description:** Complete guide to the functional API."
]
},
Expand All @@ -33,8 +33,8 @@
"source": [
"import numpy as np\n",
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"from tensorflow.keras import layers"
"import keras\n",
"from keras import layers"
]
},
{
Expand Down Expand Up @@ -1064,6 +1064,7 @@
"outputs": [],
"source": [
"\n",
"@keras.saving.register_keras_serializable()\n",
"class CustomDense(layers.Layer):\n",
" def __init__(self, units=32):\n",
" super().__init__()\n",
Expand Down Expand Up @@ -1092,7 +1093,7 @@
"model = keras.Model(inputs, outputs)\n",
"config = model.get_config()\n",
"\n",
"new_model = keras.Model.from_config(config, custom_objects={\"CustomDense\": CustomDense})"
"new_model = keras.Model.from_config(config)"
]
},
{
Expand Down Expand Up @@ -1256,6 +1257,7 @@
"model = keras.Model(inputs, outputs)\n",
"\n",
"\n",
"@keras.saving.register_keras_serializable()\n",
"class CustomRNN(layers.Layer):\n",
" def __init__(self):\n",
" super().__init__()\n",
Expand Down Expand Up @@ -1324,6 +1326,7 @@
"batch_size = 16\n",
"\n",
"\n",
"@keras.saving.register_keras_serializable()\n",
"class CustomRNN(layers.Layer):\n",
" def __init__(self):\n",
" super().__init__()\n",
Expand Down
2 changes: 1 addition & 1 deletion guides/ipynb/intro_to_keras_for_engineers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"source": [
"import numpy as np\n",
"import tensorflow as tf\n",
"from tensorflow import keras"
"import keras"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion guides/ipynb/intro_to_keras_for_researchers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow import keras"
"import keras"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"\n",
"**Author:** [fchollet](https://twitter.com/fchollet)<br>\n",
"**Date created:** 2019/03/01<br>\n",
"**Last modified:** 2023/03/16<br>\n",
"**Last modified:** 2023/07/10<br>\n",
"**Description:** Complete guide to writing `Layer` and `Model` objects from scratch."
]
},
Expand All @@ -32,7 +32,7 @@
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow import keras"
"import keras"
]
},
{
Expand Down Expand Up @@ -820,9 +820,10 @@
},
"outputs": [],
"source": [
"from tensorflow.keras import layers\n",
"from keras import layers\n",
"\n",
"\n",
"@keras.saving.register_keras_serializable()\n",
"class Sampling(layers.Layer):\n",
" \"\"\"Uses (z_mean, z_log_var) to sample z, the vector encoding a digit.\"\"\"\n",
"\n",
Expand All @@ -834,6 +835,7 @@
" return z_mean + tf.exp(0.5 * z_log_var) * epsilon\n",
"\n",
"\n",
"@keras.saving.register_keras_serializable()\n",
"class Encoder(layers.Layer):\n",
" \"\"\"Maps MNIST digits to a triplet (z_mean, z_log_var, z).\"\"\"\n",
"\n",
Expand All @@ -852,6 +854,7 @@
" return z_mean, z_log_var, z\n",
"\n",
"\n",
"@keras.saving.register_keras_serializable()\n",
"class Decoder(layers.Layer):\n",
" \"\"\"Converts z, the encoded digit vector, back into a readable digit.\"\"\"\n",
"\n",
Expand All @@ -865,6 +868,7 @@
" return self.dense_output(x)\n",
"\n",
"\n",
"@keras.saving.register_keras_serializable()\n",
"class VariationalAutoEncoder(keras.Model):\n",
" \"\"\"Combines the encoder and decoder into an end-to-end model for training.\"\"\"\n",
"\n",
Expand Down
3 changes: 2 additions & 1 deletion guides/ipynb/preprocessing_layers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@
"source": [
"import numpy as np\n",
"import tensorflow as tf\n",
"from tensorflow.keras import layers\n",
"import keras\n",
"from keras import layers\n",
"\n",
"data = np.array(\n",
" [\n",
Expand Down
4 changes: 2 additions & 2 deletions guides/ipynb/sequential_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"from tensorflow.keras import layers"
"import keras\n",
"from keras import layers"
]
},
{
Expand Down
18 changes: 14 additions & 4 deletions guides/ipynb/training_with_built_in_methods.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"from tensorflow.keras import layers"
"import keras\n",
"from keras import layers"
]
},
{
Expand Down Expand Up @@ -454,6 +454,7 @@
"outputs": [],
"source": [
"\n",
"@keras.saving.register_keras_serializable()\n",
"class CustomMSE(keras.losses.Loss):\n",
" def __init__(self, regularization_factor=0.1, name=\"custom_mse\"):\n",
" super().__init__(name=name)\n",
Expand All @@ -464,6 +465,12 @@
" reg = tf.math.reduce_mean(tf.square(0.5 - y_pred), axis=-1)\n",
" return mse + reg * self.regularization_factor\n",
"\n",
" def get_config(self):\n",
" return {\n",
" \"regularization_factor\": self.regularization_factor,\n",
" \"name\": self.name,\n",
" }\n",
"\n",
"\n",
"model = get_uncompiled_model()\n",
"model.compile(optimizer=keras.optimizers.Adam(), loss=CustomMSE())\n",
Expand Down Expand Up @@ -507,6 +514,7 @@
"outputs": [],
"source": [
"\n",
"@keras.saving.register_keras_serializable()\n",
"class CategoricalTruePositives(keras.metrics.Metric):\n",
" def __init__(self, name=\"categorical_true_positives\", **kwargs):\n",
" super().__init__(name=name, **kwargs)\n",
Expand Down Expand Up @@ -567,6 +575,7 @@
"outputs": [],
"source": [
"\n",
"@keras.saving.register_keras_serializable()\n",
"class ActivityRegularizationLayer(layers.Layer):\n",
" def call(self, inputs):\n",
" self.add_loss(tf.reduce_sum(inputs) * 0.1)\n",
Expand Down Expand Up @@ -615,6 +624,7 @@
"outputs": [],
"source": [
"\n",
"@keras.saving.register_keras_serializable()\n",
"class LogisticEndpoint(keras.layers.Layer):\n",
" def __init__(self, name=None):\n",
" super().__init__(name=name)\n",
Expand Down Expand Up @@ -1550,7 +1560,7 @@
" # the current checkpoint if and only if\n",
" # the `val_loss` score has improved.\n",
" # The saved model name will include the current epoch.\n",
" filepath=\"mymodel_{epoch}.keras\",\n",
" filepath=\"mymodel_{epoch}\",\n",
" save_best_only=True, # Only save a model if `val_loss` has improved.\n",
" monitor=\"val_loss\",\n",
" verbose=1,\n",
Expand Down Expand Up @@ -1605,7 +1615,7 @@
" # This callback saves the model every 100 batches.\n",
" # We include the training loss in the saved model name.\n",
" keras.callbacks.ModelCheckpoint(\n",
" filepath=checkpoint_dir + \"/model-loss={loss:.2f}.keras\", save_freq=100\n",
" filepath=checkpoint_dir + \"/model-loss={loss:.2f}\", save_freq=100\n",
" )\n",
"]\n",
"model.fit(x_train, y_train, epochs=1, callbacks=callbacks)"
Expand Down
2 changes: 1 addition & 1 deletion guides/ipynb/transfer_learning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"source": [
"import numpy as np\n",
"import tensorflow as tf\n",
"from tensorflow import keras"
"import keras"
]
},
{
Expand Down
Loading