svoruganti
svoruganti

Reputation: 672

Keras name 'K' is not defined

I am trying to load a Keras model like so:

mpiotte_model = keras.models.load_model('./metadata/mpiotte-standard.model')

But I get the following error:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-31-63426033cbe8> in <module>()
      2 steps = 0
      3 
----> 4 mpiotte_model = keras.models.load_model('./metadata/mpiotte-standard.model')
      5 model.set_weights(mpiotte_model.get_weights())

1 frames
/usr/local/lib/python3.7/dist-packages/keras/layers/core/lambda_layer.py in <lambda>(x)

NameError: Exception encountered when calling layer "lambda_3" (type Lambda).

name 'K' is not defined

Call arguments received:
  • inputs=['tf.Tensor(shape=(None, 512), dtype=float32)', 'tf.Tensor(shape=(None, 512), dtype=float32)']
  • mask=None
  • training=False

I also have this line, which successfully imports:

from keras import backend as K

I am running this on a Google Colab, but I do not know how to fix the issue.

Upvotes: 1

Views: 2447

Answers (1)

VEDANT JOSHI
VEDANT JOSHI

Reputation: 93

This import is absolutely fine :

from keras import backend as K

You can try this to solve the problem :

mpiotte_model = keras.models.load_model('./metadata/mpiotte-standard.model', custom_objects={"K": K})

Upvotes: 4

Related Questions