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:首先说明