Wraps a python function and uses it as a TensorFlow op. (deprecated)
tf.numpy_function( func, inp, Tout, name=None )
Warning: THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: tf.numpy_function is deprecated in TF V2. Instead, there are two options available in V2.
- tf.numpy_functiontion takes a python function which manipulates tf eager tensors instead of numpy arrays. It's easy to convert a tf eager tensor to an ndarray (just call tensor.numpy()) but having access to eager tensors means `tf.numpy_functiontion`s can use accelerators such as GPUs as well as being differentiable using a gradient tape. - tf.numnumpy_functiontion maintains the semantics of the deprecated tf.numpy_function (it is not differentiable, and manipulates numpy arrays). It drops the stateful argument making all functions stateful.
Given a python function
func, which takes numpy arrays as its
arguments 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) input = tf.placeholder(tf.float32) y = tf.numpy_function(my_func, [input], tf.float32)
tf.numpy_function() operation has the following known limitations:
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.numpy_function(). If you are using distributed TensorFlow, you must run a
tf.train.Serverin the same process as the program that calls
tf.numpy_function()and you must pin the created operation to a device in that server (e.g. using
func: A Python function, which accepts
ndarrayobjects as arguments and returns a list of
ndarrayobjects (or a single
ndarray). This function must accept as many arguments as there are tensors in
inp, and these argument types will match the corresponding
inp. The returns
ndarrays must match the number and types defined
Tout. Important Note: Input and output numpy
funcare not guaranteed to be copies. In some cases their underlying memory will be shared with the corresponding TensorFlow tensors. In-place modification or storing
funcinput or return values in python datastructures without explicit (np.)copy can have non-deterministic consequences.
inp: A list of
Tout: A list or tuple of tensorflow data types or a single tensorflow data type if there is only one, indicating what
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).
A list of
Tensor or a single