question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Saving a model that does not import the backend as "K" breaks model loading.

See original GitHub issue

The 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:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
marsan-macommented, Jun 29, 2018

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:

from keras import backend
from keras.models import load_model
from keras.activations import softmax

model = load_model("yourmodel.h5", custom_objects={
    "backend": backend,
    "softmax": softmax,
})

looks like you have to add any extra module used in your lambda layer, even it’s from keras itself (ex: the softmax here)

3reactions
joelthchaocommented, Jan 19, 2017

Quick solution: change backend import name, keras doesn’t know your variable in lambda layer

from keras import backend as K
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 + K.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")
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found