tensorflow的变量命名空间问题

参考:https://blog.csdn.net/legend_hua/article/details/78875625

在tensorflow中,tf.get_variable函数的命名空间与tf.variable_scope()有关,与tf.name_space()无关。但是如果tf.name_space命名了某个变量,tf.get_variable()获取时会有冲突,他们在一个全局空间中,类似于c++的namespace。注意:在同一个scope中,同样名字的name_scope被声明第二次时, scope的名字并不会直接被复用出现,而是改名创建一个全新的scope。代码:

with tf.name_scope('a_1'):
  with tf.name_scope('a_2'):
    with tf.variable_scope('a_3'):
      d = tf.Variable(1, name='d')
      print(d.name)    #输出a_1/a_2/a_3/d:0


    with tf.variable_scope('a_3'):
      e = tf.Variable(1, name='e')
      print(e.name)    #输出a_1/a_2/a_3_1/d:0

猜你喜欢

转载自www.cnblogs.com/jianglinliu/p/10461836.html