Skip to content

Commit 47d66e2

Browse files
committed
Call base_model.save() for backward compatibility.
The save() interface has changed in different TF versions. Since NSL supports back to TF 1.15, it is better to accept variadic arguments. Also updated the example notebook following internal review comments.
1 parent 7292095 commit 47d66e2

File tree

3 files changed

+14
-77
lines changed

3 files changed

+14
-77
lines changed

neural_structured_learning/examples/notebooks/How_to_save_and_load_model_in_nsl.ipynb renamed to neural_structured_learning/examples/notebooks/saving_and_loading_nsl_models.ipynb

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"id": "ZwZNOAMZcxl3"
77
},
88
"source": [
9-
"##### Copyright 2019 The TensorFlow Neural Structured Learning Authors"
9+
"##### Copyright 2022 The TensorFlow Neural Structured Learning Authors"
1010
]
1111
},
1212
{
@@ -37,7 +37,7 @@
3737
"id": "-BszoQj0dSZO"
3838
},
3939
"source": [
40-
"# How to save and load models in NSL\n"
40+
"# Saving and loading NSL models\n"
4141
]
4242
},
4343
{
@@ -48,10 +48,10 @@
4848
"source": [
4949
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
5050
" <td>\n",
51-
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/neural-structured-learning/blob/master/neural_structured_learning/examples/notebooks/How_to_save_and_load_model_in_nsl.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
51+
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/neural-structured-learning/blob/master/neural_structured_learning/examples/notebooks/saving_and_loading_nsl_models.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
5252
" </td>\n",
5353
" <td>\n",
54-
" <a target=\"_blank\" href=\"https://github.com/tensorflow/neural-structured-learning/blob/master/neural_structured_learning/examples/notebooks/How_to_save_and_load_model_in_nsl.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
54+
" <a target=\"_blank\" href=\"https://github.com/tensorflow/neural-structured-learning/blob/master/neural_structured_learning/examples/notebooks/saving_and_loading_nsl_models.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
5555
" </td>\n",
5656
"</table>"
5757
]
@@ -76,14 +76,14 @@
7676
" * Here we create the base model with the `tf.keras` functional API.\n",
7777
" * This procedure is also compatible with models created by the\n",
7878
" `tf.keras.Sequential` API and models created by subclasses of\n",
79-
" `tf.keras.Model`, provided that the subclasses support saving and loading.\n",
79+
" `tf.keras.Model`, provided that the subclass models support saving and loading.\n",
8080
" * For more information on Keras models in TensorFlow, see this\n",
8181
" [documentation](https://www.tensorflow.org/api_docs/python/tf/keras/Model).\n",
8282
"2. Wrap the base model with the **`AdversarialRegularization`** wrapper class.\n",
8383
" * The wrapper class is provided by the NSL framework, and it creates a new\n",
8484
" `tf.keras.Model` instance.\n",
85-
" * This new model will include the adversarial loss as a regularization term\n",
86-
" in its training objective.\n",
85+
" * This new model is a subclass model and will include the adversarial loss\n",
86+
" as a regularization term in its training objective.\n",
8787
"3. Train and evaluate the new model.\n",
8888
"4. Save the model, and load it back.\n",
8989
"5. Verify that the loaded model is the same as the one before saving."
@@ -107,17 +107,6 @@
107107
"Install the Neural Structured Learning package."
108108
]
109109
},
110-
{
111-
"cell_type": "code",
112-
"source": [
113-
"!pip uninstall -y neural-structured-learning"
114-
],
115-
"metadata": {
116-
"id": "rYfawinCPpuC"
117-
},
118-
"execution_count": null,
119-
"outputs": []
120-
},
121110
{
122111
"cell_type": "code",
123112
"execution_count": null,
@@ -135,7 +124,7 @@
135124
"id": "PZvsEQrhSqKx"
136125
},
137126
"source": [
138-
"Import libraries. We abbreviate `neural_structured_learning` to `nsl`."
127+
"Import libraries."
139128
]
140129
},
141130
{
@@ -683,7 +672,7 @@
683672
"accelerator": "GPU",
684673
"colab": {
685674
"collapsed_sections": [],
686-
"name": "How to save and load model in nsl.ipynb",
675+
"name": "Saving and loading NSL models",
687676
"private_outputs": true,
688677
"provenance": [],
689678
"toc_visible": true

neural_structured_learning/keras/adversarial_regularization.py

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323

2424

2525
import attr
26-
27-
from keras.saving import save
28-
from keras.utils import traceback_utils
29-
3026
import neural_structured_learning.configs as nsl_configs
3127
from neural_structured_learning.lib import adversarial_neighbor
3228
import six
@@ -699,58 +695,11 @@ def call(self, inputs, **kwargs):
699695
scaled_adv_loss, name='scaled_adversarial_loss', aggregation='mean')
700696
return outputs
701697

702-
703-
@traceback_utils.filter_traceback
704-
def save(self,
705-
filepath,
706-
overwrite=True,
707-
include_optimizer=True,
708-
save_format=None,
709-
signatures=None,
710-
options=None,
711-
save_traces=True):
712-
# pylint: disable=line-too-long
713-
"""Saves the model to Tensorflow SavedModel or a single HDF5 file.
714-
Please see `tf.keras.models.save_model` or the
715-
[Serialization and Saving guide](https://keras.io/guides/serialization_and_saving/)
716-
for details.
717-
Args:
718-
filepath: String, PathLike, path to SavedModel or H5 file to save the
719-
model.
720-
overwrite: Whether to silently overwrite any existing file at the
721-
target location, or provide the user with a manual prompt.
722-
include_optimizer: If True, save optimizer's state together.
723-
save_format: Either `'tf'` or `'h5'`, indicating whether to save the
724-
model to Tensorflow SavedModel or HDF5. Defaults to 'tf' in TF 2.X,
725-
and 'h5' in TF 1.X.
726-
signatures: Signatures to save with the SavedModel. Applicable to the
727-
'tf' format only. Please see the `signatures` argument in
728-
`tf.saved_model.save` for details.
729-
options: (only applies to SavedModel format)
730-
`tf.saved_model.SaveOptions` object that specifies options for
731-
saving to SavedModel.
732-
save_traces: (only applies to SavedModel format) When enabled, the
733-
SavedModel will store the function traces for each layer. This
734-
can be disabled, so that only the configs of each layer are stored.
735-
Defaults to `True`. Disabling this will decrease serialization time
736-
and reduce file size, but it requires that all custom layers/models
737-
implement a `get_config()` method.
738-
Example:
739-
```python
740-
from keras.models import load_model
741-
model.save('my_model.h5') # creates a HDF5 file 'my_model.h5'
742-
del model # deletes the existing model
743-
# returns a compiled model
744-
# identical to the previous one
745-
model = load_model('my_model.h5')
746-
```
747-
"""
748-
# pylint: enable=line-too-long
749-
save.save_model(self.base_model, filepath, overwrite, include_optimizer, save_format,
750-
signatures, options, save_traces)
751-
752-
753-
698+
def save(self, *args, **kwargs):
699+
"""Saves the base model. See base class for details of the interface."""
700+
# Adversarial regularization doesn't introduce new model variables, so
701+
# saving the base model can capture all variables in the model.
702+
self.base_model.save(*args, **kwargs)
754703

755704
def perturb_on_batch(self, x, **config_kwargs):
756705
"""Perturbs the given input to generates adversarial examples.

neural_structured_learning/requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ absl-py>=0.7
22
attrs>=19.1.0
33
scipy>=1.2.0
44
six>=1.10
5-
keras>= 2.2.5

0 commit comments

Comments
 (0)