'How to pass multiple input through a Keras model with different shapes

I am trying to create a Keras model with multiple inputs.

input_img = Input(shape=(728,))
input_1 = Input(shape=(1,))
input_2 = Input(shape=(1,))

x = (Dense(48,kernel_initializer='normal',activation="relu"))(input_img)
x = (Dropout(0.2))(x)
x = (Dense(24,activation="tanh"))(x)
x = (Dropout(0.3))(x)
x = (Dense(1))(x)
x = keras.layers.concatenate([x, input_1, input_2])
x = (Activation("sigmoid"))(x)
cnn = Model(inputs = ([input_img, input_1, input_2]), outputs = x)
cnn.compile(loss="binary_crossentropy", optimizer='adam')

I defined the inputs as

inputs = ([X_train.reshape(10000,728), input_1.reshape(10000,), input_2.reshape(10000,)])

and trained as follow

history = cnn.fit(inputs, labels, validation_split = 0.2, epochs=30, batch_size=100, validation_data=(validation, labels_test))

Whenever I run this, I get the following error

ValueError: Error when checking target: expected activation_12 to have shape (3,) but got array with shape (1,)

How do I pass the inputs as shape (3,) if they have different dimensions?



Solution 1:[1]

I reproduced your code for simulation and seems to be running correctly.

Here is the code for simulation:

from tensorflow.keras.models import Model
from tensorflow import keras
from tensorflow.keras.layers import Dense, Dropout, Activation, Input, Concatenate
import tensorflow as tf

# inputs = ([X_train.reshape(10000,728), input_1.reshape(10000,), input_2.reshape(10000,)])
inputs = ([tf.ones((10000,728)), tf.ones((10000,)), tf.ones((10000,))]) # Input Simulation
labels = tf.ones((10000,3))  # Output Simulation


input_img = Input(shape=(728,))
input_1 = Input(shape=(1,))
input_2 = Input(shape=(1,))

x = (Dense(48,kernel_initializer='normal',activation="relu"))(input_img)
x = (Dropout(0.2))(x)
x = (Dense(24,activation="tanh"))(x)
x = (Dropout(0.3))(x)
x = (Dense(1))(x)
x = keras.layers.concatenate([x, input_1, input_2])
x = (Activation("sigmoid"))(x)
cnn = Model(inputs = ([input_img, input_1, input_2]), outputs = x)
cnn.compile(loss="binary_crossentropy", optimizer='adam')

cnn.summary()

history = cnn.fit(inputs, labels,  epochs=5, batch_size=100)

I would advise you to check properly your Training Data and Label Data shapes to ensure there are no conflicting shapes that may raise errors. You could also convert your data into Tensors and see if this fixes your problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TF_Support