fiting a model with multiple inputs

Issue

I have a similar problem to this in my code, I wrote a simplified version of it :

my model has two inputs, but no matter how I send the data to fit, it won’t work.
here is a short example of the problem :

input1 = Input(shape=(1,), dtype='int64')
input2 = Input(shape=(1,), dtype='int64')

embeding1 = Embedding(1, 5, input_length=1, embeddings_regularizer=l2(1e-4) )(input1)
embeding2 = Embedding(1, 5, input_length=1, embeddings_regularizer=l2(1e-4) )(input2)



x = concatenate([embeding1, embeding2])
x = Flatten()(x)
x = Dense(1, activation='relu')(x)

model = Model([input1, input2],x)
model.compile(loss='mse', metrics=['accuracy'], optimizer='adam')

but I can’t figure out how to run fit, I tried :

history = model.fit(
     [[1,1], [1,1], [1,1]],
     [1,1,1]
     )

thinking that maybe each item in the sample need to have a list of both its properties,
but then I received the error

ValueError: Layer model_3 expects 2 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 2) dtype=int64>]

I have also tried

history = model.fit(
     [[1,1,1], [1,1,1]],
     [1,1,1]
     )

thinking maybe I need two lists for each feature.
the error :

ValueError: Data cardinality is ambiguous:
  x sizes: 2
  y sizes: 3
Make sure all arrays contain the same number of samples.

i tried also variant of switching [] with (), using np.array, np.stack
but nothing I tried worked…

Solution

Each of inputs and the output should have shape of (batch_size, 1). So this works (batch size of 32):

input_1 = np.zeros((32, 1))
input_2 = np.zeros((32, 1))
outs = np.ones((32, 1))
history = model.fit([input_1, input_2], outs)

Answered By – amin

Answer Checked By – Candace Johnson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.