Skip to content

Document behavior of save versus save_weights #899

Description

@mhucka

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 save won't work, save_weights will.

This is because the error given when calling save doesn't indicate that this is the problem; in my case, I got


File "(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 get


File "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`.

Originally posted by @mikeevmm in #56

Activity

  1. Aniketsy commented on Sep 14, 2025

    @Aniketsy
    Contributor

    @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!

  2. added theissue type on Jun 6, 2026
  3. added
    help wantedHelp with this would be appreciated
    skill-level/beginnerNo background required, self-contained issue
    on Jun 6, 2026
  4. dp1345 commented on Sep 25, 2026

    @dp1345

    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.

  5. SripriyaDevarapalli commented on Oct 4, 2026

    @SripriyaDevarapalli

    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() and model.save_weights(). It could clarify that saving the full model may require custom layers to implement appropriate serialization methods, such as get_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!

  6. akhil9tiet commented on Oct 7, 2026

    @akhil9tiet

    I'd like to work on this — please assign it to me. I'll open a PR shortly.

  7. mhucka commented on Oct 12, 2026

    @mhucka
    MemberAuthor

    @SripriyaDevarapalli thank you for your interest. Assigned to you.

  8. SripriyaDevarapalli commented on Oct 13, 2026

    @SripriyaDevarapalli

    @mhucka thank you for assigning this issue too me! I'll work on the documentation update and open a PR with the changes soon.

  9. SripriyaDevarapalli commented on Oct 14, 2026

    @SripriyaDevarapalli

    @mhucka I have created PR #1120 with the documentation changes , can you review it and let me know if any changes required .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area/docsInvolves documentation – problems, ideas, requestshelp wantedHelp with this would be appreciatedkind/feature-requestNew feature or requestskill-level/beginnerNo background required, self-contained issue

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions