2

I want to join two sequential layers in Tensorflow in the below mentioned manner.

Suppose, my first model (model 1) is the below:

from tensorflow.keras import layers, models, Sequential
from tensorflow.keras.layers import concatenate, Add, Dense

models.Sequential([
            layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
            layers.MaxPooling2D((2, 2))
])

And my second model (model 2) is the below:

model2 = models.Sequential([
            layers.Conv2D(64, (3, 3), activation='relu'),
            layers.MaxPooling2D((2, 2)),
            layers.Conv2D(64, (3, 3), activation='relu')
])

I want to join model1 and model2 to get the below arrangement of layers.

_________________________________________________________________
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d_1 (Conv2D)          (None, 30, 30, 32)        896       
                                                                 
 max_pooling2d_1 (MaxPoolin  (None, 15, 15, 32)       0         
 g2D)                                                            
                                                                 
 conv2d_2 (Conv2D)          (None, 13, 13, 64)        18496     
                                                                 
 max_pooling2d_2 (MaxPoolin  (None, 6, 6, 64)         0         
 g2D)                                                            
                                                                 
 conv2d_3 (Conv2D)          (None, 4, 4, 64)          36928     
                                                                 
=================================================================
Total params: 56,320
Trainable params: 56,320
Non-trainable params: 0
_________________________________________________________________

At present, I do it using the below code (which works fine). But, I want to know if there is a more elegant solution to this task than the below mentioned for loop.

for i in range( len( model2.layers ) ):
    model1.add(model2.layers[i])

What is the best way to join model1 and model2 in the aforesaid desired manner in Tensorflow? Here, model1 and model2 are just two randomly picked examples. I would want the solution to work for any other two compatible models also.

I tried using tensorflow.keras.layers.Concatenate and merging of layers in Keras, but I have not been able to get any of these to work. I also tried this, this and this questions and solutions in Stackoverflow. But, I could not get these to work for me.

Ji Won Song
  • 140
  • 2
  • 9

1 Answers1

1

The Model class inherits Layer. This means any model can be used as a layer as well. Sequential takes a sequence of layers. And so, you can just do

combined_model = tf.keras.Sequential([model1, model2])

Note: When you do this, combined_model.summary() will only show two layers, namely the two sequential models it consists of. If you want to see the "sublayers" as well, you can use combined_model.summary(expand_nested=True).

xdurch0
  • 9,905
  • 4
  • 32
  • 38