tenssor flow 2.0の関数署名と図の構造(推奨)


input_signatureのメリット:
1.関数の入力タイプを指定して、関数のタイミングを間違えないようにします。
2.一つの関数はinput_があります。signatureの後、tenssorflowの中で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 incomptible with input_signature:
  input:(
    tf.Tensor([1.2.3.],shop=(3,)、dtype=froat 32)
  input_signature:(
    TensorSpec(shape=(None,),dtype=tf.int 32,name='x')
tf.Tensor([1]  8 27],shop=(3,),dtype=int 32)
ゲットするconcrete_functionの使用
ノート:まず説明します。以下に紹介する関数は模型の構築、模型の訓練の過程では使われません。以下に紹介する関数は主に二つのところで使います。
@tf.functionで表示されている普通のpython関数にinput(u)を付けることができます。signatureは、このpython関数を保存可能なtenssor flow構造にします。
関数の使用例を示します。

@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)
出力:
<tenssorflow.python.eager.function.concrete Funct at 0 x 0000240 E 29695 C 0>
アウトプットの結果から、get_を呼び出します。concrete_function関数の後、出力されるのはCocrete Functionオブジェクトです。

#                    
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_funcint 32.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」
atr{
  key:"T"
  value{
    type:DT_INT 32
  }
)

print(list(pow_op.inputs))
print(list(pow_op.outputs))
出力:
[]
[]
cube_funcint 32.graphh.get_operation_by_name("x")
出力:
<tf.Operation'x'type=Placeholder>
cube_funcint 32.graphh.get_tensor_by_name(「x:0」)  #デフォルトに「:0」を追加します
<tf.Tensor'x:0'shop=(None,)dtype=int 32>
cube_funcint 32.graphh.as_graphdef()xi総名は、上の二つに対して

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
}
 ここで、テナントflow 2.0の関数署名と図の構造についての記事を紹介します。より多くの関連テナントファンクション署名と図の構成内容については、私達の以前の文章を検索したり、次の関連記事を見たりしてください。これからもよろしくお願いします。