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 enumerate遍历数组示例应用
Sep 06 Python
python实现多线程暴力破解登陆路由器功能代码分享
Jan 04 Python
使用Python脚本将Bing的每日图片作为桌面的教程
May 04 Python
详解Python操作RabbitMQ服务器消息队列的远程结果返回
Jun 30 Python
安装Python的教程-Windows
Jul 22 Python
PyQt5实现让QScrollArea支持鼠标拖动的操作方法
Jun 19 Python
python正则-re的用法详解
Jul 28 Python
Python 实现一个手机号码获取妹子名字的功能
Sep 25 Python
Pandas数据离散化原理及实例解析
Nov 16 Python
Tensorflow设置显存自适应,显存比例的操作
Feb 03 Python
Python图像处理二值化方法实例汇总
Jul 24 Python
Python帮你解决手机qq微信内存占用太多问题
Feb 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
MySQL数据源表结构图示
2008/06/05 PHP
使用PHP 5.0创建图形的巧妙方法
2010/10/12 PHP
PHP 通过Socket收发十六进制数据的实现代码
2013/08/16 PHP
php生成4位数字验证码的实现代码
2015/11/23 PHP
php正则表达式基本知识与应用详解【经典教程】
2017/04/17 PHP
ThinkPHP开发--使用七牛云储存
2017/09/14 PHP
PHP登录验证功能示例【用户名、密码、验证码、数据库、已登陆验证、自动登录和注销登录等】
2019/02/25 PHP
索趣科技的答案
2007/02/07 Javascript
总结AJAX相关JS代码片段和浏览器模型
2007/08/15 Javascript
类似GMAIL的Ajax信息反馈显示
2010/02/16 Javascript
jquery 事件对象属性小结
2010/04/27 Javascript
JQuery Study Notes 学习笔记(一)
2010/08/04 Javascript
Extjs3.0 checkboxGroup 动态添加item实现思路
2013/08/14 Javascript
Javascript保存网页为图片借助于html2canvas库实现
2014/09/05 Javascript
封装了jQuery的Ajax请求全局配置
2015/02/05 Javascript
JavaScript获取ul中li个数的方法
2017/02/13 Javascript
Nodejs之http的表单提交
2017/07/07 NodeJs
Javascript中toFixed计算错误(依赖银行家舍入法的缺陷)解决方法
2017/08/22 Javascript
Vue.js中provide/inject实现响应式数据更新的方法示例
2019/10/16 Javascript
Python ORM框架SQLAlchemy学习笔记之关系映射实例
2014/06/10 Python
Python标准库之循环器(itertools)介绍
2014/11/25 Python
Python爬虫爬取美剧网站的实现代码
2016/09/03 Python
用Python删除本地目录下某一时间点之前创建的所有文件的实例
2017/12/14 Python
python的re正则表达式实例代码
2018/01/24 Python
Python2.7.10以上pip更新及其他包的安装教程
2018/06/12 Python
利用anaconda保证64位和32位的python共存
2021/03/09 Python
Linux下远程连接Jupyter+pyspark部署教程
2019/06/21 Python
python取余运算符知识点详解
2019/06/27 Python
Softmax函数原理及Python实现过程解析
2020/05/22 Python
Hush Puppies澳大利亚官网:舒适的男女休闲和正装鞋
2019/08/24 全球购物
医药学专业大学生职业生涯规划书论文
2014/01/21 职场文书
技术比武方案
2014/05/19 职场文书
关于保护环境的建议书
2014/08/26 职场文书
科学发展观演讲稿
2014/09/11 职场文书
党员干部群众路线教育实践活动个人对照检查材料
2014/09/23 职场文书
人与自然的观后感
2015/06/18 职场文书