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:

JavaScript

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:

JavaScript

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

JavaScript

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.

JavaScript

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.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement