0

I would like to assign/modify the values of my variables and I wish to do so by calling them by their name.

For exemple:

vars = tf.trainable_variables()
print(vars[1].name)

'matrix1:0'

upt = vars['matrix1:0'].assign_add(tf.constant(1))
sess.run(upt)

The reason for this is that indexes for variables aren't reliable as they are dependant as to when they are ran in the code. By adding a new variable, all the indexes would need to be shifted, which is not convenient. Using names would make my life much easier.

4
  • 1
    How about vars={v.name:v for v in tf.trainable_variables()} Commented Jun 8, 2016 at 18:57
  • Now, how do you assign new values to the variables by calling them by their name? Commented Jun 8, 2016 at 19:30
  • 1
    And then you can do vars['matrix1:0'].assign_add(tf.constant(1)) Commented Jun 8, 2016 at 19:40
  • Great, works well! Thank you. Mind adding a reply so I can mark your comment as the answer? Commented Jun 8, 2016 at 20:12

1 Answer 1

3

You can use Python generator expression to construct a dictionary like this

vars={v.name:v for v in tf.trainable_variables()}

and then you modify the variable as

vars['matrix1:0'].assign_add(tf.constant(1))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.