Skip to content
Advertisement

How to add code lines when calling a function depending on an iterator on Python

I am trying to test many ML models using keras.models.Sequential.

My idea is that once I have an iterator that looks like [num_layers, num_units_per_layers], for example [(1, 64),(2, (64,128))], to create a script using a kind of for loop running the iterator to be able to create a keras sequential model with the number of layers and units in each step of the iterator.

This is what I am trying:

it = [[(1, 128),(2, (64,128)), (3,(128,64,256))]]
for layers, units in it:
    model = keras.Sequential([
        layers.Dense(units[0])
        #How to get another layers here when layers > 1.
    ])

But I am stuck when adding new layers automatically. To sum up, what I want in each step of the iterator is the keras model represented by its values. Is there any way to do this?

For example, when layers = 2 and units = (64,128) the code should look like:

model = keras.Sequential([
    layers.Dense(64),
    layers.Dense(128)
])

If layers = 1 and units = 128 the code must be:

model = keras.Sequential([
    layers.Dense(128)
])

Advertisement

Answer

Well the first issue is the way you set up it. The way you’re doing it makes it a single list, where you want a list of n lists (here n is 3). If you define it as follows, you can extract layers, units the way you are looking for.

it = [[1,[128]],[2,(64,128)],[3,(128,64,256)]]

If you want a model with one layer, you need to put the number of units in brackets, or it won’t work well with the other architectures (because of indexing). Next, there are some necessary tweaks to the code that I would suggest. First I would use a different way to build a Sequential model (shown below). Then, you would need to define your input shape otherwise the model will not know how to build. Finally, just create an output layer for each model outside the hidden layer generator loop.

I wrote this toy problem to fit your idea of iterating through models for 10 training samples and one input dimension and one output dimension.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import numpy as np

x = np.random.rand(10,1)
y = np.random.rand(10,1)

it = [[1,[128]],[2,(64,128)],[3,(128,64,256)]]
for layers, units in it:
    model = Sequential()
    for i in range(layers):
        model.add(Dense(units[i],input_shape=(1,)))
    model.add(Dense(1))
    model.summary()
    model.compile(loss='mse',optimizer='Adam')
    model.fit(x,y,batch_size=1,epochs=1)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement