1
votes

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?

1
I never used this, but according to the error, I would say the problem is that you have the 3 arrays inside of a list [] being itself inside a tuple (), try to remove either the () or the [] like inputs = (X_train.reshape(10000,728), input_1.reshape(10000,), input_2.reshape(10000,))Ben.T
This fixes this certain issue by a new issues arises AttributeError: 'tuple' object has no attribute 'ndim'kdf
ndim is an attribute of numpy arrays so probably the parameter inputs should be an array and not a tuple nor a list. Once again, I never used it, but you could try inputs = np.array([X_train.reshape(10000,728), input_1.reshape(10000,), input_2.reshape(10000,)]) at least it would be an array of shape (3,)Ben.T
I am sorry to bother again but there is another error that arises ValueError: could not broadcast input array from shape (10000,728) into shape (10000) when attempting to transform the list into numpy arrays.kdf
I would be better to edit the question (or create a new one) with this problem and the full error traceback, and also your import statement to help fix your problem :) it is hard to fix within commentsBen.T

1 Answers

0
votes

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.