欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

tensorflow2.0的函数签名与图结构(推荐)

程序员文章站 2023-10-31 15:47:34
input_signature的好处:1.可以限定函数的输入类型,以防止调用函数时调错,2.一个函数有了input_signature之后,在tensorflow里边才可以保存成savedmodel。...

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函数签名与图结构内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!