tensorflow2.0的函式簽名與圖結構(推薦)
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],dtype=int32)
get_concrete_function的使用
note:首先說明,下面介紹的函式在模型構建、模型訓練的過程中不會用到,下面介紹的函式主要用在兩個地方:1、如何儲存模型 2、儲存好模型後,如何載入進來。
可以給 由@tf.function標註的普通的python函式,給它加上input_signature,從而讓這個python函式變成一個可以儲存的tensorflow圖結構(SavedModel)
舉例說明函式的用法:
@tf.function(input_signature=[tf.TensorSpec([None],name='x')]) def cube(z): return tf.pow(z,3) try: print(cube(tf.constant([1.,3.]))) except ValueError as ex: print(ex) print(cube(tf.constant([1,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,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函式簽名與圖結構內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!