Search code examples
pythonkeraskeras-layertf.keras

Function to build Keras model with arbitrary number of layers


I want to see what happens to my model when I vary the number of layers it has.

I wrote a function to build, compile and fit a model with a custom number of layers. But it keeps making a (seemingly) identical model each time built with (what looks like) just one layer.

The code

def custom_num_layer_model(num_layers):
    dense_layers = [Dense(16, activation='relu')] * num_layers
    all_layers = dense_layers + [Dense(1, activation='sigmoid')]

    model = Sequential(all_layers)
    
    model.compile(optimizer='rmsprop',
                  loss='binary_crossentropy',
                  metrics=['accuracy'])
    
    history = model.fit(x_train,
                        y_train,
                        epochs=20,
                        batch_size=512,
                        validation_split=0.4)
    return history

As I was writing this, I realised it must be the line dense_layers = [Dense(16, activation='relu')] * num_layers. This must duplicate the exact layer in the list and thus render the copies useless.

So, how would I write a function to automate this process of building models with a custom number of layers?


Solution

  • Figured it out!

    Use a list comprehension on the first line of the function.

    dense_layers = [Dense(16, activation='relu') for _ in range(num_layers)]
    

    You must initialize num_layers new objects and so could also do this with a for loop.

    Using list * num_layers just creates a new list with num_layers copies of the original object. Because it's the same object, it was like having a single-layer network.

    Using a list comprehension creates num_layers distinct new objects and thus it works.