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基础之函数用法实例详解
Sep 10 Python
详解Python中open()函数指定文件打开方式的用法
Jun 04 Python
Python选课系统开发程序
Sep 02 Python
python数据处理 根据颜色对图片进行分类的方法
Dec 08 Python
理想高通滤波实现Python opencv示例
Jan 30 Python
Python 绘制酷炫的三维图步骤详解
Jul 12 Python
Python Web框架之Django框架cookie和session用法分析
Aug 16 Python
Python socket模块ftp传输文件过程解析
Nov 05 Python
Python lxml模块的基本使用方法分析
Dec 21 Python
Python装饰器的应用场景代码总结
Apr 10 Python
Keras框架中的epoch、bacth、batch size、iteration使用介绍
Jun 10 Python
Python基础学习之奇异的GUI对话框
May 27 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初学者们头痛的十四个问题
2007/01/15 PHP
PHP学习笔记 用户注册模块用户类以及验证码类
2011/09/20 PHP
php猜单词游戏
2015/09/29 PHP
php求数组全排列,元素所有组合的方法总结
2017/03/14 PHP
PHP的PDO连接讲解
2019/01/24 PHP
javascript 兼容鼠标滚轮事件
2009/04/07 Javascript
JS维吉尼亚密码算法实现代码
2010/11/09 Javascript
使用javascript实现简单的选项卡切换
2015/01/09 Javascript
jquery实现清新实用的网页菜单效果
2015/08/28 Javascript
AngularJS 单元测试(二)详解
2016/09/21 Javascript
JavaScript常用正则函数用法示例
2017/01/23 Javascript
详解Sea.js中Module.exports和exports的区别
2017/02/12 Javascript
详解使用PM2管理nodejs进程
2017/10/24 NodeJs
js实现复制功能(多种方法集合)
2018/01/06 Javascript
vue实现动态添加数据滚动条自动滚动到底部的示例代码
2018/07/06 Javascript
使用pm2部署node生产环境的方法步骤
2019/03/09 Javascript
js回文数的4种判断方法示例
2019/06/04 Javascript
JS使用for in有序获取对象数据
2020/05/19 Javascript
python字符串排序方法
2014/08/29 Python
Python单例模式实例详解
2017/03/01 Python
Python使用回溯法子集树模板解决迷宫问题示例
2017/09/01 Python
基于python requests库中的代理实例讲解
2018/05/07 Python
python简单验证码识别的实现方法
2019/05/10 Python
简单了解Python字典copy与赋值的区别
2020/09/16 Python
CSS3 伪类选择器 nth-child()说明
2010/07/10 HTML / CSS
美国在线鲜花速递:ProFlowers
2017/01/05 全球购物
Loreto Gallo英国:欧洲领先的在线药房
2021/01/21 全球购物
Oracle快照(snapshot)
2015/03/13 面试题
介绍一下UNIX启动过程
2013/11/14 面试题
大学生职业生涯规划书汇总
2014/03/20 职场文书
迎新晚会主持词
2014/03/24 职场文书
小学生寒假家长评语
2014/04/16 职场文书
药品营销专业毕业生自荐信
2014/07/02 职场文书
国庆节演讲稿范文2014
2014/09/19 职场文书
清明节寄语2015
2015/03/23 职场文书
2015年文员个人工作总结
2015/04/09 职场文书