溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

什么是tensorflow2.0的函數(shù)簽名與圖結(jié)構(gòu)

發(fā)布時(shí)間:2020-07-29 14:09:13 來源:億速云 閱讀:317 作者:小豬 欄目:開發(fā)技術(shù)

小編這次要給大家分享的是什么是tensorflow2.0的函數(shù)簽名與圖結(jié)構(gòu),文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

input_signature的好處:

1.可以限定函數(shù)的輸入類型,以防止調(diào)用函數(shù)時(shí)調(diào)錯(cuò),

2.一個(gè)函數(shù)有了input_signature之后,在tensorflow里邊才可以保存成savedmodel。在保存成savedmodel的過程中,需要使用get_concrete_function函數(shù)把一個(gè)tf.function標(biāo)注的普通的python函數(shù)變成帶有圖定義的函數(shù)。

下面的代碼具體體現(xiàn)了input_signature可以限定函數(shù)的輸入類型這一作用。

@tf.function(input_signature=[tf.TensorSpec([None], tf.int32, name='x')])
def cube(z): #實(shí)現(xiàn)輸入的立方
 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:首先說明,下面介紹的函數(shù)在模型構(gòu)建、模型訓(xùn)練的過程中不會(huì)用到,下面介紹的函數(shù)主要用在兩個(gè)地方:1、如何保存模型 2、保存好模型后,如何載入進(jìn)來。

可以給 由@tf.function標(biāo)注的普通的python函數(shù),給它加上input_signature, 從而讓這個(gè)python函數(shù)變成一個(gè)可以保存的tensorflow圖結(jié)構(gòu)(SavedModel)

舉例說明函數(shù)的用法:

@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>

從輸出結(jié)果可以看到:調(diào)用get_concrete_function函數(shù)后,輸出的是一個(gè)ConcreteFunction對(duì)象

#看用新參數(shù)獲得的對(duì)象與原來的對(duì)象是否一樣
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]))) #傳具體數(shù)據(jù)

輸出:

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")  #默認(rèn)加“:0”

<tf.Tensor 'x:0' shape=(None,) dtype=int32>

cube_func_int32.graph.as_graph_def() #總名字,針對(duì)上面兩個(gè)

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
}

看完這篇關(guān)于什么是tensorflow2.0的函數(shù)簽名與圖結(jié)構(gòu)的文章,如果覺得文章內(nèi)容寫得不錯(cuò)的話,可以把它分享出去給更多人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI