How to generate TF v1 graph and save to pb in TF 2.x

Hi all:
I tried to generate tf v1 graph and save to pb file in TF2.x, but found the graph is very different with what in TF1.x. Is there anyway can generate same pb in TF2.x?

with tf.compat.v1.Graph().as_default():
    input_t = tf.compat.v1.placeholder(tf.float32, shape=(1, 28, 10))
    gru_layer = tf.compat.v1.keras.layers.GRU(10, bias_initializer='glorot_uniform', reset_after=True)
    out = gru_layer(inp)

with tf.compat.v1.Session() as sess:
    sess.run(tf.compat.v1.global_variables_initializer())
    frozen_graph_def = tf.compat.v1.graph_util.convert_variables_to_constants(sess, sess.graph_def, output_nodes)
    with open(model_path, 'wb') as f:
        f.write(frozen_graph_def.SerializeToString())

But in Netron, the graph as below:
Same code run in TF2.x:


Many EmptyTesorList in Graph.
Same Code run in TF1.x:

But if I saved to H5 file, the graph will be clear as below:
image

Anyone can explain this result? Or can we only use H5/SavedModel in tf2.x ?
Is there anyway can generate same pb in TF2.x?

Thanks all for your reply

Hi @Runner_Zhong,

Sorry for the delay in response.

The graph structure is differs in TF 2.x from TF 1.x due to static and eager executions, this might result difference in above graphs.

In TF 2.x, models are generally saved in the SavedModel format, which includes the model architecture, weights,etc. Kindly refer this documentation for more information about saved_models in TF2.

Yes, you can create a .pb file. Please refer to this gist for the implementation details.

Hope this helps.Thank You.