title: Multi task learning in Keras
body: <p>I am trying to implement shared layers in Keras. I do see that Keras has <code>keras.layers.concatenate</code>, but I am unsure from documentation about its use. Can I use it to create multiple shared layers? What would be the best way to implement a simple shared neural network as shown below using Keras? <a href="https://dam-prod.media.mit.edu/thumb/2017/12/12/Screen%20Shot%202017-12-12%20at%2011.50.03%20AM.png.1400x1400.png" rel="nofollow noreferrer"><img src="https://dam-prod.media.mit.edu/thumb/2017/12/12/Screen%20Shot%202017-12-12%20at%2011.50.03%20AM.png.1400x1400.png" alt="Shared Neural network"></a></p>
  
  <hr>
  
  <p><strong>Edit 1:</strong>
  Note that all the shape of input, output and shared layers for all 3 NNs are the same. There are multiple shared layers (and non-shared layers) in the three NNs. The coloured layers are unique to each NN, and have same shape.</p>
  
  <p>Basically, the figure represents 3 identical NNs with multiple shared hidden layers, followed by multiple non-shared hidden layers.</p>
  
  <p>I am unsure how to share multiple layers as in the Twitter example, there was just one shared layer (example in API doc).</p>
  
  <hr>
  
  <p><strong>Edit 2:</strong>
  Based on geompalik's helpful comments, this is what I initially came up with:</p>
  
  <pre><code>sharedLSTM1 = LSTM((data.shape[1]), return_sequences=True)
  sharedLSTM2 = LSTM(data.shape[1])
  def createModel(dropoutRate=0.0, numNeurons=40, optimizer='adam'):
      inputLayer = Input(shape=(timesteps, data.shape[1]))
      sharedLSTM1Instance = sharedLSTM1(inputLayer)
      sharedLSTM2Instance =  sharedLSTM2(sharedLSTM1Instance)
      dropoutLayer = Dropout(dropoutRate)(sharedLSTM2Instance)
      denseLayer1 = Dense(numNeurons)(dropoutLayer)
      denseLayer2 = Dense(numNeurons)(denseLayer1)
      outputLayer = Dense(1, activation='sigmoid')(denseLayer2)
      return (inputLayer, outputLayer)
  
  inputLayer1, outputLayer1 = createModel()
  inputLayer2, outputLayer2 = createModel()
  model = Model(inputs=[inputLayer1, inputLayer2], outputs=[outputLayer1, outputLayer2])
  model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
  </code></pre>
  
  <p>In the above code, I expect that the LSTM Layers in the two models are shared, whereas the dropout and 2 dense layers are not shared. Is that correct?</p>
  
  <p>If so, I do not need <code>keras.layers.concatenate</code> in this example, right?</p>
  
  <p>I get the following image if I try to visualise the network using <code>plot_model</code> (which is what I was expecting):</p>
  
  <p><a href="https://i.stack.imgur.com/3dW1r.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3dW1r.png" alt="model plot"></a></p>