Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a variable which respects the name scope?

Tags:

tensorflow

Given x, y are tensors, I know I can do

with tf.name_scope("abc"):
    z = tf.add(x, y, name="z")

So that z is named "abc/z".

I am wondering if there exists a function f which assign the name directly in the following case:

with tf.name_scope("abc"):
    z = x + y
    f(z, name="z")

The stupid f I am using now is z = tf.add(0, z, name="z")

like image 599
colinfang Avatar asked Dec 21 '15 15:12

colinfang


People also ask

How can you rename a variable?

To rename a variable:Right-click the variable and select Rename.

How can I change all occurrences of a variable name at the same time in my program?

Place the cursor inside a variable you want to rename and press Ctrl + R . Then type other name and editor will change all occurrences of that variable. Save this answer.

How do you change the name of a variable in C++?

Drag a 'Create variable' block into the block editor. Click the name (item) and select 'Rename variable...' Type in the new name and click enter and now the variable has a new name.


1 Answers

If you want to "rename" an op, there is no way to do that directly, because a tf.Operation (or tf.Tensor) is immutable once it has been created. The typical way to rename an op is therefore to use tf.identity(), which has almost no runtime cost:

with tf.name_scope("abc"):
    z = x + y
    z = tf.identity(z, name="z")

Note however that the recommended way to structure your name scope is to assign the name of the scope itself to the "output" from the scope (if there is a single output op):

with tf.name_scope("abc") as scope:
    # z will get the name "abc". x and y will have names in "abc/..." if they
    # are converted to tensors.
    z = tf.add(x, y, name=scope)

This is how the TensorFlow libraries are structured, and it tends to give the best visualization in TensorBoard.

like image 197
mrry Avatar answered Oct 25 '22 12:10

mrry