Right now, I would like to create a tf.keras nn out of a list of lists that contain information about how a layer (layer-name, units and activation function) should look like. One inner list is one layer.
[['Dense', '32', 'relu'], ['Dropout', '0.5', 'placeholder'], ['Dense', '16', 'relu'], ['Dense', '1', 'sigmoid']]
Now, I wanted to know how it is possible to create a model out of these lists and their information. It should be noted that this is just one example. Ideally, the model should be flexibly build according to the length of the list (four inner lists = four layers). While the first inner list is supposed to be the first layer and the last inner list the output layer. Also, as you can see I used a placeholder for the dropout (I only set the dropout rate) otherwise I run into an out of range error
.
At first I tried out some list comprehensions such as below in combination with a dict to initialize the layer class but now I am just stuck at that point:
model_layers = [['Dense', '32', 'relu'], ['Dropout', '0.5', 'placeholder'], ['Dense', '16', 'relu'], ['Dense', '1', 'sigmoid']]
layer_type = [layer[0] for layer in model_layers]
layer_value = [layer[1] for layer in model_layers]
layer_act_function = [layer[2] for layer in model_layers]
layer_dict = {
'Dense': layers.Dense(),
'Dropout': layers.Dropout(),
}
for layer in range(len(layer_type)):
x = layer_dict[layer]
# ...
Ultimately, the goal is to create a model based on that elements in the inner list.
x = layers.Dense(32, activation='relu')(features)
x = layers.Dropout(rate=0.5)(x)
x = layers.Dense(16, activation='relu')(x)
output = layers.Dense(1, activation='sigmoid')
The reason for this question was that the model is constructed on user input via a form with input fields in which layers can be added deleted and set up with units and activation functions which are stored in the list model_layers
.
CodePudding user response:
@furas this approach worked out well for the question. Additionally, I added some slicing to handle the last (output) layer.
You are also right about the Dropout layer. For now, I will focus on Dense layers and try to include other types of layers later on.
model_layers = [
['Dense', '64', 'relu'],
['Dense', '32', 'relu'],
['Dense', '16', 'relu'],
['Dense', '1', 'sigmoid']]
layer_dict = {
'Dense': layers.Dense,
#'Dropout': layers.Dropout
}
# first layer
x = layer_dict[model_layers[0][0]](units=int(model_layers[0][1]),
activation=model_layers[0][2])(all_features_list)
for layer_type, layer_units, layer_act_function in model_layers[1:-1]:
x = layer_dict[layer_type](units=int(layer_units), activation=layer_act_function)(x)
if model_layers[-1]: # if output layer provide output layer data
output = layer_dict[layer_type](units=int(model_layers[-1][1]),
activation=model_layers[-1][2])(x)
model = keras.Model(all_inputs_list, output)
model.compile(dl_model_dl_opti, dl_model_loss, metrics=[dl_model_metric])