Repository navigation
Document behavior of save versus save_weights #899
Copy link
Copy link
Open
Labels
area/docsInvolves documentation – problems, ideas, requestsInvolves documentation – problems, ideas, requestshelp wantedHelp with this would be appreciatedHelp with this would be appreciatedkind/feature-requestNew feature or requestNew feature or requestskill-level/beginnerNo background required, self-contained issueNo background required, self-contained issue
Description
In issue #56, there is a request for improving the docs:
Would like to +1 this, and ask that it is made clear(er) in the documentation that although
savewon't work,save_weightswill.This is because the error given when calling
savedoesn't indicate that this is the problem; in my case, I gotFile "(virtual environment)/lib/python3.8/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 98, in encode_structure return self._map_structure(nested_structure, self._get_encoders()) File "(virtual environment)/lib/python3.8/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 82, in _map_structure return do(pyobj, recursion_fn) File "(virtual environment)/lib/python3.8/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 237, in do_encode pair.value.CopyFrom(encode_fn(named_tuple_value._asdict()[key])) File "(virtual environment)/lib/python3.8/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 82, in _map_structure return do(pyobj, recursion_fn) File "(virtual environment)/lib/python3.8/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 206, in do_encode encoded_dict.dict_value.fields[key].CopyFrom(encode_fn(value)) File "(virtual environment)/lib/python3.8/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 83, in _map_structure raise NotEncodableError( tensorflow.python.saved_model.nested_structure_coder.NotEncodableError: No encoder for object [<tensorflow.python.keras.initializers.initializers_v2.RandomUniform object at 0x7f241a041880>] of type [<class 'tensorflow.python.keras.initializers.initializers_v2.RandomUniform'>].Only when explicitly calling the model's
get_config()method did I getFile "learn_params.py", line 194, in <module> print(model.get_config()) File "(virtual environment)/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py", line 598, in get_config return copy.deepcopy(get_network_config(self)) File "(virtual environment)/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py", line 1278, in get_network_config layer_config = serialize_layer_fn(layer) File "(virtual environment)/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 250, in serialize_keras_object raise e File "(virtual environment)/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 245, in serialize_keras_object config = instance.get_config() File "(virtual environment)/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 676, in get_config raise NotImplementedError('Layer %s has arguments in `__init__` and ' NotImplementedError: Layer ControlledPQC has arguments in `__init__` and therefore must override `get_config`.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area/docsInvolves documentation – problems, ideas, requestsInvolves documentation – problems, ideas, requestshelp wantedHelp with this would be appreciatedHelp with this would be appreciatedkind/feature-requestNew feature or requestNew feature or requestskill-level/beginnerNo background required, self-contained issueNo background required, self-contained issue
Activity
Aniketsy commentedon Sep 14, 2025
@mhucka I’m planning to add get_config() and from_config() to ControlledPQC so it can be properly serialized/deserialized with model.save(). For non-trivial arguments (like Cirq circuits and operators), I’ll handle them with appropriate serialization or placeholders, and document any limitations. Does this sound like the right approach?
Any corrections or suggestions would be greatly appreciated. Thanks!
dp1345 commentedon Sep 25, 2026
I think this would be best addressed as a documentation clarification rather than a new tutorial. A short note in docs/concepts.md or the relevant tutorial could explain that model.save() may fail with custom TFQ/Keras layers, while model.save_weights() can be used to save the model’s weights.
Is this the correct approach? I would value any corrections or feedback you might have.
SripriyaDevarapalli commentedon Oct 4, 2026
Hi! I agree that a short documentation clarification would be helpful here, especially for users working with custom TensorFlow Quantum/Keras layers.
One possible improvement would be to add a brief note to the relevant documentation explaining the difference between
model.save()andmodel.save_weights(). It could clarify that saving the full model may require custom layers to implement appropriate serialization methods, such asget_config()and, where needed,from_config(), while saving weights is intended to preserve the model's learned parameters rather than its complete configuration.A small example showing when to use each method, along with a note about custom-layer serialization limitations, could help users understand the issue and choose the appropriate approach.
I'd be happy to work on a focused documentation update if this direction sounds appropriate. Please let me know if there is a preferred file or section where this explanation should go. Thank you!
akhil9tiet commentedon Oct 7, 2026
I'd like to work on this — please assign it to me. I'll open a PR shortly.
mhucka commentedon Oct 12, 2026
@SripriyaDevarapalli thank you for your interest. Assigned to you.
SripriyaDevarapalli commentedon Oct 13, 2026
@mhucka thank you for assigning this issue too me! I'll work on the documentation update and open a PR with the changes soon.
SripriyaDevarapalli commentedon Oct 14, 2026
@mhucka I have created PR #1120 with the documentation changes , can you review it and let me know if any changes required .