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])
-
创建或返回给定名称的变量:
tf.get_variable(<name>, <shape>, <initializer>)
-
管理传给get_variable()的变量名的作用域,作为变量名的前缀:
tf.variable_scope(<scope_name>)
- 当前环境的作用域可以通过函数tf.get_variable_scope()获取,并且reuse标志可以通过调用reuse_variables()设置为True。
- 一旦设置为True,就不能返回到False,并且该作用域的子空间reuse都是True。如果不想重用变量,则退回到上层non-reusing作用域。
-
重用已经创建的变量:
reuse_variables()
、tf.get_variable_scope().reuse == True
-
当使用with tf.variable_scope()打开一个已经存在的作用域时,就会跳转到这个作用域.
如果tf.variable_scope()传入字符参数,创建变量作用域的同时会隐式创建同名的名称作用域。
tf.variable_scope()传入的不是字符串而是scope对象,则不会隐式创建同名的名称作用域。