tensorflow / quantum

Hybrid Quantum-Classical Machine Learning in TensorFlow
https://www.tensorflow.org/quantum
Apache License 2.0
1.78k stars 571 forks source link

Add custom `get_config` for PQC layers #56

Open zaqqwerty opened 4 years ago

zaqqwerty commented 4 years ago

"This would allow users to call methods like save which can load full models without the original python code that created them. For now we have only save_weights which requires the user to also have the original python code that created the model (not a huge constraint)." @MichaelBroughton

MichaelBroughton commented 4 years ago

@trevormccrt do you want to look into this since you have some experience with Keras Layers ?

zaqqwerty commented 4 years ago

This seems to be a priority, since users are running into the issue on SO. I will try.

github-actions[bot] commented 4 years ago

This issue has not had any activity in a month. Is it stale ?

mikeevmm commented 3 years ago

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