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构造函数及解构函数介绍
Feb 26 Python
Python实现的数据结构与算法之基本搜索详解
Apr 22 Python
Python基于win32ui模块创建弹出式菜单示例
May 09 Python
python定时任务 sched模块用法实例
Nov 04 Python
使用PyQt5实现图片查看器的示例代码
Apr 21 Python
python对一个数向上取整的实例方法
Jun 18 Python
python破解同事的压缩包密码
Oct 14 Python
用Python自动清理电脑内重复文件,只要10行代码(自动脚本)
Jan 09 Python
Keras保存模型并载入模型继续训练的实现
Feb 20 Python
Django 如何实现文件上传下载
Apr 08 Python
python 实现图与图之间的间距调整subplots_adjust
May 21 Python
使用Python开发冰球小游戏
Apr 30 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
通过ICQ网关发送手机短信的PHP源程序
2006/10/09 PHP
CI框架的安全性分析
2016/05/18 PHP
Yii2中使用asset压缩js,css文件的方法
2016/11/24 PHP
浅谈php中变量的数据类型判断函数
2017/03/04 PHP
Linux服务器下PHPMailer发送邮件失败的问题解决
2017/03/04 PHP
PHP中mysqli_get_server_version()的实例用法
2020/02/03 PHP
js href的用法
2010/05/13 Javascript
jquery异步请求实例代码
2011/06/21 Javascript
跨浏览器通用、可重用的选项卡tab切换js代码
2011/09/20 Javascript
如何利用JSHint减少JavaScript的错误
2016/08/23 Javascript
基于slideout.js实现移动端侧边栏滑动特效
2016/11/28 Javascript
详解React Native网络请求fetch简单封装
2017/08/10 Javascript
javascript实现QQ空间相册展示源码
2017/12/12 Javascript
JavaScript实现计算圆周率到小数点后100位的方法示例
2018/05/08 Javascript
Vue实现验证码功能
2019/12/03 Javascript
vue实现从外部修改组件内部的变量的值
2020/07/30 Javascript
Django发送html邮件的方法
2015/05/26 Python
Windows安装Python、pip、easy_install的方法
2017/03/05 Python
Python使用type关键字创建类步骤详解
2019/07/23 Python
python pillow模块使用方法详解
2019/08/30 Python
如何基于python生成list的所有的子集
2019/11/11 Python
flask框架json数据的拿取和返回操作示例
2019/11/28 Python
python在linux环境下安装skimage的示例代码
2020/10/14 Python
python基于win32api实现键盘输入
2020/12/09 Python
CSS3中Animation属性的使用详解
2015/08/06 HTML / CSS
美国高品质个性化珠宝销售网站:Jewlr
2018/05/03 全球购物
客服端调用EJB对象的几个基本步骤
2012/01/15 面试题
表演方阵解说词
2014/02/08 职场文书
超市商业计划书
2014/05/04 职场文书
学校献爱心活动总结
2014/07/08 职场文书
工程安全生产协议书
2014/11/21 职场文书
2015年事业单位工作总结
2015/04/27 职场文书
呼兰河传读书笔记
2015/06/30 职场文书
2016年党员公开承诺书范文
2016/03/24 职场文书
JavaScript小技巧带你提升你的代码技能
2021/09/15 Javascript
Spring Boot实战解决高并发数据入库之 Redis 缓存+MySQL 批量入库问题
2022/02/12 Redis