小言_互联网的博客

Tensorflow2.0-数据流图

496人阅读  评论(0)
import tensorflow as tf


def tensorflow_demo():
    # tensorflow中的图有两种:
    # 1、默认图
    # 2、用户自定义的图(tf.Graph())

    # 流程图:定义数据对象(张量Tensor)和要执行的操作(节点OP)
    # 构建图阶段:
    # 创建一个图
    g = tf.Graph()
    # 在上下文管理器内部就可以定义用户自己创建图的数据和操作
    with g.as_default():
        a_t = tf.constant(2)
        b_t = tf.constant(3)
        c_t = a_t + b_t
    # 执行图阶段
    # 方式1:
    with tf.compat.v1.Session(graph=g) as sess:
        c_t_value = sess.run(c_t)
        print(c_t_value)
        print("我们自己创建的图为:\n", sess.graph)

    # 方式2:(包括会话的开启和关闭)
    # sess = tf.compat.v1.Session(graph=g)
    # c_t_value = sess.run(c_t)
    # print(c_t_value)
    # sess.close()
    return None


def graph_demo():
    # 若没有创建图,则使用默认图:
    a_t = tf.constant(2)
    b_t = tf.constant(3)
    c_t = a_t + b_t
    # 查看默认图的方式有两种:
    # 1、通过调用get_default_graph()方法
    default_graph = tf.compat.v1.get_default_graph()
    print("默认图为:\n", default_graph)
    # 2、通过查看OP或sess的属性来查看图(如sess.graph)


if __name__ == "__main__":
    tensorflow_demo()
    # graph_demo()

转载:https://blog.csdn.net/qq_40064490/article/details/102489723
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场