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

Created on 19 Jan 2017  路  5Comments  路  Source: keras-team/keras

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")
stale

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:

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)

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NancyZxll picture NancyZxll  路  3Comments

fredtcaroli picture fredtcaroli  路  3Comments

braingineer picture braingineer  路  3Comments

vinayakumarr picture vinayakumarr  路  3Comments

zygmuntz picture zygmuntz  路  3Comments