Loading models with 2 layers sharing weights fails
See original GitHub issueIf model has shared weights and 2 layers calling each other with shared weights, it introduces a cyclic dependency.
z = keras.layers.Input(shape=(2,))
A = keras.layers.Dense(2)
B = keras.layers.Dense(2)
output = A(B(A(B(z))))
M = keras.models.Model(input=z, output=output)
M.save("test.hd5")
keras.models.load_model("test.hd5")
Then keras/engine/topology.py:2362 assert inbound_layer_name in created_layers, 'Missing layer: %s' % inbound_layer_name
throws
AssertionError: Missing layer: dense_1
Is there a simple fix? Worth noting that M.load_weights(“test.hd5”) does work, but unfortunately I need load_model
to work
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Unable to load and use multiple keras models - Stack Overflow
There is a serious bug when you try to load multiple weight files in the same script. The above answer doesn't solve this....
Read more >How to properly save and load an intermediate model in Keras?
Try to save the model to JSON, and the weights in HDF5 format with save_weights() . # save the model model_json = model_2.to_json()...
Read more >The Functional API | TensorFlow Core
The functional API can handle models with non-linear topology, shared layers, and even multiple inputs or outputs.
Read more >Working With The Lambda Layer in Keras - Paperspace Blog
In this tutorial we'll cover how to use the Lambda layer in Keras to build, save, and load models which perform custom operations...
Read more >Serialization and saving - Keras
In order to save/load a model with custom-defined layers, or a subclassed model, you should overwrite the get_config and optionally from_config ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Here is a solution I hacked for that. Fix is changing
keras.engine.topology.Container.from_config
to step iteratively through graph. Each run ofstep_through_graph
(through whole network) will call at least one layer so afterall_calls
it should have called all layers. I am pretty sure there is a nicer solution, but that was fast to code 😃 I think each application of the layer should be stored as a separate entry in config, then from_config could be left almost unchanged, but that is hard to introduce as it seems to be a breaking change.@kudkudak note that you should have sent a PR rather than leaving a code fix in an issues thread… Feel free to check out my fix and compare to yours. I’m sure mine can be improve.