Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current TensorFlow name scope

Tags:

tensorflow

I create relative name scopes with tf.name_scope.

How can I get the current absolute name scope?

From the code, it looks like tf.get_default_graph()._name_stack would give me that but that looks like a non-official way. Is there any official way? (I think not, thus I made an upstream feature request.)

(I implemented a bunch of functions like get_current_name_scope() or reuse_name_scope() here. Note that you need to be careful when mixing tf.name_scope and tf.variable_scope.)

like image 718
Albert Avatar asked Dec 01 '16 10:12

Albert


1 Answers

Example:

import tensorflow as tf

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

Output:

foo/bar/

EDIT:

Without access to the enclosing context manager I don't know of a specific function to access the current name scope. With a small detour it's possible as follows:

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

like image 101
ben Avatar answered Sep 20 '22 06:09

ben