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 os模块学习笔记
Jun 21 Python
python脚本作为Windows服务启动代码详解
Feb 11 Python
python ftp 按目录结构上传下载的实现代码
Sep 12 Python
带你认识Django
Jan 15 Python
Python Numpy计算各类距离的方法
Jul 05 Python
Python求离散序列导数的示例
Jul 10 Python
python 两个数据库postgresql对比
Oct 21 Python
python通过nmap扫描在线设备并尝试AAA登录(实例代码)
Dec 30 Python
python如何将两张图片生成为全景图片
Mar 05 Python
Python实现爬取并分析电商评论
Jun 19 Python
浅析Python迭代器的高级用法
Jul 16 Python
Python装饰器如何实现修复过程解析
Sep 05 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
php5 apache 2.2 webservice 创建与配置(java)
2011/01/27 PHP
浅谈PHP中关于foreach使用引用变量的坑
2016/11/14 PHP
支持汉转拼和拼音分词的PHP中文工具类ChineseUtil
2018/02/23 PHP
PHP微信H5支付开发实例
2018/07/25 PHP
学习javascript,实现插入排序实现代码
2011/07/31 Javascript
jQuery之按钮组件的深入解析
2013/06/19 Javascript
jquery做的一个简单的屏幕锁定提示框
2014/03/26 Javascript
JS从数组中随机取出几个数组元素的方法
2016/08/02 Javascript
JS对大量数据进行多重过滤的方法
2016/11/04 Javascript
bootstrap table表格使用方法详解
2017/04/26 Javascript
解决option标签selected=&quot;selected&quot;属性失效的问题
2017/11/06 Javascript
vue 父组件中调用子组件函数的方法
2019/06/06 Javascript
解决Vue 给mapState中定义的属性赋值报错的问题
2020/06/22 Javascript
VSCode Vue开发推荐插件和VSCode快捷键(小结)
2020/08/08 Javascript
Python 流程控制实例代码
2009/09/25 Python
python多进程操作实例
2014/11/21 Python
python实现根据用户输入从电影网站获取影片信息的方法
2015/04/07 Python
Python中splitlines()方法的使用简介
2015/05/20 Python
对python的unittest架构公共参数token提取方法详解
2018/12/17 Python
Python实现从SQL型数据库读写dataframe型数据的方法【基于pandas】
2019/03/18 Python
Python中遍历列表的方法总结
2019/06/27 Python
Python 函数list&amp;read&amp;seek详解
2019/08/28 Python
python序列类型种类详解
2020/02/26 Python
CSS3盒子模型详解
2013/04/24 HTML / CSS
CSS3中的弹性布局em运用入门详解 1em等于多少像素
2021/02/08 HTML / CSS
悦木之源美国官网:Origins美国
2016/08/01 全球购物
世界上最大的在线旅行社新加坡网站:Expedia新加坡
2016/08/25 全球购物
几个人围成一圈的问题
2013/09/26 面试题
自动化专业职业生涯规划书范文
2014/01/16 职场文书
2014年两会学习心得体会
2014/03/17 职场文书
查摆剖析材料范文
2014/09/30 职场文书
安全承诺书
2015/01/19 职场文书
九寨沟导游词
2015/02/02 职场文书
明星邀请函
2015/02/02 职场文书
党员志愿者服务倡议书
2015/04/29 职场文书
2015年卫生监督工作总结
2015/05/21 职场文书