tensorflow 代码流程01

阶段1:正常情况实现加法运算

"""
   __author__="dazhi"
    2021/3/5-12:35
"""
import tensorflow as tf
def tensorflow_demo():
    """tensorflow Basic structure"""
    a = 2
    b = 3
    c = a+b
    print("The result of addition ",c)

if __name__=="__main__":
    tensorflow_demo()

阶段2:在tensorflow的场景下实现加法运算

"""
   __author__="dazhi"
    2021/3/5-12:35
"""
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def tensorflow_demo():
    tf.compat.v1.disable_eager_execution()
    #ensure sess.run () can operate normally

    """tensorflow Basic structure"""
    a = 2
    b = 3
    c = a+b
    print("The result of addition ",c)

    #tensorflow Realize addition operation
    a1 = tf.constant(2)
    b1 = tf.constant(3)
    c1 = a1+b1
    print("The result of addition ",c1)
    #Return call must be turned on to display results
    with tf.compat.v1.Session() as sess:
        c2 = sess.run(c1)
        print("The result of addition ", c2)
    return None



if __name__=="__main__":
    tensorflow_demo()

 

阶段3:默认图

"""
   __author__="dazhi"
    2021/3/5-12:35
"""
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def graph_demo():
    tf.compat.v1.disable_eager_execution()
    #ensure sess.run () can operate normally
    """tensorflow Basic structure"""
    #tensorflow Realize addition operation

    a1 = tf.constant(2)
    b1 = tf.constant(3)
    c1 = a1+b1
    print("The result of addition ", c1)

    #View default map
    #Method 1: call the method
    default_g=tf.compat.v1.get_default_graph()
    print("default_g :",default_g)

    #Method 2: view properties
    print(" a1 Graph properties  ",a1.graph)
    print(" c1 Graph properties  ",c1.graph)

    #Return call must be turned on to display results
    with tf.compat.v1.Session() as sess:
        c2 = sess.run(c1)
        print("The result of addition ", c2)
        print("sess Graph properties ", sess.graph)
    return None



if __name__=="__main__":
    graph_demo()

 

阶段4:自定义图

"""
   __author__="dazhi"
    2021/3/5-12:35
"""
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def graph_demo():
    tf.compat.v1.disable_eager_execution()
    #ensure sess.run () can operate normally
    """tensorflow Basic structure"""
    #tensorflow Realize addition operation

    #Custom map
    new_g=tf.Graph()
    with new_g.as_default():
        a1 = tf.constant(2)
        b1 = tf.constant(3)
        c1 = a1+b1
        print("The result of addition ", c1)

        #View default map
        #Method 1: call the method
        default_g=tf.compat.v1.get_default_graph()
        print("default_g :",default_g)

        #Method 2: view properties
        print(" a1 Graph properties  ",a1.graph)
        print(" c1 Graph properties  ",c1.graph)

    #Return call must be turned on to display results
    with tf.compat.v1.Session(graph=new_g) as sess:
        c2 = sess.run(c1)
        print("The result of addition ", c2)
        print("sess Graph properties ", sess.graph)
    return None



if __name__=="__main__":
    graph_demo()

 

阶段5:图结构的可视化

"""
   __author__="dazhi"
    2021/3/5-12:35
"""
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def graph_demo():
    tf.compat.v1.disable_eager_execution()
    #ensure sess.run () can operate normally
    """tensorflow Basic structure"""
    #tensorflow Realize addition operation

    #Custom map
    new_g=tf.Graph()
    with new_g.as_default():
        a1 = tf.constant(2)
        b1 = tf.constant(3)
        c1 = a1+b1

    #Return call must be turned on to display results
    with tf.compat.v1.Session(graph=new_g) as sess:
        c2 = sess.run(c1)
        #Write the graph to the locally generated events file
        tf.compat.v1.summary.FileWriter("./tmp/summary",graph=sess.graph)


    return None



if __name__=="__main__":
    graph_demo()

阶段6:修改指令名称

"""
   __author__="dazhi"
    2021/3/5-12:35
"""
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def graph_demo():
    tf.compat.v1.disable_eager_execution()
    #ensure sess.run () can operate normally
    """tensorflow Basic structure"""
    #tensorflow Realize addition operation

    #Custom map
    new_g=tf.Graph()
    with new_g.as_default():
        a1 = tf.constant(2,name="wen")
        b1 = tf.constant(3,name="xue")
        c1 = tf.add(a1,b1,name="zhi")

    #Return call must be turned on to display results
    with tf.compat.v1.Session(graph=new_g) as sess:
        c2 = sess.run(c1)
        #Write the graph to the locally generated events file
        tf.compat.v1.summary.FileWriter("./tmp/summary",graph=sess.graph)


    return None



if __name__=="__main__":
    graph_demo()

阶段7:placeholder操作

"""
   __author__="dazhi"
    2021/3/5-18:46
"""
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def graph_demo():
    tf.compat.v1.disable_eager_execution()
    #ensure sess.run () can operate normally
    """tensorflow Basic structure"""
    #tensorflow Realize addition operation

    new_g=tf.Graph()
    with new_g.as_default():
        a1 = tf.compat.v1.placeholder(tf.float32)
        b1 = tf.compat.v1.placeholder(tf.float32)
        c1 = tf.add(a1,b1)

    #Return call must be turned on to display results
    with tf.compat.v1.Session(graph=new_g) as sess:
        #run placeholder
        c2 = sess.run(c1,feed_dict={a1:2,b1:23})
        print("The value of c2 ",c2)

    return None



if __name__=="__main__":
    graph_demo()

原文地址:https://www.cnblogs.com/dazhi151/p/14486462.html