ValueError: Data cardinality is ambiguous. Make sure all arrays contain the same number of samples

15,851

Solution 1

Convert data_list and y to numpy arrays or tensors.

In your code the list is treated as four inputs while your model has one input - https://keras.io/api/models/model_training_apis/

Add these lines:

import tensorflow as tf

data_list = tf.stack(data_list)
y = tf.stack(y)

Solution 2

Try this

model.fit(np.array(data_list), np.array(y), verbose=0, epochs=100)
Share:
15,851
Debvrat Varshney
Author by

Debvrat Varshney

Updated on June 30, 2022

Comments

  • Debvrat Varshney
    Debvrat Varshney almost 2 years

    I am running the following code on Colab. This is a regression problem, where I want to generate 5 float values from each image of size 224 x 224. As per my understanding, to solve this problem, I should use fully connected networks with 5 nodes in the last layer. But doing so on keras gave me an error described below.

    import keras, os
    import numpy as np
    from tensorflow.keras.models import Model
    from tensorflow.keras.optimizers import Adam
    from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
    from tensorflow.keras.applications.inception_v3 import InceptionV3
    
    ## data_list = list of four 224x224 numpy arrays
    
    inception = InceptionV3(weights='imagenet', include_top=False)
    x = inception.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(5, activation='relu')(x)
    
    y = [np.random.random(5),np.random.random(5),np.random.random(5),np.random.random(5)]
    
    model = Model(inputs=inception.input, outputs=predictions)
    opt = Adam(lr=0.001)
    model.compile(optimizer=opt, loss="mae")
    model.fit(data_list, y, verbose=0, epochs=100)
    

    Error:

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

    What could be going wrong?