tf.py_func的一些使用笔记——TensorFlow1.x
tensorflow.py_func是TensorFlow1.x版本下的函数,在TensorFlow.2.x已经不建议使用了,但是依然可以通过tf.compat.v1.py_func的方式来进行调用。
可以说TensorFlow1.x下的py_func函数在TensorFlow2.x下除了通过tf.compat.v1.py_func的方式来进行调用就再也没有等价的使用方法了,具体可以看TensorFlow2.x的API文档:
https://tensorflow.google.cn/api_docs/python/tf/compat/v1/py_func

----------------------------------------------------------
这里需要着重说明一点,很多人认为TensorFlow1.x中的tf.py_func等价于TensorFlow2.x中的tf.py_function,其实不然。在TensorFlow2.x中除了对tf.py_func进行v1版本保留和兼容的tf.compat.v1.py_func,其实是没有完全同tf.py_func等价的函数。如果说在 TensorFlow2.x中 比较相近的函数应该是tf.numpy_function而不是tf.py_function。
在TensorFlow2.x中对tf.numpy_function的解释:
https://tensorflow.google.cn/api_docs/python/tf/numpy_function
在TensorFlow2.x中对tf.py_function的解释:
https://tensorflow.google.cn/api_docs/python/tf/py_function
----------------------------------------------------------
TensorFlow2.x中 tf.numpy_function 和 TensorFlow1.x中 tf.py_func(tf.compat.v1.py_func)中唯一的区别是:
tf.py_func中是可以设置函数是否考虑状态的,而tf.numpy_function中是必须要考虑状态的(没有定义不考虑状态的设置)。
This name was deprecated and removed in TF2, but tf.numpy_function is a near-exact replacement, just drop the stateful argument (all tf.numpy_function calls are considered stateful).
=============================================
对TensorFlow1.x中 tf.py_func进行下一步解释:
tf.py_func其实是将python函数包装成TensorFlow的一个操作operation,tf.py_func的输入可以是numpy,可以是tensor,也可以是Variable,其输入只能是tensor。
tf.py_func定义的操作是属于TensorFlow的计算图的,在定义tf.py_func时是不会具体执行的,只有在具体的tf.Session中还可以执行,但是tf.py_func并不同于其他的TensorFlow的operation,因为tf.py_func定义的操作是运行在python空间下的而不是运行在TensorFlow空间下的。
tf.py_func定义后,在session中运行时的基本原理就是将输入的变量(不论是tensor还是numpy.array)转换为python空间下的numpy.array变量,在经过numpy运算后在将获得的numpy.array结果转换为tensor,给到TensorFlow的计算图。
其实,tf.py_func的功能完全可以手动实现类似的,就是手动的把tensor变量转为numpy.array,然后运算好后把结果手动转为tensor,tf.py_func最大的好处就是把这一过程给自动化了,不过随之也使这个运算过程变得难以理解了。从tf.py_func的原理我们就可以知道,虽然tf.py_func可以作为TensorFlow计算图的一部分挂在计算图上,但是由于其本质是将TensorFlow空间变量转为python空间变量后经过运算再转为TensorFlow空间变量,中间经过了命名空间和运算空间的转换,因此tf.py_func是不可以进行梯度反传的,或许我们更可以把这个操作看做是一种简易的为TensorFlow提供支持的python库。
================================================
2022年10月13日更新
如果tf.py_func包装的python函数的参数是string类型,那么传到包装的函数内时会被自动转为bytes类型,也就是string变bytes,这一点需要注意,否则真的是不知道什么地方报错的。
例子:
import tensorflow as tf
import numpy as np sess = tf.Session() def fun(a, b):
print("+"*30)
print("function fun excute!!!")
print(a, b)
return np.array(len(a+b), dtype=np.float32) x = "abc"
y = "bde"
ans = tf.py_func(fun, (x, y), (tf.float32, ), name="ab_op")
print("="*30, "result:")
print(ans) print(sess.run(ans))
运行结果:

可以看到,由tensorflow空间传参到python空间会自动的将string类型转为bytes类型。
在python3.x版本中,可以使用bytes.decode()的方法将传入的bytes类型转会string类型,具体:
修改后的代码:

import tensorflow as tf
import numpy as np sess = tf.Session() def fun(a, b):
print("+"*30)
print("function fun excute!!!")
print(a, b)
a = a.decode()
b = b.decode()
print(a, b)
return np.array(len(a+b), dtype=np.float32) x = "abc"
y = "bde"
ans = tf.py_func(fun, (x, y), (tf.float32, ), name="ab_op")
print("="*30, "result:")
print(ans) print(sess.run(ans))
重点部分:

