Saving a model that does not import the backend as "K" breaks model loading.
See original GitHub issueThe following code produces this error: NameError: name 'backend' is not defined
.
from keras import backend
from keras.layers import Dense, Input, Lambda
from keras.models import load_model, Model
x = Input((100, ), dtype = "float32")
x_i = Lambda(lambda x: x + backend.epsilon())(x)
o = Dense(10, activation = "softmax")(x_i)
model = Model(input = [x], output = o)
model.compile("sgd", loss = "categorical_crossentropy")
model.save("toy_model.h5")
model = load_model("toy_model.h5")
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How to Save and Load Your Keras Deep Learning Model
You can save your model by calling the save() function on the model and specifying the filename. The example below demonstrates this by...
Read more >Cannot Reload saved Keras model using tensorflow
While keras (can) use TF as Backend, it does not guarantee that the saved model is readable in TF as well. Note that...
Read more >Save and load Keras models | TensorFlow Core
Saving everything into a single archive in the TensorFlow SavedModel format (or in the older Keras H5 format). This is the standard practice....
Read more >Complete tutorial on Keras Tuner with Tensorflow - Towards AI
During searching, the tuner automatically saves all models in the project directory, but discarded models are not dynamically deleted. This will quickly load...
Read more >Saving and Loading Gluon Models - Apache MXNet
The Model architecture of Hybrid models stays static and don't change during execution. Therefore both model parameters AND architecture can be saved and...
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
I guess this is more readable way to solve: https://github.com/keras-team/keras/issues/4609#issuecomment-329292173 just wire the used lambda object so keras could know where it’s from, like:
looks like you have to add any extra module used in your lambda layer, even it’s from keras itself (ex: the softmax here)
Quick solution: change backend import name, keras doesn’t know your variable in lambda layer