Title: How to get a tensorflow variable under certain namescope?
Tags: python;tensorflow
Question: Suppose we want fetch a value of a tensorflow variable ,we can just run it under a session.

Suppose ```a = tf.Variable(...)```

Then its value can be fetched using ```sess.run(a)```

But if there are two variables with same name but different name scope, how do I fetch the value of individual variables?

```with tf.name_scope("x"):
      a = tf.Variable(...)
with tf.name_scope("y"):
      a = tf.Variable(...)
```

Then how do I get values of ```a``` under ```x``` and ```a``` under ```y``` respectively?
If I do ```sess.run(a)```, I am getting value under name_scope ```y``` (recent one)

Comment: You are deliberately shadowing the previous variable `a`. What's the point of this?

Comment: Not familiar with tensorflow, but this looks like a Python issue not a tensorflow one: when you assign a, the original value is lost, regardless of the context in which it's assigned. You'll probably have to use a different variable under the other name scope. I'd suggest `x_a` and `y_a`.

Here is the accepted answer: You can checkout names of vars and get them by scope/names:

```with tf.variable_scope("x"):
    a = tf.get_variable('a', initializer=1)

with tf.variable_scope("y"):
    a = tf.get_variable('a', initializer=2)

with tf.Session() as s:
    s.run(tf.global_variables_initializer())
    [print(var.op.name) for var in tf.global_variables()]
    res = s.run(['x/a:0', 'y/a:0'])
    print(res)
```

returns:

```x/a
y/a
[1, 2]
```



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:01/05/18', 'FavoriteCount': 2.0, 'PostTypeId': 1, 'Tags': '<python><tensorflow>', 'Title': 'How to get a tensorflow variable under certain namescope?', 'ViewCount': 331, 'id': 48119449, 'user_AboutMe': '<p>Btech from National Institute of Techonology Tiruchirapalii</p>\n', 'user_AccountId': 7318933.0, 'user_DisplayName': 'phkr', 'user_Reputation': 119.0, 'user_Views': 32.0, 'user_id': 5714432.0}}