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抓取豆瓣图片并自动保存示例学习
Jan 10 Python
Python处理JSON数据并生成条形图
Aug 05 Python
Python实现改变与矩形橡胶的线条的颜色代码示例
Jan 05 Python
PyQt5每天必学之布局管理
Apr 19 Python
python遍历一个目录,输出所有的文件名的实例
Apr 23 Python
Django 根据数据模型models创建数据表的实例
May 27 Python
远程部署工具Fabric详解(支持Python3)
Jul 04 Python
使用Pyinstaller转换.py文件为.exe可执行程序过程详解
Aug 06 Python
python脚本后台执行方式
Dec 21 Python
Python递归实现打印多重列表代码
Feb 27 Python
Jupyter notebook如何修改平台字体
May 13 Python
sublime3之内网安装python插件Anaconda的流程
Nov 10 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接受文件并获得其后缀名的方法
2015/08/05 PHP
ThinkPHP3.2.3实现分页的方法详解
2016/06/03 PHP
input、button的不同type值在ajax提交表单时导致的陷阱
2009/02/24 Javascript
扩展javascript的Date方法实现代码(prototype)
2010/11/20 Javascript
js精度溢出解决方案
2012/12/02 Javascript
原生JS可拖动弹窗效果实例代码
2013/11/09 Javascript
javascript创建和存储cookie示例
2014/01/07 Javascript
深入理解js函数的作用域与this指向
2016/05/28 Javascript
利用JavaScript阻止表单提交的两种方法
2016/08/11 Javascript
前端设计师们最常用的JS代码汇总
2016/09/25 Javascript
jQuery插件JWPlayer视频播放器用法实例分析
2017/01/11 Javascript
JavaScript中创建对象的7种模式详解
2017/02/21 Javascript
详解使用Visual Studio Code对Node.js进行断点调试
2017/09/14 Javascript
详解JS构造函数中this和return
2017/09/16 Javascript
Angularjs实现控制器之间通信方式实例总结
2018/03/27 Javascript
10行代码实现微信小程序滑动tab切换
2018/12/28 Javascript
python pickle 和 shelve模块的用法
2013/09/16 Python
通过python+selenium3实现浏览器刷简书文章阅读量
2017/12/26 Python
Python数据分析之双色球统计单个红和蓝球哪个比例高的方法
2018/02/03 Python
在Pycharm中使用GitHub的方法步骤
2019/06/13 Python
python调用支付宝支付接口流程
2019/08/15 Python
django框架单表操作之增删改实例分析
2019/12/16 Python
jupyter notebook 恢复误删单元格或者历史代码的实现
2020/04/17 Python
DjangoWeb使用Datatable进行后端分页的实现
2020/05/18 Python
Python HTMLTestRunner库安装过程解析
2020/05/25 Python
ASP.NET Core中的配置详解
2021/02/05 Python
Joie官方网上商店:购买服装和女装配饰
2018/06/05 全球购物
物流管理毕业生自荐信范文
2014/03/15 职场文书
卫生巾广告词
2014/03/18 职场文书
销售提升方案
2014/06/07 职场文书
反四风个人对照检查材料
2014/09/26 职场文书
2015年防灾减灾工作总结
2015/07/24 职场文书
2016大学军训心得体会
2016/01/11 职场文书
2016大学生求职自荐信范文
2016/01/28 职场文书
压缩Redis里的字符串大对象操作
2021/06/23 Redis
HTML+JS实现在线朗读器
2022/02/15 Javascript