Home > Software design >  ValueError: Input 0 of layer sequential_4 is incompatible with the layer: : expected min_ndim=3, fou
ValueError: Input 0 of layer sequential_4 is incompatible with the layer: : expected min_ndim=3, fou

Time:09-07

I am working on a project where I need to segregate the sleep data and its labels. But I am stuck with the error mentioned above.

As I am new to the machine learning side I would be highly grateful if someone can help me out with how can I resolve this issue.

I have implemented a model using the following code:

EEG_training_data = EEG_training_data.reshape(EEG_training_data.shape[0],   EEG_training_data.shape[1],1)
print(EEG_training_data.shape)# (5360, 5000, 1)
EEG_validation_data = EEG_validation_data.reshape(EEG_validation_data.shape[0], EEG_validation_data.shape[1],1)
print(EEG_validation_data.shape)#(1396, 5000, 1)

label_class = (np.unique(EEG_training_label))
num_classes = label_class.size # num_classes = 5

#define the model using CNN
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv1D(filters=64, kernel_size= 16, activation='relu', batch_input_shape=(None,5000, 1)))  # #input_shape=(5000, 1)
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.MaxPool1D(8, padding='same'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))

#Summary of the model defined:
model.summary()

#Define loss function
model.compile(
                      loss=  'categorical_crossentropy', # 'sparse_categorical_crossentropy',
                      optimizer='adam',
                      metrics=[tf.keras.metrics.FalseNegatives(), tf.keras.metrics.FalsePositives(), 'accuracy'])

#one Hot Encoding
y_train_hot = tf.keras.utils.to_categorical(EEG_training_label, num_classes) 
print('New y_train shape: ', y_train_hot.shape)#(5360, 5)

y_valid_hot = tf.keras.utils.to_categorical(EEG_validation_label, num_classes)
print('New y_valid shape: ', y_valid_hot.shape)#(1396, 5)

# apply fit on data
model_history = model.fit(
                            x=EEG_training_data,
                            y=y_train_hot, 
                            batch_size=32,
                            epochs=5,
                            validation_data=(EEG_validation_data, y_valid_hot),
                             )



model_prediction = model.predict(EEG_testing_data)
predicted_matrix = tf.math.confusion_matrix(labels=EEG_testing_label.argmax(axis=1), predictions=model_prediction.argmax(axis=1)).numpy()
print(predicted_matrix)

CodePudding user response:

I'm not experiencing your issue with the code you have provided. Try executing the following code, that should work as expected. If that is the case, double check that the shapes of all your data, i.e. EEG_training_data etc. are like the ones below:

import tensorflow as tf
import numpy as np

EEG_training_data = np.ones((5360, 5000, 1))
EEG_validation_data = np.ones((1396, 5000, 1))
EEG_training_label = np.random.randint(5, size=5360)
EEG_validation_label = np.random.randint(5, size=1396)

label_class = (np.unique(EEG_training_label))
num_classes = label_class.size
print(num_classes)  # prints 5

#define the model using CNN
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv1D(filters=64, kernel_size= 16, activation='relu', batch_input_shape=(None, 5000, 1)))  # input_shape=(5000, 1)
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.MaxPool1D(8, padding='same'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))

#Summary of the model defined:
model.summary()

#Define loss function
model.compile(loss = 'categorical_crossentropy',
              optimizer='adam',
              metrics=[tf.keras.metrics.FalseNegatives(), tf.keras.metrics.FalsePositives(), 'accuracy'])

#one Hot Encoding
y_train_hot = tf.keras.utils.to_categorical(EEG_training_label, num_classes) 

print('New y_train shape: ', y_train_hot.shape)  #(5360, 5)

y_valid_hot = tf.keras.utils.to_categorical(EEG_validation_label, num_classes)
print('New y_valid shape: ', y_valid_hot.shape)  #(1396, 5)

# apply fit on data
model_history = model.fit(x=EEG_training_data,
                          y=y_train_hot, 
                          batch_size=32,
                          epochs=5,
                          validation_data=(EEG_validation_data, y_valid_hot),
)

model_prediction = model.predict(EEG_validation_data)
predicted_matrix = tf.math.confusion_matrix(labels=EEG_validation_label.argmax(axis=1), predictions=model_prediction.argmax(axis=1)).numpy()
print(predicted_matrix)
  • Related