34
$\begingroup$

I am trying to merge two Keras models into a single model and I am unable to accomplish this.

For example in the attached Figure, I would like to fetch the middle layer $A2$ of dimension 8, and use this as input to the layer $B1$ (of dimension 8 again) in Model $B$ and then combine both Model $A$ and Model $B$ as a single model.

I am using the functional module to create Model $A$ and Model $B$ independently. How can I accomplish this task?

Note: $A1$ is the input layer to model $A$ and $B1$ is the input layer to model $B$.

See Picture

$\endgroup$
29
$\begingroup$

I figured out the answer to my question and here is the code that builds on the above answer.

from keras.layers import Input, Dense
from keras.models import Model
from keras.utils import plot_model

A1 = Input(shape=(30,),name='A1')
A2 = Dense(8, activation='relu',name='A2')(A1)
A3 = Dense(30, activation='relu',name='A3')(A2)

B2 = Dense(40, activation='relu',name='B2')(A2)
B3 = Dense(30, activation='relu',name='B3')(B2)

merged = Model(inputs=[A1],outputs=[A3,B3])
plot_model(merged,to_file='demo.png',show_shapes=True)

and here is the output structure that I wanted:

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Notice that you are not merging two models (in the sense of keras Model) in the above, you're merging layers. $\endgroup$ – gented Oct 7 '19 at 11:02
9
$\begingroup$

In Keras there is a helpful way to define a model: using the functional API. With functional API you can define a directed acyclic graphs of layers, which lets you build completely arbitrary architectures. Considering your example:

#A_data = np.zeros((1,30))
#A_labels = np.zeros((1,30))
#B_labels =np.zeros((1,30))

A1 = layers.Input(shape=(30,), name='A_input')
A2 = layers.Dense(8, activation='???')(A1)
A3 = layers.Dense(30, activation='???', name='A_output')(A2)


B2 = layers.Dense(40, activation='???')(A2)
B3 = layers.Dense(30, activation='???', name='B_output')(B2)

## define A
A = models.Model(inputs=A1, outputs=A3)

## define B
B = models.Model(inputs=A1, outputs=B3) 

B.compile(optimizer='??',
          loss={'B_output': '??'}
          )

B.fit({'A_input': A_data},
  {'B_output': B_labels},
  epochs=??, batch_size=??)

So, that's it! You can see the result by: B.summary():

Layer (type)                 Output Shape              Param    
A_input (InputLayer)         (None, 30)                0         
_________________________________________________________________
dense_8 (Dense)              (None, 8)                 248     
______________________________________________________________
dense_9 (Dense)              (None, 40)                360       
_________________________________________________________________
B_output (Dense)             (None, 30)                1230      
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks for the answer, but I don't think the above code will work. First, when you say B = models.Model(inputs=A2, outputs=B3) it will give you an error TypeError: Input layers to a Model must be InputLayer objects. Received inputs: Tensor. Also, as mentioned earlier, I did use functional API to create Model A and Model B separately. I think the answer I am looking for might have to do with the section "Multi-input and multi-output models" in keras documentation that uses concatenate function (not entire sure though). $\endgroup$ – Rkz Dec 29 '17 at 18:39
  • $\begingroup$ @Rkz : I have edited the answer. It works now. We have to use "concatenate". Actually, you should mention the main input (A1) when you want to define model "B". $\endgroup$ – moh Dec 29 '17 at 19:01
  • $\begingroup$ Thanks for your time and edits. I figured out the answer from the Keras documentation (see the following answer). I did not require concatenate for my question. $\endgroup$ – Rkz Dec 29 '17 at 19:14
  • $\begingroup$ @Rkz : Look at the final edit, I also show how to compile and fit the model. $\endgroup$ – moh Dec 29 '17 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.