0

I want to access the variables from the list of variables to update it. I wonder if there is any way to do this with list rather than accessing variable as string described here

dWaa, dWax, dWya, db, dby = gradients['dWaa'], gradients['dWax'], gradients['dWya'], gradients['db'], gradients['dby']

for gradient in [dWax, dWaa, dWya, db, dby]:
    gradient = np.clip(gradient, -10, 10)
    # I need to update the value of variables(eg. dwaa) here

gradients = {"dWaa": dWaa, "dWax": dWax, "dWya": dWya, "db": db, "dby": dby}

How can I update the value of variable(eg. dwaa) inside the loop

  • I don't understand the question. When you run `gradient = np.clip(gradient, -10, 10)` inside the loop, on first iteration you are updating dWax, second iteration updating dWaa and so on... – Fábio Clug Feb 22 '18 at 03:21
  • so you want eg `dWaa` to be a modified copy of `gradients['dWaa']`? Otherwise why not just modify `gradients['dWaa']` directly. – avigil Feb 22 '18 at 03:50

1 Answers1

0

As I understand this, gradients should be a list of strings where the strings have the same name as your variables. So you could use

gradients = ["dWaa", "dWax", "dWya", "db", "dby"}
for gradient_name in gradients:
    gradient = np.clip(eval(gradient_name), -10, 10)

and manage the NameError which could be raised.