Wraps a python function and uses it as a TensorFlow op.
Aliases:
tf.compat.v1.numpy_function
tf.compat.v2.numpy_function
tf.numpy_function(
func,
inp,
Tout,
name=None
)
Used in the tutorials:
I
ma
ge
c
ap
ti
on
in
gw
i
th
v
is
ua
la
t
te
nt
io
n`` Given a pythonfunc
tionfunc
, which takes numpy arrays as its arguments and returns numpy arrays as its outputs, wrap thisfunc
tion as an operation in a TensorFlow graph. The following snippet constructs a simple TensorFlow graph that invokes thenp.sinh
() NumPyfunc
tion 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)
input = tf.compat.v1.placeholder(tf.float32)
y = tf.compat.v1.numpy_function(my_func, [input], tf.float32)
tf.compat.v1.numpy_function()N.B. The operation has the following known limitations:
- The body of the
func
tion (i.e.func
) will not be serialized in aGraphDef
. Therefore, you should not use thisfunc
tion 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.numpy_function
(). If you are using distributed TensorFlow, you must run atf.distribute.Server
in the same process as the program that callstf.compat.v1.numpy_function
() and you must pin the created operation to a device in that server (e.g. using with tf.device()😃.
Args:
func
: A Pythonfunc
tion, which acceptsndarray
objects as arguments and returns a list ofndarray
objects (or a singlendarray
). Thisfunc
tion must accept as many arguments as there are tensors ininp
, and these argument types will match the correspondingtf.Tensor
objects ininp
. The returnsndarray
s must match the number and types definedTout
. Important Note: Input and output numpyndarray
s offunc
are not guaranteed to be copies. In some cases their underlying memory will be shared with the corresponding TensorFlow tensors. In-place modification or storingfunc
inp
ut or return values in python datastructures without explicit (np.)copy can have non-deterministic consequences.inp
: A list ofTensor
objects.Tout
: A list or tuple of tensorflow data types or a single tensorflow data type if there is only one, indicating whatfunc
returns.stateful
: (Boolean.) If True, thefunc
tion should be consideredstateful
. If afunc
tion is stateless, when given the sameinp
ut 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
: Aname
for the operation (optional).
Returns:
A list of Tensor
or a single Tensor
which func
computes.