简体   繁体   中英

ValueError: Input 0 of layer sequential_32 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 256]

I am building a Conv1D model with train data (28659, 257) and test data (5053, 257), but I am facing a value error that says: expected min_ndim=3, found ndim=2. Full shape received: [None, 256]

size of datasets

print(train_data.shape)
print(test_data.shape)

model

model = Sequential()
model.add(Conv1D(filters=64, kernel_size=5, activation='relu', input_shape=(256,1)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(8, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])
model.summary()

opt = keras.optimizers.Adam(learning_rate=0.01)
model.compile(loss='CategoricalCrossentropy', optimizer=opt, metrics=['accuracy'])

history = model.fit(train_data.values[:, 0:256], to_categorical(train_data.values[:, 256]), epochs=180, batch_size=500)
y_pred = model.predict(test_data.values[:, 0:256])

testing accuracy

y_pred = model.predict(test_data.values[:,0:256])
y_pred = (y_pred > 0.5)
accuracy = metrics.accuracy_score(to_categorical(test_data.values[:,256]),y_pred)
print(f'Testing accuracy of the model is {accuracy*100:.4f}%')

The error is from the fit(), but I cannot figure my mistake with the calculation! Any help is appreciated!

try to reshape your train and test data:

X_train=np.reshape(train_data,(train_data.shape[0], train_data.shape[1],1))
X_test=np.reshape(test_data,(test_data.shape[0], test_data.shape[1],1))

You can't feed values like: [1,2,3,4] , you have to feed your values like [[1],[2],[3],[4]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (None, 1) ValueError: Input 0 of layer sequential_5 is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 953] ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 1] ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 2584] ValueError: Input 0 of layer sequential_4 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 5000) ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 256, 256] ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 32, 32] ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (10, 24) ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (32768, 1) Keras Tuner Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 216)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM