Skip to content

Commit edd6242

Browse files
nkovela1copybara-github
authored andcommitted
Updates all tensorflow.org tutorials with updated information for Keras v3 saving.
PiperOrigin-RevId: 523248906
1 parent af1c1d2 commit edd6242

File tree

11 files changed

+216
-64
lines changed

11 files changed

+216
-64
lines changed

site/en/guide/intro_to_modules.ipynb

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@
102102
"id": "yt5HEbsYAbw1"
103103
},
104104
"source": [
105-
"## Defining models and layers in TensorFlow\n",
105+
"## TensorFlow Modules\n",
106106
"\n",
107107
"Most models are made of layers. Layers are functions with a known mathematical structure that can be reused and have trainable variables. In TensorFlow, most high-level implementations of layers and models, such as Keras or [Sonnet](https://github.com/deepmind/sonnet), are built on the same foundational class: `tf.Module`.\n",
108108
"\n",
109+
"### Building Modules\n",
110+
"\n",
109111
"Here's an example of a very simple `tf.Module` that operates on a scalar tensor:\n"
110112
]
111113
},
@@ -337,7 +339,7 @@
337339
"id": "JOLVVBT8J_dl"
338340
},
339341
"source": [
340-
"## Saving weights\n",
342+
"### Saving weights\n",
341343
"\n",
342344
"You can save a `tf.Module` as both a [checkpoint](./checkpoint.ipynb) and a [SavedModel](./saved_model.ipynb).\n",
343345
"\n",
@@ -439,7 +441,7 @@
439441
"id": "pSZebVuWxDXu"
440442
},
441443
"source": [
442-
"## Saving functions\n",
444+
"### Saving functions\n",
443445
"\n",
444446
"TensorFlow can run models without the original Python objects, as demonstrated by [TensorFlow Serving](https://tensorflow.org/tfx) and [TensorFlow Lite](https://tensorflow.org/lite), even when you download a trained model from [TensorFlow Hub](https://tensorflow.org/hub).\n",
445447
"\n",
@@ -699,6 +701,25 @@
699701
"In this section, you will examine how Keras uses `tf.Module`. A complete user guide to Keras models can be found in the [Keras guide](https://www.tensorflow.org/guide/keras/sequential_model).\n"
700702
]
701703
},
704+
{
705+
"metadata": {
706+
"id": "ds08u3touwe4t"
707+
},
708+
"cell_type": "markdown",
709+
"source": [
710+
"Keras layers and models have a lot more extra features including:\n",
711+
"\n",
712+
"* Optional losses\n",
713+
"* Support for [metrics](https://keras.io/api/layers/base_layer/#add_metric-method)\n",
714+
"* Built-in support for an optional `training` argument to differentiate between training and inference use\n",
715+
"* Saving and restoring python objects instead of just black-box functions\n",
716+
"* `get_config` and `from_config` methods that allow you to accurately store configurations to allow model cloning in Python\n",
717+
"\n",
718+
"These features allow for far more complex models through subclassing, such as a custom GAN or a Variational AutoEncoder (VAE) model. Read about them in the [full guide](./keras/custom_layers_and_models.ipynb) to custom layers and models.\n",
719+
"\n",
720+
"Keras models also come with extra functionality that makes them easy to train, evaluate, load, save, and even train on multiple machines."
721+
]
722+
},
702723
{
703724
"cell_type": "markdown",
704725
"metadata": {
@@ -874,22 +895,6 @@
874895
" print(\"Failed:\", e)"
875896
]
876897
},
877-
{
878-
"cell_type": "markdown",
879-
"metadata": {
880-
"id": "YnporXiudF1I"
881-
},
882-
"source": [
883-
"Keras layers have a lot more extra features including:\n",
884-
"\n",
885-
"* Optional losses\n",
886-
"* Support for metrics\n",
887-
"* Built-in support for an optional `training` argument to differentiate between training and inference use\n",
888-
"* `get_config` and `from_config` methods that allow you to accurately store configurations to allow model cloning in Python\n",
889-
"\n",
890-
"Read about them in the [full guide](./keras/custom_layers_and_models.ipynb) to custom layers and models."
891-
]
892-
},
893898
{
894899
"cell_type": "markdown",
895900
"metadata": {
@@ -900,7 +905,7 @@
900905
"\n",
901906
"You can define your model as nested Keras layers.\n",
902907
"\n",
903-
"However, Keras also provides a full-featured model class called `tf.keras.Model`. It inherits from `tf.keras.layers.Layer`, so a Keras model can be used, nested, and saved in the same way as Keras layers. Keras models come with extra functionality that makes them easy to train, evaluate, load, save, and even train on multiple machines.\n",
908+
"However, Keras also provides a full-featured model class called `tf.keras.Model`. It inherits from `tf.keras.layers.Layer`, so a Keras model can be used and nested in the same way as Keras layers. Keras models come with extra functionality that makes them easy to train, evaluate, load, save, and even train on multiple machines.\n",
904909
"\n",
905910
"You can define the `SequentialModule` from above with nearly identical code, again converting `__call__` to `call()` and changing the parent:"
906911
]
@@ -938,7 +943,7 @@
938943
"source": [
939944
"All the same features are available, including tracking variables and submodules.\n",
940945
"\n",
941-
"Note: To emphasize the note above, a raw `tf.Module` nested inside a Keras layer or model will not get its variables collected for training or saving. Instead, nest Keras layers inside of Keras layers."
946+
"Note: A raw `tf.Module` nested inside a Keras layer or model will not get its variables collected for training or saving. Instead, nest Keras layers inside of Keras layers."
942947
]
943948
},
944949
{
@@ -1022,11 +1027,9 @@
10221027
"id": "qI9aXLnaHEFF"
10231028
},
10241029
"source": [
1025-
"## Saving Keras models\n",
1026-
"\n",
1027-
"Keras models can be checkpointed, and that will look the same as `tf.Module`.\n",
1030+
"### Saving Keras models\n",
10281031
"\n",
1029-
"Keras models can also be saved with `tf.saved_model.save()`, as they are modules. However, Keras models have convenience methods and other functionality:"
1032+
"Keras models have their own specialized zip archive saving format, marked by the `.keras` extension. When calling `tf.keras.Model.save`, add a `.keras` extension to the filename. For example:"
10301033
]
10311034
},
10321035
{
@@ -1037,7 +1040,7 @@
10371040
},
10381041
"outputs": [],
10391042
"source": [
1040-
"my_sequential_model.save(\"exname_of_file\")"
1043+
"my_sequential_model.save(\"exname_of_file.keras\")"
10411044
]
10421045
},
10431046
{
@@ -1057,7 +1060,7 @@
10571060
},
10581061
"outputs": [],
10591062
"source": [
1060-
"reconstructed_model = tf.keras.models.load_model(\"exname_of_file\")"
1063+
"reconstructed_model = tf.keras.models.load_model(\"exname_of_file.keras\")"
10611064
]
10621065
},
10631066
{
@@ -1066,7 +1069,7 @@
10661069
"id": "EA7P_MNvpviZ"
10671070
},
10681071
"source": [
1069-
"Keras `SavedModels` also save metric, loss, and optimizer states.\n",
1072+
"Keras zip archives — `.keras` files — also save metric, loss, and optimizer states.\n",
10701073
"\n",
10711074
"This reconstructed model can be used and will produce the same result when called on the same data:"
10721075
]
@@ -1082,6 +1085,17 @@
10821085
"reconstructed_model(tf.constant([[2.0, 2.0, 2.0]]))"
10831086
]
10841087
},
1088+
{
1089+
"metadata": {
1090+
"id": "seLIUG2354s"
1091+
},
1092+
"cell_type": "markdown",
1093+
"source": [
1094+
"### Checkpointing Keras models\n",
1095+
"\n",
1096+
"Keras models can also be checkpointed, and that will look the same as `tf.Module`."
1097+
]
1098+
},
10851099
{
10861100
"cell_type": "markdown",
10871101
"metadata": {

site/en/guide/migrate/migrating_feature_columns.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@
858858
"id": "O01VQIxCWBxU"
859859
},
860860
"source": [
861-
"This composed model can be saved as a [SavedModel](https://www.tensorflow.org/guide/saved_model) for later use."
861+
"This composed model can be saved as a `.keras` file for later use."
862862
]
863863
},
864864
{
@@ -869,8 +869,8 @@
869869
},
870870
"outputs": [],
871871
"source": [
872-
"inference_model.save('model')\n",
873-
"restored_model = tf.keras.models.load_model('model')\n",
872+
"inference_model.save('model.keras')\n",
873+
"restored_model = tf.keras.models.load_model('model.keras')\n",
874874
"restored_model.predict(predict_dataset)"
875875
]
876876
},

