with tf.variable_scope('foo'):
a1 = tf.get_variable('a',[1],initializer=tf.constant_initializer(1))
print(a1)
with tf.variable_scope('foo',reuse=True):
a2 = tf.get_variable('a',[1])
print(a1 == a2)
第一次运行结果:
<tf.Variable 'foo/a:0' shape=(1,) dtype=float32_ref>
True
第二次运行就会报错:
ValueError: Variable foo/a already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
原因:with tf.variable_scope('foo'):
a1 = tf.get_variable('a',[1],initializer=tf.constant_initializer(1))只能用一次,第二次运行因为变量名不能重复就会报错
第二次应该只运行with tf.variable_scope('foo',reuse=True):
a2 = tf.get_variable('a',[1])
print(a1 == a2)
第二次结果:
True
ValueError: Variable foo/a already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AU
最新推荐文章于 2023-05-23 21:54:50 发布