tf.numpy_function
Stay organized with collections
Save and categorize content based on your preferences.
Wraps a python function and uses it as a TensorFlow op.
tf.numpy_function(
func, inp, Tout, name=None
)
Given a python function func
wrap this function as an operation in a
TensorFlow function. func
must take numpy arrays as its arguments and
return numpy arrays as its outputs.
The following example creates a TensorFlow graph with np.sinh()
as an
operation in the graph:
def my_numpy_func(x):
# x will be a numpy array with the contents of the input to the
# tf.function
return np.sinh(x)
@tf.function(input_signature=[tf.TensorSpec(None, tf.float32)])
def tf_function(input):
y = tf.numpy_function(my_numpy_func, [input], tf.float32)
return y * y
tf_function(tf.constant(1.))
<tf.Tensor: shape=(), dtype=float32, numpy=1.3810978>
Comparison to tf.py_function
:
tf.py_function
and tf.numpy_function
are very similar, except that
tf.numpy_function
takes numpy arrays, and not tf.Tensor
s. If you want the
function to contain tf.Tensors
, and have any TensorFlow operations executed
in the function be differentiable, please use tf.py_function
.
The body of the function (i.e. func
) will not be serialized in a
tf.SavedModel
. 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.numpy_function()
. If you are using distributed
TensorFlow, you must run a tf.distribute.Server
in the same process as the
program that calls tf.numpy_function
you must pin the created
operation to a device in that server (e.g. using with tf.device():
).
Since the function takes numpy arrays, you cannot take gradients
through a numpy_function. If you require something that is differentiable,
please consider using tf.py_function.
The resulting function is assumed stateful and will never be optimized.
Args |
func
|
A Python function, which accepts numpy.ndarray objects as arguments
and returns a list of numpy.ndarray objects (or a single
numpy.ndarray ). This function must accept as many arguments as there are
tensors in inp , and these argument types will match the corresponding
tf.Tensor objects in inp . The returns numpy.ndarray s must match the
number and types defined Tout .
Important Note: Input and output numpy.ndarray s of func are not
guaranteed to be copies. In some cases their underlying memory will be
shared with the corresponding TensorFlow tensors. In-place modification
or storing func input or return values in python datastructures
without explicit (np.)copy can have non-deterministic consequences.
|
inp
|
A list of tf.Tensor objects.
|
Tout
|
A list or tuple of tensorflow data types or a single tensorflow data
type if there is only one, indicating what func returns.
|
name
|
(Optional) A name for the operation.
|
Returns |
Single or list of tf.Tensor which func computes.
|
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.
[[["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 2020-10-01 UTC."],[],[],null,["# tf.numpy_function\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/numpy_function) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.2.0/tensorflow/python/ops/script_ops.py#L561-L628) |\n\nWraps a python function and uses it as a TensorFlow op.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.numpy_function`](/api_docs/python/tf/numpy_function)\n\n\u003cbr /\u003e\n\n tf.numpy_function(\n func, inp, Tout, name=None\n )\n\nGiven a python function `func` wrap this function as an operation in a\nTensorFlow function. `func` must take numpy arrays as its arguments and\nreturn numpy arrays as its outputs.\n\nThe following example creates a TensorFlow graph with `np.sinh()` as an\noperation in the graph: \n\n def my_numpy_func(x):\n # x will be a numpy array with the contents of the input to the\n # tf.function\n return np.sinh(x)\n @tf.function(input_signature=[tf.TensorSpec(None, tf.float32)])\n def tf_function(input):\n y = tf.numpy_function(my_numpy_func, [input], tf.float32)\n return y * y\n tf_function(tf.constant(1.))\n \u003ctf.Tensor: shape=(), dtype=float32, numpy=1.3810978\u003e\n\nComparison to [`tf.py_function`](../tf/py_function):\n[`tf.py_function`](../tf/py_function) and [`tf.numpy_function`](../tf/numpy_function) are very similar, except that\n[`tf.numpy_function`](../tf/numpy_function) takes numpy arrays, and not [`tf.Tensor`](../tf/Tensor)s. If you want the\nfunction to contain `tf.Tensors`, and have any TensorFlow operations executed\nin the function be differentiable, please use [`tf.py_function`](../tf/py_function).\n| **Note:** The [`tf.numpy_function`](../tf/numpy_function) operation has the following known limitations:\n\n- The body of the function (i.e. `func`) will not be serialized in a\n `tf.SavedModel`. 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.numpy_function()`](../tf/numpy_function). If you are using distributed\n TensorFlow, you must run a [`tf.distribute.Server`](../tf/distribute/Server) in the same process as the\n program that calls [`tf.numpy_function`](../tf/numpy_function) you must pin the created\n operation to a device in that server (e.g. using `with tf.device():`).\n\n- Since the function takes numpy arrays, you cannot take gradients\n through a numpy_function. If you require something that is differentiable,\n please consider using tf.py_function.\n\n- The resulting function is assumed stateful and will never be optimized.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `func` | A Python function, which accepts `numpy.ndarray` objects as arguments and returns a list of `numpy.ndarray` objects (or a single `numpy.ndarray`). This function must accept as many arguments as there are tensors in `inp`, and these argument types will match the corresponding [`tf.Tensor`](../tf/Tensor) objects in `inp`. The returns `numpy.ndarray`s must match the number and types defined `Tout`. Important Note: Input and output `numpy.ndarray`s of `func` are not guaranteed to be copies. In some cases their underlying memory will be shared with the corresponding TensorFlow tensors. In-place modification or storing `func` input or return values in python datastructures without explicit (np.)copy can have non-deterministic consequences. |\n| `inp` | A list of [`tf.Tensor`](../tf/Tensor) objects. |\n| `Tout` | A list or tuple of tensorflow data types or a single tensorflow data type if there is only one, indicating what `func` returns. |\n| `name` | (Optional) A name for the operation. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Single or list of [`tf.Tensor`](../tf/Tensor) which `func` computes. ||\n\n\u003cbr /\u003e"]]