TensorFlow:图

一、计算图

图是由许多个OP构成的,执行计算图需要通过session去实现。我们很多变量是要分层的,就像神经网络那样,否则容易混乱。这时我们需要使用name_scope和varialble_scope去实现。

with tf.name_scope("123"):
    with tf.name_scope("456"):
        with tf.variable_scope("789"):
            d = tf.Variable(1, name="d")
            print(d.name)
        with tf.variable_scope("789"):
            e = tf.Variable(2, name="e")
            g = tf.get_variable("bb", 1)
            print(d.name)
            print(g.name)

当我们构造一个tf.Variable变量时,实际上会产生4个OP。可以通过graph.get_operations()去查看。

二、显示计算图

g = tf.get_default_graph()
tf.summary.FileWriter("logs",g).close()
# 或在用户路径生成一个logs文件夹,存放计算图

#在terminal中输入
tensorboard -- logdir=logs
# 去http://127.0.0.1:6006查看

在这里插入图片描述
可以清楚两种scope的区别了。

猜你喜欢

转载自blog.csdn.net/weixin_42231070/article/details/85273121
今日推荐