ValueError: Unknown layer: <function <lambda> at 0x...> in Keras Lambda layer
25.1K reputation · 18 Feb 2024, 11:50 UTC
The goal is to guarantee that a Keras model containing a Lambda layer can be saved and subsequently reloaded in a different process or environment without raising a ValueError about an unknown layer. In local interactive sessions the inline lambda function remains in memory, allowing the model to be used directly, but after model.save the layer’s configuration cannot reconstruct the function, leading to the error 'ValueError: Unknown layer: at 0x...>' when tf.keras.models.load_model is called. The current behavior places the responsibility on the user to define the lambda at module level or to supply a custom_objects mapping, yet it remains unclear whether the framework should handle this automatically.
Should Keras automatically wrap inline lambda functions in a serializable wrapper to make them loadable without user intervention? What are the potential performance, security, or compatibility trade‑offs of such automatic wrapping? Would strengthening the documentation to require module‑level definitions be enough to avoid production failures, or does the API need a change?