tfp.layers.util.deserialize_function
Stay organized with collections
Save and categorize content based on your preferences.
Deserializes the Keras-serialized function.
tfp.layers.util.deserialize_function(
serial, function_type
)
(De)serializing Python functions from/to bytecode is unsafe. Therefore we
also use 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!)
Keras-deserialized functions do not perform lexical scoping. Any modules that
the function requires must be imported within the function itself.
This serialization mimicks the implementation in tf.keras.layers.Lambda
.
Args |
serial
|
Serialized Keras object: typically a dict, string, or bytecode.
|
function_type
|
Python string denoting 'function' or 'lambda'.
|
Returns |
function
|
Function the serialized Keras object represents.
|
Examples
serial, function_type = serialize_function(lambda x: x)
function = deserialize_function(serial, function_type)
assert function(2.3) == 2.3 # function is identity
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-11-21 UTC.
[null,null,["Last updated 2023-11-21 UTC."],[],[],null,["# tfp.layers.util.deserialize_function\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/probability/blob/v0.23.0/tensorflow_probability/python/layers/util.py#L226-L264) |\n\nDeserializes the Keras-serialized function. \n\n tfp.layers.util.deserialize_function(\n serial, function_type\n )\n\n(De)serializing Python functions from/to bytecode is unsafe. Therefore we\nalso use the function's type as an anonymous function ('lambda') or named\nfunction in the Python environment ('function'). In the latter case, this lets\nus use the Python scope to obtain the function rather than reload it from\nbytecode. (Note that both cases are brittle!)\n\nKeras-deserialized functions do not perform lexical scoping. Any modules that\nthe function requires must be imported within the function itself.\n\nThis serialization mimicks the implementation in [`tf.keras.layers.Lambda`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Lambda).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------|-----------------------------------------------------------------|\n| `serial` | Serialized Keras object: typically a dict, string, or bytecode. |\n| `function_type` | Python string denoting 'function' or 'lambda'. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|------------|--------------------------------------------------|\n| `function` | Function the serialized Keras object represents. |\n\n\u003cbr /\u003e\n\n#### Examples\n\n serial, function_type = serialize_function(lambda x: x)\n function = deserialize_function(serial, function_type)\n assert function(2.3) == 2.3 # function is identity"]]