Keras: multiple inputs & outputs

from keras.models import Model
from keras.layers import Input, Dense

input_1 = Input(..., name='input_1')
x = Dense(...)(input_1)
x = Dense(...)(x)
...
output_1 = Dense(dim_output_1, ..., name='output_1')

input_2 = Input(..., name='input_2')
x = keras.layers.concatenate([output_1, input_2])
x = Dense(...)(x)
x = Dense(...)(x)
x = Dense(...)(x)
output_2 = Dense(dim_output_2, ..., name='output_2')(x)

model = Model(inputs=[input_1, input_2],
              outputs=[output_1, output_2])
model.compile(...,
    loss={'output_1': 'binary_crossentropy',
          'output_2': 'binary_crossentropy'},
    loss_weights={'output_1': weight_1.,
                  'output_2': weight_2})
model.fit({'input_1': data_input_1,
           'input_2': data_input_2},
          {'output_1': data_output_1,
           'output_2': data_output_2},
          ...)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s