简体   繁体   中英

How do I add layers at the start of a model in Keras?

I want to add new layers to a pre-trained model, using Tensorflow and Keras. The problem is, those new layers are not to be added on top of the model, but at the start. I want to create a triple-siamese model, which takes 3 different inputs and gives 3 different outputs, using the pre-trained network as the core of the model. For that, I need to insert 3 new input layers at the beginning of the model.

The default path would be to just chain the layers and the model, but this method treats the pre-trained model as a new layer (when a new model with the new inputs and the pre-trained model is created, the new model only contains4 layers, the 3 input layers, and the whole pre-trained model):

input_1 = tf.keras.layers.Input(shape = (224,224,3))
input_2 = tf.keras.layers.Input(shape = (224,224,3))
input_3 = tf.keras.layers.Input(shape = (224,224,3))

output_1 = pre_trained_model(input_1)
output_2 = pre_trained_model(input_2)
output_3 = pre_trained_model(input_3)

new_model = tf.keras.Model([input_1, input_2, input_3], [output_1, output_2, output_3])    

new_model has only 4 layers, due to the Keras API considering the pre_trained_model a layer.

I know that the above option works, as I have seen in many code samples, but I wonder if there is a better option for this. It feels awkward for me, because the access to inner layers of the final model will be messed up, not to mention the fact that the model will have an extra input layer after the added 3 input layers (the input layer from the pre trained model is still intact, and is totally unnecessary).

No, this does not add layers, you are making a multi-input multi-output model, where each siamese branch shares weights. There is no other API in Keras to do this, so this is your only option.

And you can always access the layers of the inner model through the pre_trained_model variable, so there is nothing lost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM