tf.contrib.framework.py_func
Stay organized with collections
Save and categorize content based on your preferences.
Wraps a python function and uses it as a TensorFlow op.
tf.contrib.framework.py_func(
func, args=(), kwargs=None, output_types=None, output_shapes=None,
stateful=True, name=None
)
This function is a wrapper around tf.compat.v1.py_func
and improve it with
kwargs
and output_shapes. Further it changed some argument names.
Given a python function func
, which takes numpy arrays as its
inputs and returns numpy arrays as its outputs, wrap this function as an
operation in a TensorFlow graph. The following snippet constructs a simple
TensorFlow graph that invokes the np.sinh()
NumPy function as a operation
in the graph:
def my_func(x):
# x will be a numpy array with the contents of the placeholder below
return np.sinh(x)
inp = tf.compat.v1.placeholder(tf.float32)
y = tf.compat.v1.py_func(my_func, [inp], tf.float32)
The body of the function (i.e. func
) will not be serialized in a
GraphDef
. Therefore, you should not use this function if you need to
serialize your model and restore it in a different environment.
The operation must run in the same address space as the Python program
that calls tf.compat.v1.py_func()
. If you are using distributed
TensorFlow, you
must run a tf.distribute.Server
in the same process as the program that
calls
tf.compat.v1.py_func()
and you must pin the created operation to a device
in that
server (e.g. using with tf.device():
).
Args |
func
|
A Python function, which accepts a list of NumPy ndarray objects
having element types that match the corresponding tf.Tensor objects in
inp , and returns a list of ndarray objects (or a single ndarray )
having element types that match the corresponding values in Tout .
|
args
|
A list of Tensor objects.
|
kwargs
|
A dict with Tensor objects as values.
|
output_types
|
A nested structure of tensorflow data types or a single
tensorflow data type if there is only one, indicating what func returns.
|
output_shapes
|
Same as output_types, except the types are replaces with
shapes (optional).
|
stateful
|
(Boolean.) If True, the function should be considered stateful. If
a function is stateless, when given the same input it will return the same
output and have no observable side effects. Optimizations such as common
subexpression elimination are only performed on stateless operations.
|
name
|
A name for the operation (optional).
|
Returns |
Tensorflow op that wraps the input python 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.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.contrib.framework.py_func\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/framework/python/ops/script_ops.py#L32-L147) |\n\nWraps a python function and uses it as a TensorFlow op. \n\n tf.contrib.framework.py_func(\n func, args=(), kwargs=None, output_types=None, output_shapes=None,\n stateful=True, name=None\n )\n\nThis function is a wrapper around [`tf.compat.v1.py_func`](../../../tf/py_func) and improve it with\nkwargs\nand output_shapes. Further it changed some argument names.\n\nGiven a python function `func`, which takes numpy arrays as its\ninputs and returns numpy arrays as its outputs, wrap this function as an\noperation in a TensorFlow graph. The following snippet constructs a simple\nTensorFlow graph that invokes the `np.sinh()` NumPy function as a operation\nin the graph: \n\n def my_func(x):\n # x will be a numpy array with the contents of the placeholder below\n return np.sinh(x)\n inp = tf.compat.v1.placeholder(tf.float32)\n y = tf.compat.v1.py_func(my_func, [inp], tf.float32)\n\n| **Note:** The [`tf.compat.v1.py_func()`](../../../tf/py_func) operation has the following known limitations:\n\n- The body of the function (i.e. `func`) will not be serialized in a\n `GraphDef`. Therefore, you should not use this function if you need to\n serialize your model and restore it in a different environment.\n\n- The operation must run in the same address space as the Python program\n that calls [`tf.compat.v1.py_func()`](../../../tf/py_func). If you are using distributed\n TensorFlow, you\n must run a [`tf.distribute.Server`](../../../tf/distribute/Server) in the same process as the program that\n calls\n [`tf.compat.v1.py_func()`](../../../tf/py_func) and you must pin the created operation to a device\n in that\n server (e.g. using `with tf.device():`).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `func` | A Python function, which accepts a list of NumPy `ndarray` objects having element types that match the corresponding [`tf.Tensor`](../../../tf/Tensor) objects in `inp`, and returns a list of `ndarray` objects (or a single `ndarray`) having element types that match the corresponding values in `Tout`. |\n| `args` | A list of `Tensor` objects. |\n| `kwargs` | A dict with `Tensor` objects as values. |\n| `output_types` | A nested structure of tensorflow data types or a single tensorflow data type if there is only one, indicating what `func` returns. |\n| `output_shapes` | Same as output_types, except the types are replaces with shapes (optional). |\n| `stateful` | (Boolean.) If True, the function should be considered stateful. If a function is stateless, when given the same input it will return the same output and have no observable side effects. Optimizations such as common subexpression elimination are only performed on stateless operations. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Tensorflow op that wraps the input python function. ||\n\n\u003cbr /\u003e"]]