================================================
一些例子:
以下代码均为TensorFlow1.x版本:
import tensorflow as tf
import numpy as np sess = tf.Session() def fun(a, b):
print("+"*30)
print("function fun excute!!!")
return a+1, b+1 x = np.array([1.0,2.0,3.0], dtype=np.float32)
y = np.array([4.0,5.0,6.0], dtype=np.float32) ans=tf.py_func(fun, (x, y), (tf.float32, tf.float32), name="ab_python") print("="*30, "result:")
print(ans)
print(sess.run(ans))
运行结果:

可以看到,tf.py_func的执行其实是为TensorFlow定义了一个operation,而tf.py_func所包装的python函数只有在TensorFlow执行计算图的时候才会被真正执行。
tf.py_func为包装的python函数所传入的参数可以是numpy.array类型,也可以是tensor类型,也可以是Variable类型,但是不管在tf.py_func中传入的参数是什么类型,最后传入到所包装的python函数中都会被转为numpy.array类型,而包装后的函数在session开始执行后所返回给计算图的数据类型也会被转换为tensor类型。
--------------------------------------------------------
包装的参数为tensor:
import tensorflow as tf
import numpy as np sess = tf.Session() def fun(a, b):
print("+"*30)
print("function fun excute!!!")
return a+1, b+1 x = tf.constant([1.0,2.0,3.0], dtype=np.float32)
y = tf.constant([4.0,5.0,6.0], dtype=np.float32)
ans =tf.py_func(fun, (x, y), (tf.float32, tf.float32), name="ab_op") print("="*30, "result:")
print(ans)
print("session is running!!!")
print(sess.run(ans))
运行结果:

--------------------------------------------------------
包装的参数为Variable:
import tensorflow as tf
import numpy as np sess = tf.Session() def fun(a, b):
print("+"*30)
print("function fun excute!!!")
return a+1, b+1 x = tf.Variable([1.0,2.0,3.0], dtype=np.float32)
y = tf.Variable([4.0,5.0,6.0], dtype=np.float32)
ans = tf.py_func(fun, (x, y), (tf.float32, tf.float32), name="ab_op")
print("="*30, "result:")
print(ans) sess.run(tf.global_variables_initializer())
print(sess.run(ans))
运行结果:

---------------------------
包装的参数为Variable:
import tensorflow as tf
import numpy as np sess = tf.Session() def fun(a, b):
print("+"*30)
print("function fun excute!!!")
return a+b x = tf.Variable([1.0,2.0,3.0], dtype=np.float32)
y = tf.Variable([4.0,5.0,6.0], dtype=np.float32)
ans = tf.py_func(fun, (x, y), (tf.float32, ), name="ab_op")
print("="*30, "result:")
print(ans) sess.run(tf.global_variables_initializer())
print(sess.run(ans))
运行结果:

---------------------------------------------
包装的参数为Variable,求反传梯度报错:
import tensorflow as tf
import numpy as np sess = tf.Session() def fun(a, b):
print("+"*30)
print("function fun excute!!!")
return a+b x = tf.Variable([1.0,2.0,3.0], dtype=np.float32)
y = tf.Variable([4.0,5.0,6.0], dtype=np.float32)
x2 = tf.Variable([1.0,2.0,3.0], dtype=np.float32)
y2 = tf.Variable([4.0,5.0,6.0], dtype=np.float32)
ans = tf.py_func(fun, (x, y), (tf.float32, ), name="ab_op")
ans2 = x2 + y2
print("="*30, "result:")
print(ans) sess.run(tf.global_variables_initializer())
print(sess.run(ans)) op2 = tf.gradients(ans2, (x2, y2))
print("Ops2 Gradients: \n", sess.run(op2)) op = tf.gradients(ans, (x, y))
print("Ops Gradients: \n", sess.run(op))
运行结果:

