Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine 2 trained models in Keras

Tags:

merge

keras

I want to to concatenate the last layer before the output of 2 trained models and have a new model that uses the merged layer to give predictions. below is the relevant parts of my code:

model1 = load_model("model1_location.model")
model2 = load_model("model1_location.model")
merged_model = Sequential(name='merged_model')
merged_model.add(merge([model1.layers[-1],model2.layers[-1]]))
merged_model.add(Dense(3, activation='softmax'))

The above code gives the following error:

ValueError: Layer merge_2 was called with an input that isn't a symbolic tensor. Received type: <class 'keras.layers.core.Dense'>.

What is the correct way to combine those models, Alternatively how do I get a symbolic tensor from a layer?

like image 775
Alonzorz Avatar asked Oct 29 '22 05:10

Alonzorz


1 Answers

you need to get the output attribute like so:

merged_model.add(merge([model1.layers[-1].output, model2.layers[-1].output]))

like image 86
louis_guitton Avatar answered Jan 02 '23 19:01

louis_guitton