tensorflow2.0的函数签名与图结构(推荐)


Posted in Python onApril 28, 2020

input_signature的好处:

1.可以限定函数的输入类型,以防止调用函数时调错,

2.一个函数有了input_signature之后,在tensorflow里边才可以保存成savedmodel。在保存成savedmodel的过程中,需要使用get_concrete_function函数把一个tf.function标注的普通的python函数变成带有图定义的函数。

下面的代码具体体现了input_signature可以限定函数的输入类型这一作用。

@tf.function(input_signature=[tf.TensorSpec([None], tf.int32, name='x')])
def cube(z): #实现输入的立方
 return tf.pow(z, 3)
try:
 print(cube(tf.constant([1., 2., 3.])))
except ValueError as ex:
 print(ex)
print(cube(tf.constant([1, 2, 3])))

输出:

Python inputs incompatible with input_signature:
  inputs: (
    tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32))
  input_signature: (
    TensorSpec(shape=(None,), dtype=tf.int32, name='x'))
tf.Tensor([ 1  8 27], shape=(3,), dtype=int32)

get_concrete_function的使用

note:首先说明,下面介绍的函数在模型构建、模型训练的过程中不会用到,下面介绍的函数主要用在两个地方:1、如何保存模型 2、保存好模型后,如何载入进来。

可以给 由@tf.function标注的普通的python函数,给它加上input_signature, 从而让这个python函数变成一个可以保存的tensorflow图结构(SavedModel)

举例说明函数的用法:

@tf.function(input_signature=[tf.TensorSpec([None], tf.int32, name='x')])
def cube(z):
 return tf.pow(z, 3)
 
try:
 print(cube(tf.constant([1., 2., 3.])))
except ValueError as ex:
 print(ex)
 
print(cube(tf.constant([1, 2, 3])))
 
# @tf.function py func -> tf graph
# get_concrete_function -> add input signature -> SavedModel
 
cube_func_int32 = cube.get_concrete_function(
 tf.TensorSpec([None], tf.int32)) #tensorflow的类型
print(cube_func_int32)

输出:

<tensorflow.python.eager.function.ConcreteFunction object at 0x00000240E29695C0>

从输出结果可以看到:调用get_concrete_function函数后,输出的是一个ConcreteFunction对象

#看用新参数获得的对象与原来的对象是否一样
print(cube_func_int32 is cube.get_concrete_function(
 tf.TensorSpec([5], tf.int32))) #输入大小为5
print(cube_func_int32 is cube.get_concrete_function(
 tf.constant([1, 2, 3]))) #传具体数据

输出:

True
True

cube_func_int32.graph #图定义

输出:

[<tf.Operation 'x' type=Placeholder>,
 <tf.Operation 'Pow/y' type=Const>,
 <tf.Operation 'Pow' type=Pow>,
 <tf.Operation 'Identity' type=Identity>]
pow_op = cube_func_int32.graph.get_operations()[2]
print(pow_op)

输出:

name: "Pow"
op: "Pow"
input: "x"
input: "Pow/y"
attr {
  key: "T"
  value {
    type: DT_INT32
  }
}

print(list(pow_op.inputs))
print(list(pow_op.outputs))

输出:

[<tf.Tensor 'x:0' shape=(None,) dtype=int32>, <tf.Tensor 'Pow/y:0' shape=() dtype=int32>]
[<tf.Tensor 'Pow:0' shape=(None,) dtype=int32>]

cube_func_int32.graph.get_operation_by_name("x")

输出:

<tf.Operation 'x' type=Placeholder>

cube_func_int32.graph.get_tensor_by_name("x:0")  #默认加“:0”

<tf.Tensor 'x:0' shape=(None,) dtype=int32>

cube_func_int32.graph.as_graph_def() #总名字,针对上面两个

node {
 name: "x"
 op: "Placeholder"
 attr {
 key: "_user_specified_name"
 value {
 s: "x"
 }
 }
 attr {
 key: "dtype"
 value {
 type: DT_INT32
 }
 }
 attr {
 key: "shape"
 value {
 shape {
 dim {
  size: -1
 }
 }
 }
 }
}
node {
 name: "Pow/y"
 op: "Const"
 attr {
 key: "dtype"
 value {
 type: DT_INT32
 }
 }
 attr {
 key: "value"
 value {
 tensor {
 dtype: DT_INT32
 tensor_shape {
 }
 int_val: 3
 }
 }
 }
}
node {
 name: "Pow"
 op: "Pow"
 input: "x"
 input: "Pow/y"
 attr {
 key: "T"
 value {
 type: DT_INT32
 }
 }
}
node {
 name: "Identity"
 op: "Identity"
 input: "Pow"
 attr {
 key: "T"
 value {
 type: DT_INT32
 }
 }
}
versions {
 producer: 119
}

 到此这篇关于tensorflow2.0的函数签名与图结构的文章就介绍到这了,更多相关tensorflow函数签名与图结构内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python pickle类库介绍(对象序列化和反序列化)
