Search code examples
numpykerasdeep-learningconv-neural-networkkeras-layer

ValueError: Error when checking input: expected keras_layer_input to have 4 dimensions, but got array with shape (10, 1)


Before this gets marked as duplicate, I already tried all of the the similar questions and most of them were not resolved, if they have an answer, it did not work with my problem. The original code has more than 10 samples.

Input: list of model input np.arrays. sample_train_emb1 has length = 2 enter image description here

Problem: model.fit() error ValueError: Error when checking input: expected keras_layer_input to have 4 dimensions, but got array with shape (10, 1)

Here is my plot_model image: enter image description here

The model.fit() looks like this:

model.fit(
    sample_train_emb1,
    sample_y_train,
    validation_data=(sample_valid_emb1, sample_y_valid),
    epochs=epoch,
    batch_size=batch_size,
    verbose=1,
)

Thank you! Let me know if you need more details to help me solve this problem. It has many similar posts that remained unresolved so I thought it will help anybody who might face the same problem in the future.

What I've tried so far:

  • Swapping the two features.
  • Converting the image feature into a `TensorShape([Dimension(1),
    Dimension(224), Dimension(224), Dimension(3)]) based on a similar question's answer

Solution

  • I eventually figured it out. Using the answer from this post.

    sample_train_emb1[1] = np.array([x for x in sample_train_emb1[1]])

    Hope this helps in the future to anyone.