TensorFLow 变量命名空间实例
一、name_scope
withtf.name_scope(name):
name_scope:为了更好地管理变量的命名空间而提出的。比如在tensorboard中,因为引入了name_scope,我们的Graph看起来才井然有序。
name_scope对get_variable创建变量的name没有影响,即get_variable创建的变量不在name_scope这个命名空间中
二、variable_scope
withtf.variable_scope(name_or_scope,reuse=None):
variable_scope:大部分情况下,跟tf.get_variable()配合使用,实现变量共享的功能
可通过tf.get_variable_scope().reuse==True/False判断参变量是否共享
当前变量作用域可以用tf.get_variable_scope()进行检索并且reuse标签可以通过调用tf.get_variable_scope().reuse_variables()设置为True
三、共享参变量
1、方法
使用tf.Variable()创建同一个name的变量(操作名不同),均不会报错,但系统会自动修改name(实质还是不让共享参变量)
使用tf.get_varible()创建同一个name的变量(操作名不同),均会报错(为了避免无意识的参变量复用造成的错误)
我们可以在variable_scope中使用tf.get_variable()创建变量,并通过withtf.variable_scope(name_or_scope,reuse=True)来共享参变量:
reuse=True:将只能获取命名空间中已经创建过的变量,如果变量不存在,则tf.get_variable函数将报错。
reuse=None/False:tf.get_variable操作将创建新的变量,如果同名的变量已经存在,则tf.get_variable函数将报错。
2、代码示例
#下面是定义一个卷积层的通用方式
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_intializer(0.0))
conv=tf.nn.conv2d(input,weights,
strides=[1,1,1,1],padding='SAME')
returntf.nn.relu(conv+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])
#实验一:调用my_image_filter()两次
result1=my_image_filter(image1)
result2=my_image_filter(image2)
>>>RaisesValueError(...conv1/weightsalreadyexists...),tf.get_variable()会检测已经存在的变量是否已经共享
#解决方法一,可以在设计网络时加上一个布尔型的reuse参数
withtf.variable_scope("image_filters"):
result1=my_image_filter(image1)
withtf.variable_scope("image_filters",reuse=True):
result2=my_image_filter(image2)
#解决方法二
withtf.variable_scope("image_filters")asscope:
#下面我们两次调用my_image_filter函数,但是由于引入了变量共享机制
#可以看到我们只是创建了一遍网络结构。
result1=my_image_filter(image1)
scope.reuse_variables()
result2=my_image_filter(image2)
#解决方法三
withtf.variable_scope("image_filters")asscope:
result1=my_image_filter(image1)
withtf.variable_scope(scope,reuse=True):
result2=my_image_filter(image2)
#打印出所有的可训练参变量
vs=tf.trainable_variables()
print('Thereare%dtrainable_variablesintheGraph:'%len(vs))
forvinvs:
print(v)
#输出结果证明确实:参变量共享,因为只有四个变量,没有创建新的变量。
Thereare4trainable_variablesintheGraph:
Tensor("image_filters/conv1/weights/read:0",shape=(5,5,32,32),dtype=float32)
Tensor("image_filters/conv1/biases/read:0",shape=(32,),dtype=float32)
Tensor("image_filters/conv2/weights/read:0",shape=(5,5,32,32),dtype=float32)
Tensor("image_filters/conv2/biases/read:0",shape=(32,),dtype=float32)
四、取出所有可训练参数
#Returnsallvariablescreatedwithtrainable=Trueinavar_list var_list=tf.trainable_variables() init=tf.global_variables_initializer() sess.run(init) forvarinvar_list: sess.run(var)
以上这篇TensorFLow变量命名空间实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。