-1

I want to create a NN with a given number of layers. For that, I want to loop through the variable hidden layers and initialize the weights for each of the layers. However, I need different names in order to store the different weights, so I want to name the W variables with a parameter inside them. Initializing as many as the parameter says

So, if: hidden_layers = 2. My variables to initialize are: w1, w2

If hidden_layers = 4 Then I would like to have: w1, w2, w3, w4

I want to initialize the variables with a for loop:

for i in range (hidden_layers):
    W + str(i) = tf.Variable(tf.initializers.GlorotUniform()(shape=[input_shape,code_length]),name='W1') #This is wrong!

Could someone help? Thanks!

user11400799
  • 143
  • 1
  • 3
  • 10
  • How would you even begin using them? – Peter Wood Jul 02 '19 at 21:45
  • 4
    No. You want a list, `w`. The if `hidden_layers = 2` you have `w[0]` and `w[1]` and if you are building neural networks you probable want a numpy array. – Mark Jul 02 '19 at 21:45
  • 1
    I can't imagine a situation where you would actually want to do this instead of using a list, but just in case someone in the future stumbles on this question and really needs to know how to do this, instead of `w + str(i) = ...` you can (but really, do not do this) do `eval(f"w{i} = ...")` – Dan Jul 02 '19 at 22:00
  • @Dan that is not guaranteed to work in local scopes. – juanpa.arrivillaga Jul 02 '19 at 23:24
  • @MarkMeyer Please see my updated question, I am more specific now – user11400799 Jul 03 '19 at 05:16
  • The answer is *still* use a list! `weights = []`, `for layer in hidden_layers:`, `weights.append( tf.Variable(tf.initializers.GlorotUniform()(shape=[input_shape,code_length]),name=f"W{layer}"))`. Also, 1 letter variables names are awful, don't do that. – Dan Jul 03 '19 at 08:47

2 Answers2

1

You can't name variables on the fly like that. If you want to have four "variables" you could use a list which would allow you to add as many "variables" as you want. If you don't want to use a list, you could also instantiate four objects.

variables_list = []
hidden_layers = 4
for i in range(hidden_layers):
    variables_list.append(i)

# set the variables as you wish
variables_list[0] = "value"
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
0

Use a list could be a solution as others suggested, but just in case if your "layers" are not increased in sequence, I would suggest you to use a dictionary:

my_variables = {}
hidden_layers = [1, 2, 4, 6]
for layer in hidden_layers:
    my_variables['w' + str(layer)] = ...

# To use it:
my_variables.get('w2')
YKL
  • 542
  • 2
  • 9