Reputation: 1301
I'm trying to build a multi input network using keras functional api. Right now I'm stuck as I get the error
ValueError: Error when checking input: expected input_1 to have shape (5,) but got array with shape (1,)
Given my code (below), I don't see how this is possible. The arrays I pass surely have the shapes (5,) and (15,) as my network specifies.
def buildModel():
# define two sets of inputs
inputA = ls.Input(shape=(5,))
inputB = ls.Input(shape=(15,))
# the first branch operates on the first input
x = ls.Dense(8, activation="relu")(inputA)
x = ls.Dense(4, activation="relu")(x)
x = ks.Model(inputs=inputA, outputs=x)
# the second branch opreates on the second input
y = ls.Dense(64, activation="relu")(inputB)
y = ls.Dense(32, activation="relu")(y)
y = ls.Dense(4, activation="relu")(y)
y = ks.Model(inputs=inputB, outputs=y)
# combine the output of the two branches
combined = ls.concatenate([x.output, y.output])
# apply a FC layer and then a regression prediction on the
# combined outputs
z1 = ls.Dense(2, activation="relu")(combined)
z1 = ls.Dense(5, activation="relu")(z1)
z2 = ls.Dense(2, activation="relu")(combined)
z2 = ls.Dense(15, activation="relu")(z2)
# our model will accept the inputs of the two branches and
# then output a single value
model = ks.Model(inputs=[x.input, y.input], outputs=[z1, z2])
model.compile(optimizer='adam', loss='poisson', metrics=['accuracy'])
return model
def train_model(model, x1, x2, y1, y2):
print(x1.shape)
model.fit([x1, x2], [y1, y2], batch_size=1, epochs=100) # Error raised here
if __name__ == "__main__":
mod = buildModel()
x1 = np.array([5, 2, 1, 4, 5])
x2 = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
y1 = np.array([0, 0, 1, 0, 0])
y2 = np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
train_model(mod, x1, x2, y1, y2)
The arrays passed are for testing purposes only, the AI will eventually tackle a real problem.
If you think the question isn't well defined or needs more specification, please let me know.
EDIT: Here is the traceback:
Traceback (most recent call last):
File "/Users/henrihlo/project/ai.py", line 70, in <module>
train_model(mod, x1, x2, y1, y2)
File "/Users/henrihlo/project/ai.py", line 49, in train_model
model.fit([x1, x2], [y1, y2], batch_size=1, epochs=100)
File "/Users/henrihlo/anaconda3/lib/python3.7/site packages/keras/engine/training.py", line 1154, in fit
batch_size=batch_size)
File "/Users/henrihlo/anaconda3/lib/python3.7/site packages/keras/engine/training.py", line 579, in _standardize_user_data
exception_prefix='input')
File "/Users/henrihlo/anaconda3/lib/python3.7/site packages/keras/engine/training_utils.py", line 145, in
standardize_input_data
str(data_shape))
ValueError: Error when checking input: expected input_1 to have shape (5,) but got array with shape (1,)
Printing the shape of x1 yields (5,).
Upvotes: 1
Views: 105
Reputation: 22031
you need to feed keras model with 2D array (n_sample, n_feat)
. A simple reshape in your case does the trick
mod = buildModel()
x1 = np.array([5, 2, 1, 4, 5]).reshape(1,-1)
x2 = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]).reshape(1,-1)
y1 = np.array([0, 0, 1, 0, 0]).reshape(1,-1)
y2 = np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).reshape(1,-1)
train_model(mod, x1, x2, y1, y2)
Upvotes: 1