举例说明
TensorFlow中的变量一般就是模型的参数。当模型复杂的时候共享变量会无比复杂。
官网给了一个case,当创建两层卷积的过滤器时,每输入一次图片就会创建一次过滤器对应的变量,但是我们希望所有图片都共享同一过滤器变量,一共有4个变量:conv1_weights,conv1_biases,conv2_weights, and conv2_biases。
通常的做法是将这些变量设置为全局变量。但是存在的问题是打破封装性,这些变量必须文档化被其他代码文件引用,一旦代码变化,调用方也可能需要变化。
还有一种保证封装性的方式是将模型封装成类。
不过TensorFlow提供了Variable Scope 这种独特的机制来共享变量。这个机制涉及两个主要函数:
1
2
|
tf.get_variable(<name>, <shape>, <initializer>) 创建或返回给定名称的变量 tf.variable_scope(<scope_name>) 管理传给get_variable()的变量名称的作用域 |
在下面的代码中,通过tf.get_variable()创建了名称分别为weights和biases的两个变量。
1
2
3
4
5
6
7
8
9
10
|
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,
在这个作用域下有两个变量weights和biases。
1
2
3
4
5
6
7
|
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(),代码如下:
1
2
3
4
|
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,调用该函数会创建新的变量
123with tf.variable_scope(
"foo"
):
v
=
tf.get_variable(
"v"
, [
1
])
assert
v.name
=
=
"foo/v:0"
-
当tf.get_variable_scope().reuse == True,调用该函数会重用已经创建的变量
12345with 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()用来指定变量的作用域,作为变量名的前缀,支持嵌套,如下:
1
2
3
4
|
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,这个非常有用,如下
1
2
3
4
5
|
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当前作用域,如
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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 |
一个作用域可以作为另一个新的作用域的参数,如:
1
2
3
4
5
6
7
8
9
|
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()打开一个已经存在的作用域时,就会跳转到这个作用域。
1
2
3
4
5
6
7
|
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()函数,除非中间有函数改变,否则不变。
1
2
3
4
5
6
7
8
9
10
11
|
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
1
2
3
|
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
1
2
3
4
5
6
|
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" |
相关推荐
变量作用域(Variable Scope)是指变量在代码中的可见范围。Python有四种作用域: 1. 全局作用域(Global Scope):在所有函数外部定义的变量,可以在整个程序中访问。 2. 局部作用域(Local Scope):在函数内部...
理解变量作用域的概念 理解局部变量与全局变量的区别及作用域 能区分局部变量与全局变量,并读懂程序 变量的作用域(scope of a variable) 就指变量在程序中可以被引用的范围。 变量的作用域 public static void ...
最后,我们来谈谈“变量作用域”(Variable Scope)。在Python中,变量的作用域决定了在哪里可以访问它们。主要有四种作用域: 1. 全局作用域(Global):在函数或类外部定义的变量,可以在整个程序中访问。 2. ...
通常与 `tf.get_variable()` 函数配合使用,确保在特定的变量作用域内可以复用或创建新变量。通过设置 `reuse` 参数,我们可以控制变量是否应该被共享。 - 当 `reuse=None` 或 `False` 时,尝试创建新变量,如果已...
3. **全局作用域**(Global Scope):模块级别定义的变量在整个模块内都是可见的。如果函数内部没有显式地声明变量为局部变量,则默认认为它是全局变量。 4. **内置作用域**(Built-in Scope):包含Python解释器...
Tensorflow |(1)初识Tensorflow Tensorflow |(2)张量的阶和...通过tf.variable_scope()创建指定名字的变量作用域 with tf.variable_scope(itcast) as scope: print(----) 加上with语句就可以在整个itcast变量作
PHP有四种主要的变量作用域: 1. **局部作用域(Local Scope)**:变量在函数内部定义时,它就是局部的。这些变量只在该函数内部可用,一旦函数执行完毕,它们就会被销毁。 ```php function myFunction() { $...
执行上下文包含了词法环境(Lexical Environment)、变量环境(Variable Environment)、作用域链(Scope Chain)等多个组件,其中变量对象就是变量环境的一部分。 变量对象存储了在该上下文中声明的所有变量和函数...
### Scope(作用域) #### 一、什么是作用域 作用域是编程语言中用来定义变量可见性和可访问性的概念。简单来说,作用域决定了变量在何处可以被引用和使用。在JavaScript中,作用域主要分为两种类型:全局作用域和...
二、作用域(Scope)和作用域链(Scope Chain) 在 JavaScript 中,每个执行环境都有一个 Variable Object,用于存储变量和函数。函数体中可以使用全局变量、函数嵌套定义时情况更复杂点。这些情况下怎样处理?...
`let`可以重新赋值,而`const`一旦声明就不能再改变,它们都有效地解决了变量提升(Variable Hoisting)的问题,使得变量作用域更加清晰。 理解变量作用域对于编写高效且易于维护的JavaScript代码至关重要。合理地...
当`reuse`参数被设置为`tf.AUTO_REUSE`,并且在相同的变量作用域内,调用`tf.get_variable`时,如果已经存在同名变量,它将返回这个已存在的变量,而不是创建新的。例如: ```python with tf.variable_scope('scope...
此外,TensorFlow提供了`tf.variable_scope`来管理变量的作用域。通过`variable_scope`,我们可以控制变量的创建和复用。例如: ```python with tf.variable_scope("scope1"): w1 = tf.get_variable("w1", shape=[]...
例如,在上述的`resnet_stack`函数中,卷积层的权重可能隐藏在`resnet_style_transfer`的变量作用域内。 ```python # 获取卷积层的权重 with tf.variable_scope('resnet_style_transfer', reuse=True): conv_...
JavaScript 中有一个被称为作用域(Scope)的特性。虽然对于许多新手开发者来说,作用域的概念并不是很容易理解,本文我会尽我所能用最简单的方式来解释作用域和作用域链,希望大家有所收获! 作用域(Scope) 1. 什么是...
本文将深入探讨`global`标识对Python变量作用域的影响。 首先,变量的作用域分为全局作用域(Global Scope)和局部作用域(Local Scope)。全局作用域中的变量在整个程序中都可被访问,而局部作用域中的变量仅在其...
本篇文章将深入探讨Python中的变量作用域,包括全局作用域和局部作用域,以及如何在函数内部修改全局变量。 1. 全局作用域(Global Scope) 全局作用域是指程序执行期间变量可以被任何位置访问的区域。在Python中,...