What is the most efficient way to modify a Keras Model?

Viewed 1130

Is there a way to add nodes to a layer in an existing Keras model? if so, what is the most efficient way to do so?

Also, is it possible to do the same but with layers? i.e. add a new layer to an existing Keras model (for example, right after the input layer).

One way I know of is to use Keras functional API by iterating and cloning each layer of the model in order to create a "copy" of the original model with the desired changes, but is it the most efficient way to accomplish this task?

1 Answers

You can take the output of a layer in a model and build another model starting from it:

import tensorflow as tf

# One simple model
inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4, activation='relu')(inputs)
outputs = tf.keras.layers.Dense(5, activation='softmax')(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)

# Make a second model starting from layer in previous model
x2 = tf.keras.layers.Dense(8, activation='relu')(model.layers[1].output)
outputs2 = tf.keras.layers.Dense(7, activation='softmax')(x2)
model2 = tf.keras.Model(inputs=model.input, outputs=outputs2)

Note that in this case model and model2 share the same input layer and first dense layer objects (model.layers[0] is model2.layers[0] and model.layers[1] is model2.layers[1]).

Related