def py_func(func, inp, Tout, stateful=True, name=None): """Wraps a python function and uses it as a tensorflow op. Given a python function `func`, which takes numpy arrays as its inputs and returns numpy arrays as its outputs. E.g., ```python def my_func(x): # x will be a numpy array with the contents of the placeholder below return np.sinh(x) inp = tf.placeholder(tf.float32, [...]) y = py_func(my_func, [inp], [tf.float32]) ``` The above snippet constructs a tf graph which invokes a numpy sinh(x) as an op in the graph. Args: func: A python function. inp: A list of `Tensor`. Tout: A list of tensorflow data types indicating what `func` returns. stateful: A boolean indicating whether the function should be considered stateful or stateless. I.e. whether it, given the same input, will return the same output and at the same time does not change state in an observable way. Optimizations such as common subexpression elimination are only possible when operations are stateless. name: A name for the operation (optional). Returns: A list of `Tensor` which `func` computes. """ token = _py_funcs.insert(func) # We tie the registered function's life-time with the current # default graph. I.e., when the current graph is destroyed, we # should remove its py funcs. cleanup = CleanupFunc(token) g = ops.get_default_graph() # pylint: disable=protected-access # # TODO(zhifengc): Consider adding a Graph method to collect # `cleanup` objects in one of its member. if not hasattr(g, "_cleanup_py_funcs_used_in_graph"): g._cleanup_py_funcs_used_in_graph = [] # When g is destroyed, elements in _cleanup_py_funcs_used_in_graph # will be destroyed and their __del__ will remove the 'token' from # the funcs registry. g._cleanup_py_funcs_used_in_graph.append(cleanup) if stateful: return gen_script_ops._py_func(input=inp, token=token, Tout=Tout, name=name) # pylint: enable=protected-access else: return gen_script_ops._py_func_stateless( input=inp, token=token, Tout=Tout, name=name)
def _internal_py_func(func, inp, Tout, stateful=None, eager=False, name=None): """See documentation for py_func and eager_py_func.""" is_list_or_tuple = False if isinstance(Tout, (list, tuple)): is_list_or_tuple = True else: Tout = [Tout] if eager: func = EagerFunc(func, Tout) token = _py_funcs.insert(func) # We tie the registered function's lifetime with the current default graph, # i.e., when the current graph is destroyed, we remove its py funcs. graph = ops.get_default_graph() # pylint: disable=protected-access while isinstance(graph, function._FuncGraph): # If the py_func was declared inside a _FuncGraph, its lifetime should be # bound to that of the outer graph instead. graph = graph._outer_graph cleanup = CleanupFunc(token) # TODO(zhifengc): Consider adding a Graph method to collect # `cleanup` objects in one of its member. if not hasattr(graph, "_cleanup_py_funcs_used_in_graph"): graph._cleanup_py_funcs_used_in_graph = [] # When `graph` is destroyed, elements in _cleanup_py_funcs_used_in_graph # will be destroyed and their __del__ will remove the 'token' from # the funcs registry. graph._cleanup_py_funcs_used_in_graph.append(cleanup) # pylint: enable=protected-access # pylint: disable=protected-access if eager: result = gen_script_ops._eager_py_func(input=inp, token=token, Tout=Tout, name=name) else: if stateful: result = gen_script_ops._py_func(input=inp, token=token, Tout=Tout, name=name) else: result = gen_script_ops._py_func_stateless(input=inp, token=token, Tout=Tout, name=name) # pylint: enable=protected-access return result if is_list_or_tuple else result[0]
def _internal_py_func(func, inp, Tout, stateful=None, eager=False, name=None): """See documentation for py_func and eager_py_func.""" is_list_or_tuple = False if isinstance(Tout, (list, tuple)): is_list_or_tuple = True else: Tout = [Tout] if eager: func = EagerFunc(func, Tout) token = _py_funcs.insert(func) # We tie the registered function's lifetime with the current default graph, # i.e., when the current graph is destroyed, we remove its py funcs. graph = ops.get_default_graph() # pylint: disable=protected-access while isinstance(graph, function._FuncGraph): # If the py_func was declared inside a _FuncGraph, its lifetime should be # bound to that of the outer graph instead. graph = graph._outer_graph cleanup = CleanupFunc(token) # TODO(zhifengc): Consider adding a Graph method to collect # `cleanup` objects in one of its member. if not hasattr(graph, "_cleanup_py_funcs_used_in_graph"): graph._cleanup_py_funcs_used_in_graph = [] # When `graph` is destroyed, elements in _cleanup_py_funcs_used_in_graph # will be destroyed and their __del__ will remove the 'token' from # the funcs registry. graph._cleanup_py_funcs_used_in_graph.append(cleanup) # pylint: enable=protected-access # pylint: disable=protected-access if eager: result = gen_script_ops._eager_py_func( input=inp, token=token, Tout=Tout, name=name) else: if stateful: result = gen_script_ops._py_func( input=inp, token=token, Tout=Tout, name=name) else: result = gen_script_ops._py_func_stateless( input=inp, token=token, Tout=Tout, name=name) # pylint: enable=protected-access return result if is_list_or_tuple else result[0]
def py_func(func, inp, Tout, stateful=True, name=None): """Wraps a python function and uses it as a TensorFlow op. Given a python function `func`, which takes numpy arrays as its inputs 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: ```python def my_func(x): # x will be a numpy array with the contents of the placeholder below return np.sinh(x) inp = tf.placeholder(tf.float32) y = tf.py_func(my_func, [inp], tf.float32) ``` **N.B.** The `tf.py_func()` 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.py_func()`. If you are using distributed TensorFlow, you must run a `tf.train.Server` in the same process as the program that calls `tf.py_func()` and you must pin the created operation to a device in that server (e.g. using `with tf.device():`). Args: func: A Python function, which accepts a list of NumPy `ndarray` objects having element types that match the corresponding `tf.Tensor` objects in `inp`, and returns a list of `ndarray` objects (or a single `ndarray`) having element types that match the corresponding values in `Tout`. inp: A list of `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. 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). Returns: A list of `Tensor` or a single `Tensor` which `func` computes. """ token = _py_funcs.insert(func) # We tie the registered function's life-time with the current # default graph. I.e., when the current graph is destroyed, we # should remove its py funcs. g = ops.get_default_graph() # pylint: disable=protected-access while isinstance(g, function._FuncGraph): # If the py_func was declared inside a _FuncGraph, its lifetime should be # bound to that of the outer graph instead. g = g._outer_graph cleanup = CleanupFunc(token) # TODO(zhifengc): Consider adding a Graph method to collect # `cleanup` objects in one of its member. if not hasattr(g, "_cleanup_py_funcs_used_in_graph"): g._cleanup_py_funcs_used_in_graph = [] # When g is destroyed, elements in _cleanup_py_funcs_used_in_graph # will be destroyed and their __del__ will remove the 'token' from # the funcs registry. g._cleanup_py_funcs_used_in_graph.append(cleanup) # pylint: enable=protected-access if isinstance(Tout, (list, tuple)): is_list_or_tuple = True else: Tout = [Tout] is_list_or_tuple = False # pylint: disable=protected-access if stateful: result = gen_script_ops._py_func(input=inp, token=token, Tout=Tout, name=name) else: result = gen_script_ops._py_func_stateless(input=inp, token=token, Tout=Tout, name=name) # pylint: enable=protected-access return result if is_list_or_tuple else result[0]
def py_func(func, inp, Tout, stateful=True, name=None): """Wraps a python function and uses it as a TensorFlow op. Given a python function `func`, which takes numpy arrays as its inputs 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: ```python def my_func(x): # x will be a numpy array with the contents of the placeholder below return np.sinh(x) inp = tf.placeholder(tf.float32) y = tf.py_func(my_func, [inp], tf.float32) ``` **N.B.** The `tf.py_func()` 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.py_func()`. If you are using distributed TensorFlow, you must run a `tf.train.Server` in the same process as the program that calls `tf.py_func()` and you must pin the created operation to a device in that server (e.g. using `with tf.device():`). Args: func: A Python function, which accepts a list of NumPy `ndarray` objects having element types that match the corresponding `tf.Tensor` objects in `inp`, and returns a list of `ndarray` objects (or a single `ndarray`) having element types that match the corresponding values in `Tout`. inp: A list of `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. 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). Returns: A list of `Tensor` or a single `Tensor` which `func` computes. """ token = _py_funcs.insert(func) # We tie the registered function's life-time with the current # default graph. I.e., when the current graph is destroyed, we # should remove its py funcs. cleanup = CleanupFunc(token) g = ops.get_default_graph() # pylint: disable=protected-access # # TODO(zhifengc): Consider adding a Graph method to collect # `cleanup` objects in one of its member. if not hasattr(g, "_cleanup_py_funcs_used_in_graph"): g._cleanup_py_funcs_used_in_graph = [] # When g is destroyed, elements in _cleanup_py_funcs_used_in_graph # will be destroyed and their __del__ will remove the 'token' from # the funcs registry. g._cleanup_py_funcs_used_in_graph.append(cleanup) if isinstance(Tout, (list, tuple)): is_list_or_tuple = True else: Tout = [Tout] is_list_or_tuple = False if stateful: result = gen_script_ops._py_func( input=inp, token=token, Tout=Tout, name=name) # pylint: enable=protected-access else: result = gen_script_ops._py_func_stateless( input=inp, token=token, Tout=Tout, name=name) # pylint: enable=protected-access return result if is_list_or_tuple else result[0]
def py_func(func, inp, Tout, stateful=True, name=None): """Wraps a python function and uses it as a tensorflow op. Given a python function `func`, which takes numpy arrays as its inputs and returns numpy arrays as its outputs. E.g., ```python def my_func(x): # x will be a numpy array with the contents of the placeholder below return np.sinh(x) inp = tf.placeholder(tf.float32, [...]) y = py_func(my_func, [inp], [tf.float32]) ``` The above snippet constructs a tf graph which invokes a numpy sinh(x) as an op in the graph. Args: func: A python function. inp: A list of `Tensor`. Tout: A list of tensorflow data types or a single tensorflow data type indicating what `func` returns. stateful: A boolean indicating whether the function should be considered stateful or stateless. I.e. whether it, given the same input, will return the same output and at the same time does not change state in an observable way. Optimizations such as common subexpression elimination are only possible when operations are stateless. name: A name for the operation (optional). Returns: A list of `Tensor` or a single `Tensor` which `func` computes. """ token = _py_funcs.insert(func) # We tie the registered function's life-time with the current # default graph. I.e., when the current graph is destroyed, we # should remove its py funcs. cleanup = CleanupFunc(token) g = ops.get_default_graph() # pylint: disable=protected-access # # TODO(zhifengc): Consider adding a Graph method to collect # `cleanup` objects in one of its member. if not hasattr(g, "_cleanup_py_funcs_used_in_graph"): g._cleanup_py_funcs_used_in_graph = [] # When g is destroyed, elements in _cleanup_py_funcs_used_in_graph # will be destroyed and their __del__ will remove the 'token' from # the funcs registry. g._cleanup_py_funcs_used_in_graph.append(cleanup) if isinstance(Tout, list): is_list = True else: Tout = [Tout] is_list = False if stateful: result = gen_script_ops._py_func( input=inp, token=token, Tout=Tout, name=name) # pylint: enable=protected-access else: result = gen_script_ops._py_func_stateless( input=inp, token=token, Tout=Tout, name=name) # pylint: enable=protected-access return result if is_list else result[0]