【问题标题】:Tensor Flow variable scope:TensorFlow 变量范围:
【发布时间】:2017-05-03 17:32:50
【问题描述】:

我们为什么要使用 tf.variable_scope:

我知道它的东西链接创建实例的基本思想。但是在下面的代码和许多其他代码中,使用 tf.get_variable 无法在任何地方检索范围。那么范围的作用是什么?不使用会怎样?

with tf.variable_scope("placeholder"):
    input = tf.placeholder(tf.float32, shape=[None, 1024])
    y_true = tf.placeholder(tf.int32, shape=[None, 1])
with tf.variable_scope('FullyConnected'):
    w = tf.get_variable('w', shape=[1024, 1024], 
initializer=tf.random_normal_initializer(stddev=1e-1))
    b = tf.get_variable('b', shape=[1024], 
initializer=tf.constant_initializer(0.1))
    z = tf.matmul(input, w) + b
    y = tf.nn.relu(z)

我看不到以后任何地方都在使用范围。它服务于什么目的。 我想知道如果我删除它会发生什么

【问题讨论】:

标签: variables scope tensorflow


【解决方案1】:

当您在 tensorboard 中可视化该图表时,您会有一个“啊哈”的时刻。

当您调试一些烦人的问题并需要知道是什么张量导致这个或那个错误消息时,您将有另一个“啊哈”时刻。

你绝对需要做这些事情吗?不,不是。你可能还会活下来。但为什么不让您未来的生活更轻松呢?

我在摘要统计中广泛使用变量范围,因为它们可以很好地在 tensorboard 中分组。我通常不会做到 OP 所显示的程度,但这样做确实没有什么坏处。

【讨论】:

    【解决方案2】:

    我们使用变量作用域的原因之一是在 Tensorboard 中总结操作很有用。例如,考虑以下示例:

    g = tf.Graph()
    
    with g.as_default() as g:
    
        tf_x = tf.Variable([[1., 2.], 
                            [3., 4.],
                            [5., 6.]], 
                           name='tf_x_0',
                           dtype=tf.float32)
    
        tf_y = tf.Variable([[7., 8.], 
                            [9., 10.],
                            [11., 12.]], 
                           name='tf_y_0',
                           dtype=tf.float32)
    
        # add custom name scope
        with tf.name_scope('addition'):
            output = tf_x + tf_y
    
        # add custom name scope
        with tf.name_scope('matrix_multiplication'):
            output = tf.matmul(tf.transpose(tf_x), output)
    
    with tf.Session(graph=g) as sess:
        sess.run(tf.global_variables_initializer())
        file_writer = tf.summary.FileWriter(logdir='logs/2', graph=g)
        result = sess.run(output)
        print(result)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 2018-01-18
      • 2019-09-28
      • 2020-01-04
      • 1970-01-01
      相关资源
      最近更新 更多