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中的元类(metaclass)
Feb 14 Python
对于Python的Django框架部署的一些建议
Apr 09 Python
Python实现基于二叉树存储结构的堆排序算法示例
Dec 08 Python
PyQt5每天必学之关闭窗口
Apr 19 Python
详解Python 正则表达式模块
Nov 05 Python
解决Django 在ForeignKey中出现 non-nullable field错误的问题
Aug 06 Python
详解Python中打乱列表顺序random.shuffle()的使用方法
Nov 11 Python
pycharm实现在虚拟环境中引入别人的项目
Mar 09 Python
python 给图像添加透明度(alpha通道)
Apr 09 Python
利用jupyter网页版本进行python函数查询方式
Apr 14 Python
在echarts中图例legend和坐标系grid实现左右布局实例
May 16 Python
Python实现ElGamal加密算法的示例代码
Jun 19 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
如何去掉文章里的 html 语法
2006/10/09 PHP
php使用curl模拟登录后采集页面的例子
2013/11/04 PHP
php+js iframe实现上传头像界面无跳转
2014/04/29 PHP
Smarty模板简单配置与使用方法示例
2016/05/23 PHP
javascript Excel操作知识点
2009/04/24 Javascript
js中关于String对象的replace使用详解
2011/05/24 Javascript
自定义jQuery选项卡插件实例
2013/03/27 Javascript
使用javascript过滤html的字符串(注释标记法)
2013/07/08 Javascript
Web表单提交之disabled问题js解决方法
2015/01/13 Javascript
基于JQuery实现仿网易邮箱全屏动感滚动插件fullPage
2015/09/20 Javascript
JS获取数组最大值、最小值及长度的方法
2015/11/24 Javascript
理解javascript函数式编程中的闭包(closure)
2016/03/08 Javascript
JS原生轮播图的简单实现(推荐)
2017/07/22 Javascript
Node.js搭建小程序后台服务
2018/01/03 Javascript
axios发送post请求,提交图片类型表单数据方法
2018/03/16 Javascript
详解Vue.js在页面加载时执行某个方法
2018/11/20 Javascript
微信小程序人脸识别功能代码实例
2019/05/07 Javascript
ES6中的迭代器、Generator函数及Generator函数的异步操作方法
2019/05/12 Javascript
create-react-app中添加less支持的实现
2019/11/15 Javascript
微信小程序中data-key属性之数据传输(经验总结)
2020/08/22 Javascript
antd table按表格里的日期去排序操作
2020/11/17 Javascript
python实现将元祖转换成数组的方法
2015/05/04 Python
Jupyter中直接显示Matplotlib的图形方法
2018/05/24 Python
python实现大文本文件分割
2019/07/22 Python
tensorflow求导和梯度计算实例
2020/01/23 Python
python GUI模拟实现计算器
2020/06/22 Python
Python如何操作docker redis过程解析
2020/08/10 Python
Python虚拟环境virtualenv创建及使用过程图解
2020/12/08 Python
简单几步用纯CSS3实现3D翻转效果
2019/01/17 HTML / CSS
html5 worker 实例(二) 图片变换效果
2013/06/24 HTML / CSS
详解如何获取localStorage最大存储大小的方法
2020/05/21 HTML / CSS
Julep官网:美容产品和指甲油
2017/02/25 全球购物
师范大学应届生求职信
2013/11/21 职场文书
新党章的学习心得体会
2014/11/07 职场文书
国情备忘录观后感
2015/06/04 职场文书
一文搞懂MySQL索引页结构
2022/02/28 MySQL