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函数签名与图结构内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新动态
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]