【问题标题】:When do I have to explicitly use the reuse option in tensorflow我什么时候必须在 tensorflow 中显式使用重用选项
【发布时间】:2017-05-30 13:10:11
【问题描述】:

在 Tensorflow 文档中它说,variable_scope 中的所有内容都继承了 reuse 选项,如下所示:

with tf.variable_scope("name", reuse=True):
    # inherits reuse=True

但我的问题是,例如,当我使用以下辅助方法之一时:

fc = tf.contrib.layers.fully_connected(input, 512, activation_fn=tf.nn.relu, weights_initializer=tf.contrib.layers.xavier_initializer(uniform=False))

norm = tf.contrib.layers.batch_norm(conv, 0.9, epsilon=1e-5, activation_fn=tf.nn.relu, scope=scope, fused=True)

我是否必须像这样明确设置选项:reusescope

fc = tf.contrib.layers.fully_connected(input, 512, activation_fn=tf.nn.relu, weights_initializer=tf.contrib.layers.xavier_initializer(uniform=False), scope=scope, reuse=True)

norm = tf.contrib.layers.batch_norm(conv, 0.9, epsilon=1e-5, activation_fn=tf.nn.relu, scope=scope, reuse=True, is_training=self.is_training, fused=True)

或者这是如何工作的?

【问题讨论】:

    标签: tensorflow deep-learning


    【解决方案1】:

    我是否必须像这样明确设置选项:重用和范围:

    reuse 参数旨在在图中的不同操作之间重用现有变量Sharing Variables。在神经网络训练步骤中学习参数;因此,要在验证期间使用这些学习参数,您可以使用 reuse 参数检索变量,而无需重置值或重新创建这些变量。

    跟踪reuse 参数是一种很好的做法

    # a usual CNN model definition with reuse parameter
    def model(input, reuse=None):
        fc = tf.contrib.layers.fully_connected(input, 512,activation_fn=tf.nn.relu,   
             weights_initializer=tf.contrib.layers.xavier_initializer(uniform=False),   
             scope=scope, reuse=reuse)
    
        norm = tf.contrib.layers.batch_norm(conv, 0.9, epsilon=1e-5,
            activation_fn=tf.nn.relu, scope=scope, reuse=reuse, 
            is_training=self.is_training, fused=True)
        ...
    
    
    # Now for training; setting reuse=None creates the graph variables
    some_ouput = model(input, reuse=None)
    
    # Now for validation; setting reuse=True; retrieve the learned variables for validation without creating new one.
    some_output = model(input, reuse=True) 
    

    【讨论】:

      【解决方案2】:

      首先,谷歌搜索!这应该可以回答您的问题:Tensorflow variable scope: reuse if variable exists

      但如果不是.. 如果您实际上重用模型的一部分,而不是使用该层,则需要调用重用。假设您有一个函数定义为:

      def func(input,scope=scope, reuse=reuse):
          fc = tf.contrib.layers.fully_connected(input, ..., scope=scope, reuse=reuse)
          norm = tf.contrib.layers.batch_norm(fc, ..., scope=scope, reuse=reuse) 
          return norm
      

      Scope 将定义变量的命名空间(阅读更多关于范围 What's the difference of name scope and a variable scope in tensorflow?https://jasdeep06.github.io/posts/variable-sharing-in-tensorflow/Understanding Variable scope example in Tensorflow 的信息),所以一旦你使用 scope='scp' 和 input.shape=(1, 2,3),您将无法以相同的范围和 input.shape=(3,4,5) 运行相同的函数,因为具有给定范围和不同形状的变量已经存在。 因此,您应该在第二次调用函数时使用 reuse=True。

      【讨论】:

      • 这根本不能回答我的问题!我在问,是否必须明确设置范围=范围,重用=重用,或者当我使用 tf.variable_scope(name, reuse=True) 作为范围时我是否可以离开它:..
      • 也许我误解了你,对不起。然而,您的问题可以通过阅读范围来回答。 tensorflow.org/programmers_guide/variable_scope 通常 - 不,您不再需要将重用作为参数。尽管如此,范围界定在您的情况下没有多大意义,因为您编写的代码将始终使用新输入。正如我所提到的,一旦你真正定义了你自己想要重用的模型,作用域就会很有用。希望对您有更多帮助。
      猜你喜欢
      • 2019-02-28
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多