Multi Input and Multi Output Models in Keras

The Keras functional API is used to define complex models in deep learning . On of its good use case is to use multiple input and output in a model. In this blog we will learn how to define a keras model which takes more than one input and output.

Multi Output Model

Let say you are using MNIST dataset (handwritten digits images) for creating an autoencoder and classification problem both. In that case, you will be having single input but multiple outputs (predicted class and the generated image). Let take a look into the code.

In the above code we have used a single input layer and two output layers as ‘classification_output’ and ‘decoder_output’. Let’s see how to create model with these input and outputs.

Now we have created the model, the next thing is to compile this model. Here we will define two loss functions for both outputs. Also we can assign weights for both losses. See code.

Multi Input Model

Let’s take an example where you need to take two inputs: one grayscale image and another RGB image. Using these two images you want to do an image classification. To perform this, we will use Keras functional API. Let’s see code.

In the above code, we have extracted two different feature layers from both inputs and then concatenated both to create output layer. And created model with two inputs and one output.

A nice example where you can you use both multi input and multi output is capsule network. If you want to take a look into this, refer this blog.

Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

4 thoughts on “Multi Input and Multi Output Models in Keras

  1. madhuri

    Hey, I am trying the develop the multi-output model However while prediction I am getting strange results and unable to visualize it.

    I have developed the model having 1 input and 3 output and model is working fine without any error. But while prediction (model.predict(input)) I should get 3 samples, one for each output, however i am getting 516 output samples.
    Could you please help me in this.

    Thanks

    Reply
    1. kang & atul Post author

      As you are saying that the model is working fine, so the only issue I think is that the output arrays are concatenated in the list. You might have done something like this

      One approach is you do pred[0][i],pred[1][i] and pred[2][i] to access the 3 outputs corresponding to the ith example.
      Another thing to do is define 3 outputs as

      Hope this helps. If it still doesn’t resolve, please provide the code.

      Reply
  2. David

    Hi,

    If you feed 2 images as input, how do you keep the consistency between image pairs? Could you elaborate on that more? Thanks

    Reply
  3. Biniyam Abuhay

    i was working with vgg16 and mobile net,how do i use “model.fit()” when i have two inputs. to be specific how to pass train data set for both

    Reply

Leave a Reply to madhuri Cancel reply