Keras (ten) TF function signature and graph structure

This article will introduce the following:

  • TF function signature
  • Graph structure

The role of input_signature in the TF function:
1. Signature (make sure the incoming parameters meet the requirements).
2. Only after there is input_signature can it be saved as TF graphic hook-SaveModel in tf.

One, TF function signature

The function signature consists of the function prototype. What it tells you is general information about the function, its name, parameters, its scope, and other miscellaneous information. It can be determined that the incoming parameters meet the requirements.

We use input_signaturethe function modified by tf.function to digitally sign;

tf.TensorSpec ( shape, dtype=tf.dtypes.float32, name=None )

tf.TensorSpec() :#TensorSpec: Describe a tensor.

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras

# 1,打印使用的python库的版本信息
print(tf.__version__)
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
    print(module.__name__, module.__version__)

# 2,TF函数的签名机制
@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])))

#---output-------
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)

Second, the graph structure

For the attributes that the tf.functionmodified function has get_concrete_function, you can @tf.function()add a function signature to the function to obtain a specific trace. Only after adding the function signature can the model be saved .

1. Get tf.functionthe get_concrete_functionattributes of the modified function
# @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))
print(cube_func_int32)

# tf.TensorSpec()中可以携带参数,且类型属性不变
print(cube_func_int32 is cube.get_concrete_function(tf.TensorSpec([5], tf.int32)))
print(cube_func_int32 is cube.get_concrete_function(tf.constant([1, 2, 3])))

#----output------
<tensorflow.python.eager.function.ConcreteFunction object at 0x7f7553345240>
True
True
2. Get the graph structure object in the attribute of the decorated function get_concrete_function
print(cube_func_int32.graph)

#---output-----
FuncGraph(name=cube, id=140141878427488)
3. Get all the op nodes of the graph structure in the attribute of the decorated function get_concrete_function
1) All op nodes are as follows:
print(cube_func_int32.graph.get_operations())

#---output-----
[<tf.Operation 'x' type=Placeholder>, <tf.Operation 'Pow/y' type=Const>, <tf.Operation 'Pow' type=Pow>, <tf.Operation 'Identity' type=Identity>]
2) The specific op node information is as follows:
pow_op = cube_func_int32.graph.get_operations()[2]
print(pow_op)

#---output------------
name: "Pow"
op: "Pow"
input: "x"
input: "Pow/y"
attr {
    
    
  key: "T"
  value {
    
    
    type: DT_INT32
  }
}
4. Get the node object by the node name and tensor name
# 通过节点名称获取节点对象
print(cube_func_int32.graph.get_operation_by_name("x"))

# 通过tensor名称获取节点对象
print(cube_func_int32.graph.get_tensor_by_name("x:0"))

#---output-----------
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
      }
    }
  }
}

Tensor("x:0", shape=(None,), dtype=int32)
5. Get all graph structure information
print(cube_func_int32.graph.as_graph_def())

#---output-----
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: 175
}

Guess you like

Origin blog.csdn.net/TFATS/article/details/110563480