Search code examples
pythontensorflowmachine-learningkerassequential

Data dimensionality problem with tensorflow sequential model


Being new to Keras sequential models is causing me a few troubles!

I have an x_train of shape : 17755 x 500 x 12

and y_train of shape: 17755 x 15 (labels are already one-hot encoded)

And I made the next model to be trained on this data:

model = Sequential()
model.add(Conv2D(32,3,padding="same", activation="relu", input_shape=(17755,500,12)))
model.add(MaxPool2D())

model.add(Conv2D(32, 3, padding="same", activation="relu"))
model.add(MaxPool2D())

model.add(Conv2D(64, 3, padding="same", activation="relu"))
model.add(MaxPool2D())
model.add(Dropout(0.4))

model.add(Flatten())
model.add(Dense(128,activation="relu"))
model.add(Dense(15, activation="sigmoid"))

model.compile(optimizer ='adam', loss='categorical_crossentropy', metrics = ['Accuracy'])

history = model.fit(x_train, y_train, epochs=5)

1- when I don’t use np.expand_dims to add an axis for batch, I get this error:

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 17755, 500, 12), found shape=(None, 500, 12)

2- when I do use np.expand_dims and the shape of x_train became: 1x17755x500x12 I get this error: Data cardinality is ambiguous: x sizes: 1 y sizes: 17755 Make sure all arrays contain the same number of samples.

3- when I use np.expand_dims for y_train too and its shape became: 1x17755x15 I get this error:

ValueError: Shapes (None, 17755, 15) and (None, 15) are incompatible

I know I’m doing something fundamentally wrong, but what what is that? Can anyone please help me out with the shape of data please?


Solution

  • Regarding x_train try adding a new dimension at the end to represent the channel dimension needed for Conv2D layers. Note also that you do not provide the number of samples to your input shape. Here is a working example:

    import tensorflow as tf
    import numpy as np
    
    x_train = np.random.random((17755,500,12))
    x_train = np.expand_dims(x_train, axis=-1)
    y_train = np.random.random((17755,15))
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Conv2D(32,3,padding="same", activation="relu", input_shape=(500, 12, 1)))
    model.add(tf.keras.layers.MaxPool2D())
    
    model.add(tf.keras.layers.Conv2D(32, 3, padding="same", activation="relu"))
    model.add(tf.keras.layers.MaxPool2D())
    
    model.add(tf.keras.layers.Conv2D(64, 3, padding="same", activation="relu"))
    model.add(tf.keras.layers.MaxPool2D())
    model.add(tf.keras.layers.Dropout(0.4))
    
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(128,activation="relu"))
    model.add(tf.keras.layers.Dense(15, activation="sigmoid"))
    
    model.compile(optimizer ='adam', loss='categorical_crossentropy', metrics = ['Accuracy'])
    
    history = model.fit(x_train, y_train, epochs=5)