Repository navigation
docs: explain save vs save_weights for TFQ layers #1120
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,34 @@ operations. Additionally, you can create a layer with a high degree of | |
| complexity supporting batch circuit, batch control parameter value, and perform | ||
| batch readout operations. See `tfq.layers.Sample` for an example. | ||
|
|
||
| #### Saving and loading models | ||
|
|
||
| Saving a complete Keras model with `model.save()` is not supported for | ||
| models containing TFQ layers, including `tfq.layers.PQC` and | ||
| `tfq.layers.ControlledPQC`. These layers accept quantum objects, such as | ||
| `cirq.Circuit`, `cirq.PauliSum`, and `sympy.Symbol`, that are not | ||
| JSON-serializable, and the layers do not implement `get_config()` to | ||
| serialize their configurations. | ||
|
|
||
| Depending on the serialization path, calling `model.save()` may raise a | ||
| `NotImplementedError` indicating that the layer must override | ||
| `get_config()`, a `TypeError` for non-JSON-serializable `call()` arguments | ||
| such as `sympy.Symbol` or `cirq.PauliSum`, or a `NotEncodableError` when | ||
| tracing layer `call` defaults for `SavedModel` export. This limitation is | ||
| not specific to a particular TFQ, TensorFlow, or Keras version. | ||
|
|
||
| As an alternative, `model.save_weights()` saves the model's weights without | ||
| saving the complete model configuration. Recreate the same model | ||
| architecture before loading the saved weights. The recreated model must | ||
| be built (its variables must be created) before calling `load_weights()`: | ||
|
|
||
| ```python | ||
| model.save_weights("my_model.weights.h5") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. build_model() is never defined in the example. Readers have to guess what it should contain. At minimum, add a sentence saying the recreated model must be built (its variables created) before calling |
||
|
|
||
| new_model = build_model() # Recreate the same model architecture. | ||
| new_model.load_weights("my_model.weights.h5") | ||
| ``` | ||
|
|
||
| ### Differentiators | ||
|
|
||
| Unlike many TensorFlow operations, observables in quantum circuits do not have | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please wrap to 80 columns, per .editorconfig and .markdownlintrc settings.