TENSORFLOW变量作用域(VARIABLE SCOPE)
举例说明
TensorFlow中的变量一般就是模型的参数。当模型复杂的时候共享变量会无比复杂。
官网给了一个case,当创建两层卷积的过滤器时,每输入一次图片就会创建一次过滤器对应的变量,但是我们希望所有图片都共享同一过滤器变量,一共有4个变量:conv1_weights,conv1_biases,conv2_weights,andconv2_biases。
通常的做法是将这些变量设置为全局变量。但是存在的问题是打破封装性,这些变量必须文档化被其他代码文件引用,一旦代码变化,调用方也可能需要变化。
还有一种保证封装性的方式是将模型封装成类。
不过TensorFlow提供了VariableScope这种独特的机制来共享变量。这个机制涉及两个主要函数:
tf.get_variable(, , )创建或返回给定名称的变量 tf.variable_scope( )管理传给get_variable()的变量名称的作用域
在下面的代码中,通过tf.get_variable()创建了名称分别为weights和biases的两个变量。
defconv_relu(input,kernel_shape,bias_shape): #Createvariablenamed"weights". weights=tf.get_variable("weights",kernel_shape, initializer=tf.random_normal_initializer()) #Createvariablenamed"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') returntf.nn.relu(conv+biases)
但是我们需要两个卷积层,这时可以通过tf.variable_scope()指定作用域进行区分,如withtf.variable_scope("conv1")这行代码指定了第一个卷积层作用域为conv1,
在这个作用域下有两个变量weights和biases。
defmy_image_filter(input_images): withtf.variable_scope("conv1"): #Variablescreatedherewillbenamed"conv1/weights","conv1/biases". relu1=conv_relu(input_images,[5,5,32,32],[32]) withtf.variable_scope("conv2"): #Variablescreatedherewillbenamed"conv2/weights","conv2/biases". returnconv_relu(relu1,[5,5,32,32],[32])
最后在image_filters这个作用域重复使用第一张图片输入时创建的变量,调用函数reuse_variables(),代码如下:
withtf.variable_scope("image_filters")asscope: 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,调用该函数会创建新的变量
withtf.variable_scope("foo"): v=tf.get_variable("v",[1]) assertv.name=="foo/v:0"
当tf.get_variable_scope().reuse==True,调用该函数会重用已经创建的变量
withtf.variable_scope("foo"): v=tf.get_variable("v",[1]) withtf.variable_scope("foo",reuse=True): v1=tf.get_variable("v",[1]) assertv1isv
变量都是通过作用域/变量名来标识,后面会看到作用域可以像文件路径一样嵌套。
tf.variable_scope理解
tf.variable_scope()用来指定变量的作用域,作为变量名的前缀,支持嵌套,如下:
withtf.variable_scope("foo"): withtf.variable_scope("bar"): v=tf.get_variable("v",[1]) assertv.name=="foo/bar/v:0"
当前环境的作用域可以通过函数tf.get_variable_scope()获取,并且reuse标志可以通过调用reuse_variables()设置为True,这个非常有用,如下
withtf.variable_scope("foo"): v=tf.get_variable("v",[1]) tf.get_variable_scope().reuse_variables() v1=tf.get_variable("v",[1]) assertv1isv
作用域中的resuse默认是False,调用函数reuse_variables()可设置为True,一旦设置为True,就不能返回到False,并且该作用域的子空间reuse都是True。如果不想重用变量,那么可以退回到上层作用域,相当于exit当前作用域,如
withtf.variable_scope("root"): #Atstart,thescopeisnotreusing. asserttf.get_variable_scope().reuse==False withtf.variable_scope("foo"): #Openedasub-scope,stillnotreusing. asserttf.get_variable_scope().reuse==False withtf.variable_scope("foo",reuse=True): #Explicitlyopenedareusingscope. asserttf.get_variable_scope().reuse==True withtf.variable_scope("bar"): #Nowsub-scopeinheritsthereuseflag. asserttf.get_variable_scope().reuse==True #Exitedthereusingscope,backtoanon-reusingone. asserttf.get_variable_scope().reuse==False
一个作用域可以作为另一个新的作用域的参数,如:
withtf.variable_scope("foo")asfoo_scope: v=tf.get_variable("v",[1]) withtf.variable_scope(foo_scope): w=tf.get_variable("w",[1]) withtf.variable_scope(foo_scope,reuse=True): v1=tf.get_variable("v",[1]) w1=tf.get_variable("w",[1]) assertv1isv assertw1isw
不管作用域如何嵌套,当使用withtf.variable_scope()打开一个已经存在的作用域时,就会跳转到这个作用域。
withtf.variable_scope("foo")asfoo_scope: assertfoo_scope.name=="foo" withtf.variable_scope("bar"): withtf.variable_scope("baz")asother_scope: assertother_scope.name=="bar/baz" withtf.variable_scope(foo_scope)asfoo_scope2: assertfoo_scope2.name=="foo"#Notchanged.
variablescope的Initializers可以创递给子空间和tf.get_variable()函数,除非中间有函数改变,否则不变。
withtf.variable_scope("foo",initializer=tf.constant_initializer(0.4)): v=tf.get_variable("v",[1]) assertv.eval()==0.4#Defaultinitializerassetabove. w=tf.get_variable("w",[1],initializer=tf.constant_initializer(0.3)): assertw.eval()==0.3#Specificinitializeroverridesthedefault. withtf.variable_scope("bar"): v=tf.get_variable("v",[1]) assertv.eval()==0.4#Inheriteddefaultinitializer. withtf.variable_scope("baz",initializer=tf.constant_initializer(0.2)): v=tf.get_variable("v",[1]) assertv.eval()==0.2#Changeddefaultinitializer.
算子(ops)会受变量作用域(variablescope)影响,相当于隐式地打开了同名的名称作用域(namescope),如+这个算子的名称为foo/add
withtf.variable_scope("foo"): x=1.0+tf.get_variable("v",[1]) assertx.op.name=="foo/add"
除了变量作用域(variablescope),还可以显式打开名称作用域(namescope),名称作用域仅仅影响算子的名称,不影响变量的名称。另外如果tf.variable_scope()传入字符参数,创建变量作用域的同时会隐式创建同名的名称作用域。如下面的例子,变量v的作用域是foo,而算子x的算子变为foo/bar,因为有隐式创建名称作用域foo
withtf.variable_scope("foo"): withtf.name_scope("bar"): v=tf.get_variable("v",[1]) x=1.0+v assertv.name=="foo/v:0" assertx.op.name=="foo/bar/add"
注意:如果tf.variable_scope()传入的不是字符串而是scope对象,则不会隐式创建同名的名称作用域。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。