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.