tf.compat.v1.wrap_function
Stay organized with collections
Save and categorize content based on your preferences.
Wraps the TF 1.x function fn into a graph function.
tf.compat.v1.wrap_function(
fn, signature, name=None
)
Used in the notebooks
The python function fn
will be called once with symbolic arguments specified
in the signature
, traced, and turned into a graph function. Any variables
created by fn
will be owned by the object returned by wrap_function
. The
resulting graph function can be called with tensors which match the
signature.
def f(x, do_add):
v = tf.Variable(5.0)
if do_add:
op = v.assign_add(x)
else:
op = v.assign_sub(x)
with tf.control_dependencies([op]):
return v.read_value()
f_add = tf.compat.v1.wrap_function(f, [tf.TensorSpec((), tf.float32), True])
assert float(f_add(1.0)) == 6.0
assert float(f_add(1.0)) == 7.0
# Can call tf.compat.v1.wrap_function again to get a new trace, a new set
# of variables, and possibly different non-template arguments.
f_sub= tf.compat.v1.wrap_function(f, [tf.TensorSpec((), tf.float32), False])
assert float(f_sub(1.0)) == 4.0
assert float(f_sub(1.0)) == 3.0
Both tf.compat.v1.wrap_function
and tf.function
create a callable
TensorFlow graph. But while tf.function
runs all stateful operations
(e.g. tf.print
) and sequences operations to provide the same semantics as
eager execution, wrap_function
is closer to the behavior of session.run
in
TensorFlow 1.x. It will not run any operations unless they are required to
compute the function's outputs, either through a data dependency or a control
dependency. Nor will it sequence operations.
Unlike tf.function
, wrap_function
will only trace the Python function
once. As with placeholders in TF 1.x, shapes and dtypes must be provided to
wrap_function
's signature
argument.
Since it is only traced once, variables and state may be created inside the
function and owned by the function wrapper object.
Args |
fn
|
python function to be wrapped
|
signature
|
the placeholder and python arguments to be passed to the wrapped
function
|
name
|
Optional. The name of the function.
|
Returns |
the wrapped graph function.
|
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. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.compat.v1.wrap_function\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/eager/wrap_function.py#L576-L647) |\n\nWraps the TF 1.x function fn into a graph function. \n\n tf.compat.v1.wrap_function(\n fn, signature, name=None\n )\n\n### Used in the notebooks\n\n| Used in the guide |\n|-------------------------------------------------------------------------------------------|\n| - [Migrate the SavedModel workflow](https://www.tensorflow.org/guide/migrate/saved_model) |\n\nThe python function `fn` will be called once with symbolic arguments specified\nin the `signature`, traced, and turned into a graph function. Any variables\ncreated by `fn` will be owned by the object returned by `wrap_function`. The\nresulting graph function can be called with tensors which match the\nsignature. \n\n def f(x, do_add):\n v = tf.Variable(5.0)\n if do_add:\n op = v.assign_add(x)\n else:\n op = v.assign_sub(x)\n with tf.control_dependencies([op]):\n return v.read_value()\n\n f_add = tf.compat.v1.wrap_function(f, [tf.TensorSpec((), tf.float32), True])\n\n assert float(f_add(1.0)) == 6.0\n assert float(f_add(1.0)) == 7.0\n\n # Can call tf.compat.v1.wrap_function again to get a new trace, a new set\n # of variables, and possibly different non-template arguments.\n f_sub= tf.compat.v1.wrap_function(f, [tf.TensorSpec((), tf.float32), False])\n\n assert float(f_sub(1.0)) == 4.0\n assert float(f_sub(1.0)) == 3.0\n\nBoth [`tf.compat.v1.wrap_function`](../../../tf/compat/v1/wrap_function) and [`tf.function`](../../../tf/function) create a callable\nTensorFlow graph. But while [`tf.function`](../../../tf/function) runs all stateful operations\n(e.g. [`tf.print`](../../../tf/print)) and sequences operations to provide the same semantics as\neager execution, `wrap_function` is closer to the behavior of `session.run` in\nTensorFlow 1.x. It will not run any operations unless they are required to\ncompute the function's outputs, either through a data dependency or a control\ndependency. Nor will it sequence operations.\n\nUnlike [`tf.function`](../../../tf/function), `wrap_function` will only trace the Python function\nonce. As with placeholders in TF 1.x, shapes and dtypes must be provided to\n`wrap_function`'s `signature` argument.\n\nSince it is only traced once, variables and state may be created inside the\nfunction and owned by the function wrapper object.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|---------------------------------------------------------------------------|\n| `fn` | python function to be wrapped |\n| `signature` | the placeholder and python arguments to be passed to the wrapped function |\n| `name` | Optional. The name of the function. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| the wrapped graph function. ||\n\n\u003cbr /\u003e"]]