tensorflow程序分为构图和执行两部分。构图完成后,再创建Session,session创建后再不能构图。tensorflow使用python代码来构图。实际上在图执行的时候,是不执行python代码的,执行的tensorflow的op。而py_func的作用是是把python函数打包成tensorflow的op.
>>> help(tf.py_func)
Help on function py_func in module tensorflow.python.ops.script_ops:
py_func(func, inp, Tout, stateful=True, name=None)
Wraps a python function and uses it as a TensorFlow op. 把python函数打包成tensorflow成Op.
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. 要求补打包的python函数输入输出是num数组。
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)
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:
* 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. python函数不会被序列化入模型
* The operation must run in the same address space as the Python program 被打包的操作执行代码必须和调用py_func的python程序在同一进程中。
that calls `tf.compat.v1.py_func()`. If you are using distributed
TensorFlow, you
must run a `tf.distribute.Server` in the same process as the program that
calls
`tf.compat.v1.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 `ndarray` objects as arguments and
returns a list of `ndarray` objects (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 `tf.Tensor` objects in `inp`.
The returns `ndarray`s must match the number and types defined `Tout`.
important Note: Input and output numpy `ndarray`s of `func` are not
guaranteed to be copies. In some cases their underlying memory will be
shared with the corresponding TensorFlow tensors. In-place modification
or storing `func` input or return values in python datastructures
without explicit (np.)copy can have non-deterministic consequences.
inp: A list of `Tensor` objects. 只是tensor列表。不能是sparse tensor
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. tf.py_func返回的是一个tensor,或者一个tensor列表
样例代码
import tensorflow.compat.v1 as tf
import numpy as np
tf.disable_v2_behavior()
print(tf.__version__)
print(tf.py_func)
def my_func(x,y):
# x will be a numpy array with the contents of the placeholder below
print("sin", y) #print变成了op的代码,所以会在图执行时运行
return np.sinh(x)
input = tf.compat.v1.placeholder(tf.float32)
sparse = tf.SparseTensor(indices=[(1,1)], values=[2.0], dense_shape=(2,2)) #sparse不能作为py_func的参数
print(sparse.values.shape)
y = tf.compat.v1.py_func(my_func, [input, sparse.values], tf.float32)
print(y)
with tf.Session() as sess:
print(sess.run(y, feed_dict={input:[0.1,0.2,0.3,0.4]}))
print(sess.run(y, feed_dict={input:[0.1,0.2,0.3,0.4]}))
#输出结果
2.3.0
(1,)
Tensor("PyFunc_13:0", dtype=float32)
sin [2.]
[0.10016675 0.20133601 0.3045203 0.41075233]
sin [2.]
[0.10016675 0.20133601 0.3045203 0.41075233]



