Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras Sequential model with multiple inputs

I am making a MLP model which takes two inputs and produces a single output.

I have two input arrays (one for each input) and 1 output array. The neural network has 1 hidden layer with 2 neurons. Each array has 336 elements.

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),
])

# Compile the neural network #
model0.compile(
    optimizer = keras.optimizers.RMSprop(lr=0.02,rho=0.9,epsilon=None,decay=0),
    loss = 'mean_squared_error',
    metrics=['accuracy']
)

I tried two ways, both of them are giving errors.

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

ValueError: Error when checking input: expected dense_input to have shape (2,) but got array with shape (336,)

The second way:

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

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 2 arrays:

Similar question. But not using sequential model.

like image 722
ghostwar 88 Avatar asked Mar 19 '19 03:03

ghostwar 88


People also ask

How do I input multiple inputs in Keras model?

Make a model and compile it Now that you've input and output layers for the 3-input model, wrap them up in a Keras model class, and then compile the model, so you can fit it to data and use it to make predictions on new data.

Does functional API can handle models with non-linear topology shared layers and even multiple inputs or outputs?

The functional API can handle models with non-linear topology, shared layers, and even multiple inputs or outputs. The main idea is that a deep learning model is usually a directed acyclic graph (DAG) of layers. So the functional API is a way to build graphs of layers.

What is sequential model in Keras?

From the definition of Keras documentation the Sequential model is a linear stack of layers.You can create a Sequential model by passing a list of layer instances to the constructor: from keras.models import Sequential from keras.layers import Dense, Activation model = Sequential([ Dense(32, input_shape=(784,)), ...


2 Answers

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)

like image 74
Mitiku Avatar answered Sep 22 '22 09:09

Mitiku


As in the answer you've linked, you cant be using the Sequential API for the stated reason. You should use Model API which is also called the functional API. Architecturally, you need to define to the model how you'll combine the inputs with the Dense layer ie how you want to create the intermediate layer viz. merge/add or subtract etc/construct a embedding layer etc), or maybe you want to have 2 neural networks, 1 for each input and only want to combine the output in the last layer. The code for each of the above will vary.

Here's a working solution assuming you want to merge the inputs into a vector of shape 672 and then construct a neural network on that input:

import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.optimizers import Adam, RMSprop
import numpy as np

input1 = Input(shape=(336,))
input2 = Input(shape=(336,))
input = Concatenate()([input1, input2])
x = Dense(2)(input)
x = Dense(1)(x)
model = Model(inputs=[input1, input2], outputs=x)
model.summary()

You'll notice that this model merges or concatenates the two inputs and then constructs a neural network on top of that:

Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, 336)          0                                            
__________________________________________________________________________________________________
input_2 (InputLayer)            (None, 336)          0                                            
__________________________________________________________________________________________________
concatenate (Concatenate)       (None, 672)          0           input_1[0][0]                    
                                                                 input_2[0][0]                    
__________________________________________________________________________________________________
dense (Dense)                   (None, 2)            1346        concatenate[0][0]                
__________________________________________________________________________________________________
dense_1 (Dense)                 (None, 1)            3           dense[0][0]                      
==================================================================================================
Total params: 1,349
Trainable params: 1,349
Non-trainable params: 0

If you have some other preferred way to create the intermediate layer, you should replace the Concatenate line with that in the code.

You can then compile and fit the model:

model.compile(
    optimizer = RMSprop(lr=0.02,rho=0.9,epsilon=None,decay=0),
    loss = 'mean_squared_error'
)


x1, x2 = np.random.randn(100, 336),np.random.randn(100, 336,)
y = np.random.randn(100, 1)
model.fit([x1, x2], y)
like image 25
Piyush Singh Avatar answered Sep 22 '22 09:09

Piyush Singh