How can I connect the outputs of 2 keras models into one seperate layer?

Viewed 136

I'm trying to solve a problem where I initially need to get 2 inputs, pass each of them through layers of autoencoders and then connect everything to a layer that will connect to both of the autoencoder outputs.

This is how the model should look at the end

I've already created the autoencoder layers and saved them. I'm having trouble with finding out how to connect both of the outputs of the encoded data to one layer. An advice would be very appreciated.

edit:

I've been looking into the concatenate layer, and it seems like it should do the job. But, I still can't figure out how to connect the final layers of the autoencoders to the concatenae layer.

The layers of autoencoders are shaped like this:

input_1 (InputLayer)         [(None, 85)]                       
_________________________________________________________________
dense (Dense)                (None, 54)                     
_________________________________________________________________
dense_2 (Dense)              (None, 36)                      

And the forming of the concatenate layer:

connector = keras.layers.Concatenate()([autoencoder1.layers[2], autoencoder2.layers[2]])

autoencoder1, autoencoder2 are both the loaded autoencoder model layers.

It throws the following error:

TypeError: 'NoneType' object is not subscriptable
2 Answers

Try using the Concatenate layer. It takes multiple tensors and concatenates them to a single tensor.

https://keras.io/api/layers/merging_layers/concatenate/

According to your edited question, I understand that you want to share the layers of your encoder, meaning you want to use the same weights with your two inputs. If I understand correctly, the network should look something like this:

from keras import Sequential, Model
from keras.layers import Input, Dense, Concatenate

encoder = Sequential([
        Dense(54),
        Dense(36)])
input_1 = Input(85)
input_2 = Input(85)
encoded_1 = encoder(input_1)
encoded_2 = encoder(input_2)
concat = Concatenate()([encoded_1, encoded_2])
...
model = Model(inputs=[input_1, input_2], outputs=[...])
Related