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 ...
随机推荐
- python生成随机四位数和AttributeError: module 'random' has no attribute 'sample'
python生成随机四位数和AttributeError: module 'random' has no attribute 'sample' ## AttributeError: module 'r ...
- python 发起PUT请求,报"Method not Allowed" 和 取返回的报文的内容
发起请求的时候,默认使用的POST请求方式,导致发起请求,返回[405 Method not Allowed ],检查此更新接口的请求方式为PUT,更改请求方式为PUT PUT接口返回的内容,不能通过 ...
- 用基础Array数组实现动态数组、链表、栈和队列
代码地址: https://gitee.com/Tom-shushu/Algorithm-and-Data-Structure.git 一.ArrayList自定义封装 package com.zho ...
- 个人团队兼职开发app(社交,语聊1v1,视频直播)
如果您有意向创业,意向社交类产品,如语聊,及时通信,视频直播,1v1等,又苦无没有人力资源. 我们岁数都是30+,在互联网行业摸爬滚打十年有余. 后端,前端,客户端,运维,四个人. 我们共事很长一段时 ...
- 2个qubit的量子门
量子计算机就是基于单qubit门和双qubit门的,再多的量子操作都是基于这两种门.双qubit门比单qubit门难理解得多,不过也重要得多.它可以用来创建纠缠,没有纠缠,量子机就不可能有量子霸权. ...
- new操作符具体干了什么呢?
new操作符的作用如下: 1.创建一个空对象2.由this变量引用该对象3.该对象继承该函数的原型4.把属性和方法加入到this引用的对象中5.新创建的对象由this引用,最后隐式地返回this.过程 ...
- Mac 版本10.15.4 安装 telnel工具
下载脚本 mac新版本安装telnel发生的变化,进入下面的链接,右键另存为,保存到桌面 https://raw.githubusercontent.com/Homebrew/install/mast ...
- SpringBoot彩蛋之定制启动画面
写在前面 在日常开发中,我们经常会看到各种各样的启动画面.例如以下几种 ① spring项目启动画面 ② mybatisplus启动画面 ③若依项目启动画面 还有很多各式各样好看的启动画面,那么怎么定 ...
- vue2.x版本升级2.7版本
2022年7月1日,vue正式迎来2.7版本,代号:"Naruto".支持 Composition API + <script setup> .原文链接 也就是说,你可 ...
- 洛谷P2430
还是很容易能看出来是01背包 #include<iostream> #include<utility> using namespace std; typedef long lo ...