tf.variable_scope和tf.name_scope的区别

简单地说,get_variable所在的命名空间受到tf.variable_scope影响,而不会受到tf.name_scope的命名空间的影响,即在tf.name_scope的命名空间a中使用get_variable声明一个名字为b的变量,那么这个变量的名字应为b而不是a/b。具体用法见下面的代码:

import tensorflow as tf

with tf.variable_scope('foo'):
    a = tf.get_variable('bar',[1])
    #打印foo/bar:0
    print(a.name)

with tf.variable_scope('bar'):
    b = tf.get_variable('bar',[1])
    #打印bar/bar:0
    print(b.name)

with tf.name_scope('a'):
    a = tf.Variable([1])
    #打印a/Variable:0
    print(a.name)

    a = tf.get_variable('b',[1])
    #由于不受name_scope影响,因此打印b:0
    print(a.name)

with tf.name_scope('b'):
    a = tf.get_variable('b',[1])
    #因为不受name_scope影响,因此应该打印出b:0,又因为之前已经声明过,因此会报错
    print(a.name)

猜你喜欢

转载自blog.csdn.net/a13602955218/article/details/80934676