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实现超简单端口转发的方法
Mar 13 Python
python操作 hbase 数据的方法
Dec 18 Python
python3.0 模拟用户登录,三次错误锁定的实例
Nov 02 Python
Python实现自定义顺序、排列写入数据到Excel的方法
Apr 23 Python
基于python3 OpenCV3实现静态图片人脸识别
May 25 Python
解决Python2.7中IDLE启动没有反应的问题
Nov 30 Python
Python进阶之全面解读高级特性之切片
Feb 19 Python
Django框架模板语言实例小结【变量,标签,过滤器,继承,html转义】
May 23 Python
使用python telnetlib批量备份交换机配置的方法
Jul 25 Python
如何在django中添加日志功能
Feb 06 Python
解决Python3.8用pip安装turtle-0.0.2出现错误问题
Feb 11 Python
Python中的xlrd模块使用整理
Jun 15 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面向对象全攻略 (十六) 对象的串行化
2009/09/30 PHP
详解PHP中instanceof关键字及instanceof关键字有什么作用
2015/11/05 PHP
PHP浮点比较大小的方法
2016/02/14 PHP
jQuery 浮动导航菜单适合购物商品类型的网站
2014/09/09 Javascript
javascript图片预加载实例分析
2015/07/16 Javascript
20分钟轻松创建自己的Bootstrap站点
2016/05/12 Javascript
bootstrap-datetimepicker实现只显示到日期的方法
2016/11/25 Javascript
最常用的jQuery表单验证(简单)
2017/05/23 jQuery
Vue组件实例间的直接访问实现代码
2017/08/20 Javascript
深入理解 JS 垃圾回收
2019/06/03 Javascript
解决VueCil代理本地proxytable无效报错404的问题
2020/11/07 Javascript
vue 获取url参数、get参数返回数组的操作
2020/11/12 Javascript
python实现连接mongodb的方法
2015/05/08 Python
Python编程把二叉树打印成多行代码
2018/01/04 Python
python 读取.csv文件数据到数组(矩阵)的实例讲解
2018/06/14 Python
Python中的heapq模块源码详析
2019/01/08 Python
Python实现的调用C语言函数功能简单实例
2019/03/13 Python
django admin后台添加导出excel功能示例代码
2019/05/15 Python
Python检测数据类型的方法总结
2019/05/20 Python
Python函数参数类型及排序原理总结
2019/12/19 Python
深入浅析CSS3中的Flex布局整理
2020/04/27 HTML / CSS
利用简洁的图片预加载组件提升html5移动页面的用户体验
2016/03/11 HTML / CSS
就业自荐信
2013/12/04 职场文书
门卫人员岗位职责
2013/12/24 职场文书
生日宴会答谢词
2014/01/09 职场文书
信息科学与技术专业求职信范文
2014/02/20 职场文书
跳蚤市场口号
2014/06/13 职场文书
装修施工安全责任书
2014/07/24 职场文书
解除劳动合同证明书模板
2014/11/20 职场文书
作文评语怎么写
2014/12/25 职场文书
员工工作表现自我评价
2015/03/06 职场文书
教师见习总结范文
2015/06/23 职场文书
《卖火柴的小女孩》教学反思
2016/02/19 职场文书
用Python爬取英雄联盟的皮肤详细示例
2021/12/06 Python
vue实现书本翻页动画效果实例详解
2022/04/08 Vue.js
利用nginx搭建RTMP视频点播、直播、HLS服务器
2022/05/25 Servers