2016-12-01 27 views
3

我使用tf.name_scope創建了相對名稱範圍。如何獲得當前的TensorFlow名稱範圍

如何獲取當前的絕對名稱範圍?

從代碼看,tf.get_default_graph()._name_stack看起來像是給了我,但看起來像一個非官方的方式。有沒有官方的方式? (我想不會,所以我做了一個upstream feature request

(我實現了一堆的功能,如get_current_name_scope()reuse_name_scope()here請注意,你需要混合tf.name_scopetf.variable_scope時要小心。)

+0

你有沒有想過如何做到這一點,而無需訪問上下文管理器?或者那是不可能的? –

回答

1

例子:

import tensorflow as tf 

with tf.name_scope("foo"): 
    with tf.name_scope("bar") as absolute: 
     print(absolute) 

輸出:

foo/bar/ 

編輯:

沒有訪問封閉的上下文管理器我不知道訪問當前名稱範圍的特定功能。用小彎路有可能如下:

import tensorflow as tf 

with tf.name_scope("foo"): 
    with tf.name_scope("bar") as absolute: 
     print(tf.no_op(name='.').name[:-1]) 

Relevant Doc

+0

這假設我可以訪問'絕對'。但是我問,如果我不知道「絕對」,我怎麼能得到它? – Albert

+0

我看到了,已更新。 – ben

+0

所以,你的答案基本上是'tf.no_op(name ='。')。name [: - 1]'。這真的是最好的方法嗎?比'tf.get_default_graph()._ name_stack'更好嗎? – Albert