Keras Sequential model with multiple inputs

To solve this problem you have two options.

1. Using a sequential model

You can concatenate both arrays into one before feeding to the network. Let’s assume the two arrays have a shape of (Number_data_points, ), now the arrays can be merged using numpy.stack method.

merged_array = np.stack([array_1, array_2], axis=1)

model0 = keras.Sequential([
keras.layers.Dense(2, input_dim=2, activation=keras.activations.sigmoid, use_bias=True),
keras.layers.Dense(1, activation=keras.activations.relu, use_bias=True),
])

model0.fit(merged_array,output, batch_size=16, epochs=100)

2. Using Functional API.

This is the most recommened way to use when there are multiple inputs to the model.

input1 = keras.layers.Input(shape=(1, ))
input2 = keras.layers.Input(shape=(1,))
merged = keras.layers.Concatenate(axis=1)([input1, input2])
dense1 = keras.layers.Dense(2, input_dim=2, activation=keras.activations.sigmoid, use_bias=True)(merged)
output = keras.layers.Dense(1, activation=keras.activations.relu, use_bias=True)(dense1)
model10 = keras.models.Model(inputs=[input1, input2], output=output)

Now you can use the second method you have trying to fit to the model

model0.fit([array_1, array_2],output, batch_size=16, epochs=100)

Leave a Comment