site/en/guide/migrate/saved_model.ipynb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,11 @@
352352
"source": [
353353
"### Save and export a SavedModel defined with Keras\n",
354354
"\n",
355-
"The Keras APIs for saving and exporting—`Mode.save` or `tf.keras.models.save_model`—can export a SavedModel from a `tf.keras.Model`. Check out the [Save and load Keras models](../..guide/keras/save_and_serialize) for more details."
355+
"\n",
356+
"Deprecated: For Keras objects, it's recommended to use the new high-level `.keras` format and `tf.keras.Model.export`, as demonstrated in the guide [here](https://www.tensorflow.org/guide/keras/save_and_serialize). The low-level SavedModel format continues to be supported for existing code.\n",
357+
"\n",
358+
"\n",
359+
"The Keras APIs for saving and exporting—`Model.save` or `tf.keras.models.save_model`—can export a SavedModel from a `tf.keras.Model`. Check out the [Save and load Keras models](../..guide/keras/save_and_serialize) for more details."
356360
]
357361
},
358362
{
@@ -506,6 +510,9 @@
506510
"source": [
507511
"### TensorFlow 2: Load a model saved with Keras\n",
508512
"\n",
513+
"Deprecated: For Keras objects, it's recommended to use the new high-level `.keras` format and `tf.keras.Model.export`, as demonstrated in the guide [here](https://www.tensorflow.org/guide/keras/save_and_serialize). The low-level SavedModel format continues to be supported for existing code.\n",
514+
"\n",
515+
"\n",
509516
"The Keras loading API—`tf.keras.models.load_model`—allows you to reload a saved model back into a Keras Model object. Note that this only allows you to load SavedModels saved with Keras (`Model.save` or `tf.keras.models.save_model`).\n",
510517
"\n",
511518
"Models saved with `tf.saved_model.save` should be loaded with `tf.saved_model.load`. You can load a Keras model saved with `Model.save` using `tf.saved_model.load` but you will only get the TensorFlow graph. Refer to the `tf.keras.models.load_model` API docs and [Save and load Keras models](https://www.tensorflow.org/guide/keras/save_and_serialize#savedmodel_format) guide for details."

site/en/guide/saved_model.ipynb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,24 @@
8686
"id": "9SuIC7FiI9g8"
8787
},
8888
"source": [
89-
"## Creating a SavedModel from Keras\n",
89+
"## Creating a SavedModel from Keras"
90+
]
91+
},
92+
{
93+
"cell_type": "markdown",
94+
"metadata": {
95+
"id": "AtSmftAvhJvE"
96+
},
97+
"source": [
98+
"Deprecated: For Keras objects, it's recommended to use the new high-level `.keras` format and `tf.keras.Model.export`, as demonstrated in the guide [here](https://www.tensorflow.org/guide/keras/save_and_serialize). The low-level SavedModel format continues to be supported for existing code."
99+
]
100+
},
101+
{
102+
"cell_type": "markdown",
103+
"metadata": {
104+
"id": "eLSOptpYhJvE"
105+
},
106+
"source": [
90107
"\n",
91108
"For a quick introduction, this section exports a pre-trained Keras model and serves image classification requests with it. The rest of the guide will fill in details and discuss other ways to create SavedModels."
92109
]
@@ -978,8 +995,8 @@
978995
],
979996
"metadata": {
980997
"colab": {
981-
"collapsed_sections": [],
982998
"name": "saved_model.ipynb",
999+
"provenance": [],
9831000
"toc_visible": true
9841001
},
9851002
"kernelspec": {

site/en/tutorials/distribute/keras.ipynb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@
385385
"# Define a callback for printing the learning rate at the end of each epoch.\n",
386386
"class PrintLR(tf.keras.callbacks.Callback):\n",
387387
" def on_epoch_end(self, epoch, logs=None):\n",
388-
" print('\\nLearning rate for epoch {} is {}'.format(",
389-
" epoch + 1, model.optimizer.lr.numpy()))"
388+
" print('\\nLearning rate for epoch {} is {}'.format( epoch + 1, model.optimizer.lr.numpy()))"
390389
]
391390
},
392391
{
@@ -529,7 +528,7 @@
529528
"id": "kBLlogrDvMgg"
530529
},
531530
"source": [
532-
"## Export to SavedModel"
531+
"## Save the model"
533532
]
534533
},
535534
{
@@ -538,7 +537,7 @@
538537
"id": "Xa87y_A0vRma"
539538
},
540539
"source": [
541-
"Export the graph and the variables to the platform-agnostic SavedModel format using Keras `Model.save`. After your model is saved, you can load it with or without the `Strategy.scope`."
540+
"Save the model to a `.keras` zip archive using `Model.save`. After your model is saved, you can load it with or without the `Strategy.scope`."
542541
]
543542
},
544543
{
@@ -549,7 +548,7 @@
549548
},
550549
"outputs": [],
551550
"source": [
552-
"path = 'saved_model/'"
551+
"path = 'my_model.keras'"
553552
]
554553
},
555554
{
@@ -560,7 +559,7 @@
560559
},
561560
"outputs": [],
562561
"source": [
563-
"model.save(path, save_format='tf')"
562+
"model.save(path)"
564563
]
565564
},
566565
{

site/en/tutorials/distribute/multi_worker_with_keras.ipynb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,15 @@
10111011
"With that, you're now ready to save:"
10121012
]
10131013
},
1014+
{
1015+
"cell_type": "markdown",
1016+
"metadata": {
1017+
"id": "XnToxeIcg_6O"
1018+
},
1019+
"source": [
1020+
"Deprecated: For Keras objects, it's recommended to use the new high-level `.keras` format and `tf.keras.Model.export`, as demonstrated in the guide [here](https://www.tensorflow.org/guide/keras/save_and_serialize). The low-level SavedModel format continues to be supported for existing code."
1021+
]
1022+
},
10141023
{
10151024
"cell_type": "code",
10161025
"execution_count": null,
@@ -1275,8 +1284,8 @@
12751284
],
12761285
"metadata": {
12771286
"colab": {
1278-
"collapsed_sections": [],
12791287
"name": "multi_worker_with_keras.ipynb",
1288+
"provenance": [],
12801289
"toc_visible": true
12811290
},
12821291
"kernelspec": {

site/en/tutorials/distribute/save_and_load.ipynb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
"\n",
7878
"Caution: TensorFlow models are code and it is important to be careful with untrusted code. Learn more in [Using TensorFlow securely](https://github.com/tensorflow/tensorflow/blob/master/SECURITY.md).\n",
7979
"\n"
80-
8180
]
8281
},
8382
{
@@ -190,8 +189,8 @@
190189
"Now that you have a simple model to work with, let's explore the saving/loading APIs. \n",
191190
"There are two kinds of APIs available:\n",
192191
"\n",
193-
"* High-level (Keras): `Model.save` and `tf.keras.models.load_model`\n",
194-
"* Low-level: `tf.saved_model.save` and `tf.saved_model.load`\n"
192+
"* High-level (Keras): `Model.save` and `tf.keras.models.load_model` (`.keras` zip archive format)\n",
193+
"* Low-level: `tf.saved_model.save` and `tf.saved_model.load` (TF SavedModel format)\n"
195194
]
196195
},
197196
{
@@ -220,7 +219,7 @@
220219
},
221220
"outputs": [],
222221
"source": [
223-
"keras_model_path = '/tmp/keras_save'\n",
222+
"keras_model_path = '/tmp/keras_save.keras'\n",
224223
"model.save(keras_model_path)"
225224
]
226225
},
@@ -251,7 +250,7 @@
251250
"id": "gYAnskzorda-"
252251
},
253252
"source": [
254-
"After restoring the model, you can continue training on it, even without needing to call `Model.compile` again, since it was already compiled before saving. The model is saved in TensorFlow's standard `SavedModel` proto format. For more information, please refer to [the guide to `SavedModel` format](../../guide/saved_model.ipynb).\n",
253+
"After restoring the model, you can continue training on it, even without needing to call `Model.compile` again, since it was already compiled before saving. The model is saved a Keras zip archive format, marked by the `.keras` extension. For more information, please refer to [the guide on Keras saving](https://www.tensorflow.org/guide/keras/save_and_serialize).\n",
255254
"\n",
256255
"Now, restore the model and train it using a `tf.distribute.Strategy`:"
257256
]
@@ -465,12 +464,12 @@
465464
"model = get_model()\n",
466465
"\n",
467466
"# Saving the model using Keras `Model.save`\n",
468-
"model.save(keras_model_path)\n",
467+
"model.save(saved_model_path)\n",
469468
"\n",
470469
"another_strategy = tf.distribute.MirroredStrategy()\n",
471470
"# Loading the model using the lower-level API\n",
472471
"with another_strategy.scope():\n",
473-
" loaded = tf.saved_model.load(keras_model_path)"
472+
" loaded = tf.saved_model.load(saved_model_path)"
474473
]
475474
},
476475
{
@@ -554,7 +553,7 @@
554553
"\n",
555554
"my_model = SubclassedModel()\n",
556555
"try:\n",
557-
" my_model.save(keras_model_path)\n",
556+
" my_model.save(saved_model_path)\n",
558557
"except ValueError as e:\n",
559558
" print(f'{type(e).__name__}: ', *e.args)"
560559
]
@@ -635,7 +634,7 @@
635634
"my_model.fit(dataset, epochs=2)\n",
636635
"\n",
637636
"print(my_model.save_spec() is None)\n",
638-
"my_model.save(keras_model_path)"
637+
"my_model.save(saved_model_path)"
639638
]
640639
}
641640
],

site/en/tutorials/keras/regression.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@
13211321
},
13221322
"outputs": [],
13231323
"source": [
1324-
"dnn_model.save('dnn_model')"
1324+
"dnn_model.save('dnn_model.keras')"
13251325
]
13261326
},
13271327
{
@@ -1341,7 +1341,7 @@
13411341
},
13421342
"outputs": [],
13431343
"source": [
1344-
"reloaded = tf.keras.models.load_model('dnn_model')\n",
1344+
"reloaded = tf.keras.models.load_model('dnn_model.keras')\n",
13451345
"\n",
13461346
"test_results['reloaded'] = reloaded.evaluate(\n",
13471347
" test_features, test_labels, verbose=0)"

0 commit comments

Comments
 (0)