tensorflow变量做用域(variable scope)

举例说明

TensorFlow中的变量通常就是模型的参数。当模型复杂的时候共享变量会无比复杂。python

官网给了一个case,当建立两层卷积的过滤器时,每输入一次图片就会建立一次过滤器对应的变量,可是咱们但愿全部图片都共享同一过滤器变量,一共有4个变量:conv1_weights,conv1_biases,conv2_weights, and conv2_biases。dom

一般的作法是将这些变量设置为全局变量。可是存在的问题是打破封装性,这些变量必须文档化被其余代码文件引用,一旦代码变化,调用方也可能须要变化。ide

还有一种保证封装性的方式是将模型封装成类。函数

不过TensorFlow提供了Variable Scope 这种独特的机制来共享变量。这个机制涉及两个主要函数:orm

tf.get_variable(<name>, <shape>, <initializer>) 建立或返回给定名称的变量
tf.variable_scope(<scope_name>) 管理传给get_variable()的变量名称的做用域

  

在下面的代码中,经过tf.get_variable()建立了名称分别为weights和biases的两个变量。对象

def conv_relu(input, kernel_shape, bias_shape):
    # Create variable named "weights".
    weights = tf.get_variable("weights", kernel_shape,
        initializer=tf.random_normal_initializer())
    # Create variable named "biases".
    biases = tf.get_variable("biases", bias_shape,
        initializer=tf.constant_initializer(0.0))
    conv = tf.nn.conv2d(input, weights,
        strides=[1, 1, 1, 1], padding='SAME')
    return tf.nn.relu(conv + biases)

  

可是咱们须要两个卷积层,这时能够经过tf.variable_scope()指定做用域进行区分,如with tf.variable_scope("conv1")这行代码指定了第一个卷积层做用域为conv1,blog

在这个做用域下有两个变量weights和biases。图片

def my_image_filter(input_images):
    with tf.variable_scope("conv1"):
        # Variables created here will be named "conv1/weights", "conv1/biases".
        relu1 = conv_relu(input_images, [5, 5, 32, 32], [32])
    with tf.variable_scope("conv2"):
        # Variables created here will be named "conv2/weights", "conv2/biases".
        return conv_relu(relu1, [5, 5, 32, 32], [32])

  

最后在image_filters这个做用域重复使用第一张图片输入时建立的变量,调用函数reuse_variables(),代码以下:ci

with tf.variable_scope("image_filters") as scope:
    result1 = my_image_filter(image1)
    scope.reuse_variables()
    result2 = my_image_filter(image2)

  

tf.get_variable()工做机制

tf.get_variable()工做机制是这样的:作用域

  • 当tf.get_variable_scope().reuse == False,调用该函数会建立新的变量

      with tf.variable_scope("foo"):
          v = tf.get_variable("v", [1])
      assert v.name == "foo/v:0"
    

      

  • 当tf.get_variable_scope().reuse == True,调用该函数会重用已经建立的变量

      with tf.variable_scope("foo"):
          v = tf.get_variable("v", [1])
      with tf.variable_scope("foo", reuse=True):
          v1 = tf.get_variable("v", [1])
      assert v1 is v
    

      

变量都是经过做用域/变量名来标识,后面会看到做用域能够像文件路径同样嵌套。

tf.variable_scope理解

tf.variable_scope()用来指定变量的做用域,做为变量名的前缀,支持嵌套,以下:

with tf.variable_scope("foo"):
    with tf.variable_scope("bar"):
        v = tf.get_variable("v", [1])
assert v.name == "foo/bar/v:0"

  

当前环境的做用域能够经过函数tf.get_variable_scope()获取,而且reuse标志能够经过调用reuse_variables()设置为True,这个很是有用,以下

with tf.variable_scope("foo"):
    v = tf.get_variable("v", [1])
    tf.get_variable_scope().reuse_variables()
    v1 = tf.get_variable("v", [1])
assert v1 is v

  

做用域中的resuse默认是False,调用函数reuse_variables()可设置为True,一旦设置为True,就不能返回到False,而且该做用域的子空间reuse都是True。若是不想重用变量,那么能够退回到上层做用域,至关于exit当前做用域,如

with tf.variable_scope("root"):
    # At start, the scope is not reusing.
    assert tf.get_variable_scope().reuse == False
    with tf.variable_scope("foo"):
        # Opened a sub-scope, still not reusing.
        assert tf.get_variable_scope().reuse == False
    with tf.variable_scope("foo", reuse=True):
        # Explicitly opened a reusing scope.
        assert tf.get_variable_scope().reuse == True
        with tf.variable_scope("bar"):
            # Now sub-scope inherits the reuse flag.
            assert tf.get_variable_scope().reuse == True
    # Exited the reusing scope, back to a non-reusing one.
    assert tf.get_variable_scope().reuse == False

  

一个做用域能够做为另外一个新的做用域的参数,如:

with tf.variable_scope("foo") as foo_scope:
    v = tf.get_variable("v", [1])
with tf.variable_scope(foo_scope):
    w = tf.get_variable("w", [1])
with tf.variable_scope(foo_scope, reuse=True):
    v1 = tf.get_variable("v", [1])
    w1 = tf.get_variable("w", [1])
assert v1 is v
assert w1 is w

  

无论做用域如何嵌套,当使用with tf.variable_scope()打开一个已经存在的做用域时,就会跳转到这个做用域。

with tf.variable_scope("foo") as foo_scope:
    assert foo_scope.name == "foo"
with tf.variable_scope("bar"):
    with tf.variable_scope("baz") as other_scope:
        assert other_scope.name == "bar/baz"
        with tf.variable_scope(foo_scope) as foo_scope2:
            assert foo_scope2.name == "foo"  # Not changed.

  

variable scope的Initializers能够创递给子空间和tf.get_variable()函数,除非中间有函数改变,不然不变。

with tf.variable_scope("foo", initializer=tf.constant_initializer(0.4)):
    v = tf.get_variable("v", [1])
    assert v.eval() == 0.4  # Default initializer as set above.
    w = tf.get_variable("w", [1], initializer=tf.constant_initializer(0.3)):
    assert w.eval() == 0.3  # Specific initializer overrides the default.
    with tf.variable_scope("bar"):
        v = tf.get_variable("v", [1])
        assert v.eval() == 0.4  # Inherited default initializer.
    with tf.variable_scope("baz", initializer=tf.constant_initializer(0.2)):
        v = tf.get_variable("v", [1])
        assert v.eval() == 0.2  # Changed default initializer.

  

算子(ops)会受变量做用域(variable scope)影响,至关于隐式地打开了同名的名称做用域(name scope),如+这个算子的名称为foo/add

with tf.variable_scope("foo"):
    x = 1.0 + tf.get_variable("v", [1])
assert x.op.name == "foo/add"

  

除了变量做用域(variable scope),还能够显式打开名称做用域(name scope),名称做用域仅仅影响算子的名称,不影响变量的名称。另外若是tf.variable_scope()传入字符参数,建立变量做用域的同时会隐式建立同名的名称做用域。以下面的例子,变量v的做用域是foo,而算子x的算子变为foo/bar,由于有隐式建立名称做用域foo

with tf.variable_scope("foo"):
    with tf.name_scope("bar"):
        v = tf.get_variable("v", [1])
        x = 1.0 + v
assert v.name == "foo/v:0"
assert x.op.name == "foo/bar/add"

  

注意: 若是tf.variable_scope()传入的不是字符串而是scope对象,则不会隐式建立同名的名称做用域。

相关文章
相关标签/搜索