可以改变通过tf.variable_scope()定义的变量中的参数reuse,若reuse为False,不可用tf.get_variable()调用该变量,tf.get_variable_scope().reuse_variables()可将reuse从False改为True
看代码(tf.get_variable_scope().reuse用于打印此时变量的reuse状态):
没有使用tf.get_variable_scope().reuse_variables()的结果(初始化变量默认reuse为False):
import tensorflow as tf
with tf.variable_scope('a1'):
print(tf.get_variable_scope().reuse)
with tf.variable_scope('a3'):
print(tf.get_variable_scope().reuse)
with tf.variable_scope('a4'):
# tf.get_variable_scope().reuse_variables()
print(tf.get_variable_scope().reuse)
False
False
False
a4使用了tf.get_variable_scope().reuse_variables()的结果:
import tensorflow as tf
with tf.variable_scope('a1'):
print(tf.get_variable_scope().reuse)
with tf.variable_scope('a3'):
print(tf.get_variable_scope().reuse)
with tf.variable_scope('a4'):
tf.get_variable_scope().reuse_variables()
print(tf.get_variable_scope().reuse)
False
False
True