MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / py_func

Function py_func

tensorflow/contrib/framework/python/ops/script_ops.py:32–147  ·  view source on GitHub ↗

Wraps a python function and uses it as a TensorFlow op. 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

(func,
            args=(),
            kwargs=None,
            output_types=None,
            output_shapes=None,
            stateful=True,
            name=None)

Source from the content-addressed store, hash-verified

30
31
32def py_func(func,
33 args=(),
34 kwargs=None,
35 output_types=None,
36 output_shapes=None,
37 stateful=True,
38 name=None):
39 """Wraps a python function and uses it as a TensorFlow op.
40
41 This function is a wrapper around `tf.compat.v1.py_func` and improve it with
42 kwargs
43 and output_shapes. Further it changed some argument names.
44
45 Given a python function `func`, which takes numpy arrays as its
46 inputs and returns numpy arrays as its outputs, wrap this function as an
47 operation in a TensorFlow graph. The following snippet constructs a simple
48 TensorFlow graph that invokes the `np.sinh()` NumPy function as a operation
49 in the graph:
50
51 ```python
52 def my_func(x):
53 # x will be a numpy array with the contents of the placeholder below
54 return np.sinh(x)
55 inp = tf.compat.v1.placeholder(tf.float32)
56 y = tf.compat.v1.py_func(my_func, [inp], tf.float32)
57 ```
58
59
60 **N.B.** The `tf.compat.v1.py_func()` operation has the following known
61 limitations:
62
63 * The body of the function (i.e. `func`) will not be serialized in a
64 `GraphDef`. Therefore, you should not use this function if you need to
65 serialize your model and restore it in a different environment.
66
67 * The operation must run in the same address space as the Python program
68 that calls `tf.compat.v1.py_func()`. If you are using distributed
69 TensorFlow, you
70 must run a `tf.distribute.Server` in the same process as the program that
71 calls
72 `tf.compat.v1.py_func()` and you must pin the created operation to a device
73 in that
74 server (e.g. using `with tf.device():`).
75
76 Args:
77 func: A Python function, which accepts a list of NumPy `ndarray` objects
78 having element types that match the corresponding `tf.Tensor` objects in
79 `inp`, and returns a list of `ndarray` objects (or a single `ndarray`)
80 having element types that match the corresponding values in `Tout`.
81 args: A list of `Tensor` objects.
82 kwargs: A dict with `Tensor` objects as values.
83 output_types: A nested structure of tensorflow data types or a single
84 tensorflow data type if there is only one, indicating what `func` returns.
85 output_shapes: Same as output_types, except the types are replaces with
86 shapes (optional).
87 stateful: (Boolean.) If True, the function should be considered stateful. If
88 a function is stateless, when given the same input it will return the same
89 output and have no observable side effects. Optimizations such as common

Callers

nothing calls this directly

Calls 4

typeFunction · 0.85
formatMethod · 0.45
flattenMethod · 0.45
set_shapeMethod · 0.45

Tested by

no test coverage detected