Nov 21 Python
给Python的Django框架下搭建的BLOG添加RSS功能的教程
Apr 08 Python
在Linux系统上安装Python的Scrapy框架的教程
Jun 11 Python
python字典键值对的添加和遍历方法
Sep 11 Python
python使用threading获取线程函数返回值的实现方法
Nov 15 Python
解决nohup重定向python输出到文件不成功的问题
May 11 Python
Python常见MongoDB数据库操作实例总结
Jul 24 Python
python监控进程状态,记录重启时间及进程号的实例
Jul 15 Python
解决pycharm 安装numpy失败的问题
Dec 05 Python
Python3读写Excel文件(使用xlrd,xlsxwriter,openpyxl3种方式读写实例与优劣)
Feb 13 Python
推荐8款常用的Python GUI图形界面开发框架
Feb 23 Python
使用Keras构造简单的CNN网络实例
Jun 29 Python
Python startswith()和endswith() 方法原理解析
Apr 28 #Python
Python如何将函数值赋给变量
Apr 28 #Python
Python多线程thread及模块使用实例
Apr 28 #Python
Python基于模块Paramiko实现SSHv2协议
Apr 28 #Python
Python内置函数locals和globals对比
Apr 28 #Python
使用python实现CGI环境搭建过程解析
Apr 28 #Python
基于python连接oracle导并出数据文件
Apr 28 #Python
You might like
PHP_MySQL教程-第一天
2007/03/18 PHP
PHP编写简单的App接口
2016/08/28 PHP
php利用ffmpeg提取视频中音频与视频画面的方法详解
2017/06/07 PHP
php设计模式之状态模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
基于jquery自定义图片热区效果
2012/07/21 Javascript
EASYUI TREEGRID异步加载数据实现方法
2012/08/22 Javascript
JavaScript修改浏览器tab标题小技巧
2015/01/06 Javascript
浅谈setTimeout 与 setInterval
2015/06/23 Javascript
第一次动手实现bootstrap table分页效果
2016/09/22 Javascript
Javascript实现图片懒加载插件的方法
2016/10/20 Javascript
微信小程序 跳转传递数据的实例
2017/07/06 Javascript
关于vue.js组件数据流的问题
2017/07/26 Javascript
node.js学习之断言assert的使用示例
2017/09/28 Javascript
快速了解Vue父子组件传值以及父调子方法、子调父方法
2020/07/15 Javascript
js+css3实现炫酷时钟
2020/08/18 Javascript
[06:45]2018DOTA2亚洲邀请赛 4.5 SOLO赛 Sccc vs Maybe
2018/04/06 DOTA
[56:48]FNATIC vs EG 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/16 DOTA
教你安装python Django(图文)
2013/11/04 Python
python基于urllib实现按照百度音乐分类下载mp3的方法
2015/05/25 Python
Python使用pylab库实现画线功能的方法详解
2017/06/08 Python
对Python 检查文件名是否规范的实例详解
2019/06/10 Python
OpenCV+face++实现实时人脸识别解锁功能
2019/08/28 Python
Python 批量刷博客园访问量脚本过程解析
2019/08/30 Python
python中设置超时跳过,超时退出的方式
2019/12/13 Python
完美解决pycharm 不显示代码提示问题
2020/06/02 Python
纽约服装和生活方式品牌:Saturdays NYC
2017/08/13 全球购物
Turnbull & Asser官网:英国皇室御用的顶级定制衬衫
2019/01/31 全球购物
某公司C#程序员面试题笔试题
2014/05/26 面试题
设计模式的基本要素是什么
2014/04/21 面试题
企业申诉管理制度
2014/01/30 职场文书
2014政务公开实施方案
2014/02/19 职场文书
优秀学生评语大全
2014/04/25 职场文书
公司股东合作协议书
2014/09/14 职场文书
优秀教师单行材料
2014/12/16 职场文书
领导激励员工的演讲稿,各种会上用得到,建议收藏
2019/08/13 职场文书
python Django框架快速入门教程(后台管理)
2021/07/21 Python