Join us at TensorFlow World, Oct 28-31. Use code TF20 for 20% off select passes. Register now


Serializes function for Keras.


Defined in python/layers/

(De)serializing Python functions from/to bytecode is unsafe. Therefore we return the function's type as an anonymous function ('lambda') or named function in the Python environment ('function'). In the latter case, this lets us use the Python scope to obtain the function rather than reload it from bytecode. (Note that both cases are brittle!)

This serialization mimicks the implementation in tf.keras.layers.Lambda.


  • func: Python function to serialize.


(serial, function_type): Serialized object, which is a tuple of its bytecode (if function is anonymous) or name (if function is named), and its function type.