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")
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")
@joelthchao - thanks for trying to help, but I had already realized that changing the import name for backend to K would make the error go away (hence my title). The problem is that Keras allows you to save a model using the actual name of the backend module but is unable to load such a model.
No quick fix but worth looking into. There are similar issues if you try to use some custom imports in your lambda. Another workaround is to do the import within the Lambda.
def mylambda(x):
from keras import backend
import mymodule
... #use backend and mymodule
y = Lambda(mylambda)(h)
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)
custom_objects helped me to provide my custom function name which i am using inside lambda function in the model. I was able to load the model now which otherwise was showing that 'customobject' is not found error.
Most helpful comment
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)