与TensorFlow一起工作,我正在尝试在不同的可变范围中共享相同的网络权重以节省内存。但是,似乎没有简单的方法可以做到这一点。我已经准备了一个小型代码样本,以在较小的规模上说明我想用较大的子网做什么:
import tensorflow as tf
graph = tf.Graph()
with graph.as_default():
with tf.variable_scope("super_scope_one"):
scope1 = tf.variable_scope("sub_scope_one")
with scope1:
number_one = tf.get_variable("number_one", shape=[1],
initializer=tf.ones_initializer)
with tf.variable_scope("super_scope_two"):
with tf.variable_scope("sub_scope_one", reuse=True) as scope2:
# Here is the problem.
# scope1.reuse_variables() # this crashes too if reuse=None.
number_one = tf.get_variable("number_one", shape=[1])
with tf.variable_scope("sub_scope_two"):
number_two = tf.get_variable("number_two", shape=[1],
initializer=tf.ones_initializer)
number_three = number_one + number_two
init_op = tf.global_variables_initializer()
with tf.Session(graph=graph):
init_op.run()
print(number_three.eval())
是否可以在两个子范围中共享变量,而无需删除上面的范围?如果没有,这是一个很好的理由,为什么这将是一个坏主意?
您可以简单地在"super_scope_one"
中仅定义number_one
并在"super_scope_two"
中使用它。
不同范围中的两个变量可以一起使用。请参阅下面:
import tensorflow as tf
graph = tf.Graph()
with graph.as_default():
with tf.variable_scope("super_scope_one"):
scope1 = tf.variable_scope("sub_scope_one")
with scope1:
number_one = tf.get_variable("number_one", shape=[1],
initializer=tf.ones_initializer)
with tf.variable_scope("super_scope_two"):
with tf.variable_scope("sub_scope_two"):
number_two = tf.get_variable("number_two", shape=[1],
initializer=tf.ones_initializer)
number_three = number_one + number_two
init_op = tf.global_variables_initializer()
with tf.Session(graph=graph):
init_op.run()
print(number_three.eval())
返回[2]