证明:
tf.py_func包装后的函数是不可以进行反传的。
其实,tf.py_func就是在tensorflow计算图执行的时候调用python代码,而调用python代码时运行在python的代码空间中,自然是不支持反传的。
==================================================
tf.py_func的一些使用笔记——TensorFlow1.x的更多相关文章
- tf.py_func
在 faster rcnn的tensorflow 实现中看到这个函数 rois,rpn_scores=tf.py_func(proposal_layer,[rpn_cls_prob,rpn_bbox ...
- Tensorflow之调试(Debug) && tf.py_func()
Tensorflow之调试(Debug)及打印变量 tensorflow调试tfdbg 几种常用方法: 1.通过Session.run()获取变量的值 2.利用Tensorboard查看一些可视化统计 ...
- 使用多块GPU进行训练 1.slim.arg_scope(对于同等类型使用相同操作) 2.tf.name_scope(定义名字的范围) 3.tf.get_variable_scope().reuse_variable(参数的复用) 4.tf.py_func(构造函数)
1. slim.arg_scope(函数, 传参) # 对于同类的函数操作,都传入相同的参数 from tensorflow.contrib import slim as slim import te ...
- tf.contrib.layers.fully_connected参数笔记
tf.contrib.layers.fully_connected 添加完全连接的图层. tf.contrib.layers.fully_connected( inputs, num_ou ...
- tf.split函数的用法(tensorflow1.13.0)
tf.split(input, num_split, dimension): dimension指输入张量的哪一个维度,如果是0就表示对第0维度进行切割:num_split就是切割的数量,如果是2就表 ...
- TensorFlow学习笔记(一):数据操作指南
扩充 TensorFlow tf.tile 对数据进行扩充操作 import tensorflow as tf temp = tf.tile([1,2,3],[2]) temp2 = tf.tile( ...
- tf.data
以往的TensorFLow模型数据的导入方法可以分为两个主要方法,一种是使用feed_dict另外一种是使用TensorFlow中的Queues.前者使用起来比较灵活,可以利用Python处理各种输入 ...
- tf调试函数
Tensorflow之调试(Debug)及打印变量 参考资料:https://wookayin.github.io/tensorflow-talk-debugging 几种常用方法: 1.通过Se ...
- R2CNN项目部分代码学习
首先放出大佬的项目地址:https://github.com/yangxue0827/R2CNN_FPN_Tensorflow 那么从输入的数据开始吧,输入的数据要求为tfrecord格式的数据集,好 ...
- tensorflow_目标识别object_detection_api,RuntimeError: main thread is not in main loop,fig = plt.figure(frameon=False)_tkinter.TclError: no display name and no $DISPLAY environment variable
最近在使用目标识别api,但是报错了: File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/script_o ...
随机推荐
- 各种语言的OEP大全
Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解` 各种语言的OEP大全 日期:2017-5-19 阿珏 教程 ...
- C# 语言在AGI 赛道上能做什么
自从2022年11月OpenAI正式对外发布ChatGPT依赖,AGI 这条赛道上就挤满了重量级的选手,各大头部公司纷纷下场布局.原本就在机器学习.深度学习领域占据No.1的Python语言更是继续稳 ...
- QT6设置应用程序图标
准备好一个ico格式的图标, 放到源码文件中, 比如放在 resources/logo.ico 在源码目录中新建一个icon.rc的文件, 内容如下: IDI_ICON1 ICON DISCARDAB ...
- 基于cifar数据集合成含开集、闭集噪声的数据集
前言 噪声标签学习下的一个任务是:训练集上存在开集噪声和闭集噪声:然后在测试集上对闭集样本进行分类. 训练集中被加入的开集样本,会被均匀得打上闭集样本的标签充当开集噪声:而闭集噪声的设置与一般的噪声标 ...
- 三层交换机vlan间路由
sw1: [Huawei]vlan batch 10 20 [Huawei]int e0/0/1 [Huawei-Ethernet0/0/1]port link-type access [Huawei ...
- python3 安装pyodbc失败 pip3 install pyodbc
python3 安装pyodbc失败 报错1: 关键报错信息: fatal error: sql.h: No such file or directory [root@centfos python3 ...
- [HDCTF 2023]BabyMisc
BabyMisc ...脑洞坑题(如果7z密码不是那一串超长字符串真不至于0解) 先打开Script.zip,随便打开一个文件夹,得到的是pxx的文件,内容为16进制字节.猜测pxx为对应字节的位置 ...
- Java 自定义注解校验字段唯一性
业务场景 在项目中,某些情景下我们需要验证编码是否重复,账号是否重复,身份证号是否重复等... 那么有没有办法可以解决这类似的重复代码量呢? 我们可以通过自定义注解校验的方式去实现,在实体类上面加上自 ...
- Pypi配置API Token
技术背景 在许久之前写的一篇博客中,我们介绍过使用twine向pypi上传我们自己的开源包的方法.最近发现这个方法已经不再支持了(报错信息如下所示),现在最新版需要使用API Token进行文件上传, ...
- 文件系统(九):一文看懂yaffs2文件系统原理
liwen01 2024.07.07 前言 yaffs 是专为nand flash 设计的一款文件系统,与jffs 类似,都是属于日志结构文件系统.与jffs 不同的是,yaffs 文件系统利用了na ...