Defining a multiple input model
In some cases, the sequential API will not be sufficiently flexible to accommodate your desired model architecture and you will need to use the functional API instead. If, for instance, you want to train two models with different architectures jointly, you will need to use the functional API to do this. In this exercise, we will see how to do this. We will also use the .summary()
method to examine the joint model's architecture.
Note that keras
has been imported from tensorflow
for you. Additionally, the input layers of the first and second models have been defined as m1_inputs
and m2_inputs
, respectively. Note that the two models have the same architecture, but one of them uses a sigmoid
activation in the first layer and the other uses a relu
.
This exercise is part of the course
Introduction to TensorFlow in Python
Exercise instructions
- Pass model 1's input layer to its first layer and model 1's first layer to its second layer.
- Pass model 2's input layer to its first layer and model 2's first layer to its second layer.
- Use the
add()
operation to combine the second layers of model 1 and model 2. - Complete the functional model definition.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# For model 1, pass the input layer to layer 1 and layer 1 to layer 2
m1_layer1 = keras.layers.Dense(12, activation='sigmoid')(____)
m1_layer2 = keras.layers.Dense(4, activation='softmax')(____)
# For model 2, pass the input layer to layer 1 and layer 1 to layer 2
m2_layer1 = keras.layers.Dense(12, activation='relu')(____)
m2_layer2 = keras.layers.Dense(4, activation='softmax')(____)
# Merge model outputs and define a functional model
merged = keras.layers.add([m1_layer2, ____])
model = keras.Model(inputs=[____, m2_inputs], outputs=____)
# Print a model summary
print(model.summary())