tf.compat.v1.py_func

View source on GitHub

Wraps a python function and uses it as a TensorFlow op.

tf.compat.v1.py_func(
    func, inp, Tout, stateful=True, name=None
)

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.compat.v1.placeholder(tf.float32)
y = tf.compat.v1.py_func(my_func, [input], tf.float32)

N.B. The tf.compat.v1.py_func() operation has the following known limitations:

Args:

Returns:

A list of Tensor or a single Tensor which func computes.