How Do I Obtain Switch_v1.pb?

  1. Copy the following script content to a .py file, for example, Switch_v1.py.
    import os
    import numpy as np
    import tensorflow as tf
    
    x = tf.compat.v1.placeholder(tf.float32, name='x')
    y = tf.compat.v1.placeholder(tf.float32, name='y')
    z = tf.compat.v1.placeholder(tf.float32, name='z')
    
    
    def then_branch(x, y, z):
        m = tf.square(x)
        return m + tf.multiply(y, z)
    
    def else_branch(x, y, z):
        m = tf.pow(x, y)
        return m - tf.div(y, z)
    
    # Entry to use control flow operators. After the script is executed, the corresponding V1 control flow operators are generated.
    def testDefun(x, y, z):
        return tf.cond(pred=x < y, true_fn=lambda: then_branch(x, y, z), false_fn=lambda: else_branch(x, y, z)), y
    
    def testCase(x, y, z):
        a, b = testDefun(x, y, z)
        return a + b * z
    
    
    with tf.compat.v1.Session() as sess:
        result = sess.run(testCase(x, y, z), feed_dict={x: 1., y: .6, z: .2})
    
        with tf.io.gfile.GFile('./Switch_v1.pb', 'wb') as f:
            f.write(sess.graph_def.SerializeToString())
    
  2. Go to the Switch_v1.py script directory and create Switch_v1.pb using the following command.
    python3.7.5 Switch_v1.py

    You will find the generated network in the current directory after executing the command.