简体   繁体   中英

ValueError: Input 0 of layer max_pooling1d is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 128, 1, 32)

I am trying to create a CNN model. The shape of the x_train data is (8040, 128) and that of y_train data is (8040, 1). Similarly the shape of the x_test data is (3960, 128) and that of y_test data is (3960, 1).

Loading your data:

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(data,out_data,test_size=0.33, random_state=21)
print(x_train.shape,y_train.shape)
print(x_test.shape,y_test.shape)
#(8040, 128) (8040, 1)
#(3960, 128) (3960, 1)
from sklearn.preprocessing import OneHotEncoder
OHE = OneHotEncoder()

y_train1 = OHE.fit_transform(y_train).toarray()
y_test1 = OHE.fit_transform(y_test).toarray()

I'm getting an error in the input layer of the CNN model. Here is my model:

# Conv1:
model.add(keras.layers.Conv1D(filters = 32, kernel_size=1,strides = 1, activation='relu', input_shape=(128,1,1)))
# Conv2:
model.add(keras.layers.Conv1D(filters = 32, kernel_size=1,strides = 1, activation='relu'))
# Pool1:
model.add(keras.layers.MaxPool1D(pool_size=(3), strides = 1)) 
# BN1:
model.add(keras.layers.BatchNormalization())
# Conv3:
model.add(keras.layers.Conv1D(filters = 64, kernel_size=1,strides = 1, activation='relu'))
# Conv4:
model.add(keras.layers.Conv1D(filters = 64, kernel_size=1,strides = 1, activation='relu'))
# Pool2:
model.add(keras.layers.MaxPool1D(pool_size=(3), strides = 1))
# BN2:
model.add(keras.layers.BatchNormalization())

model.add(keras.layers.Conv1D(filters = 128, kernel_size=1,strides = 1, activation='relu'))
# Conv6:
model.add(keras.layers.Conv1D(filters = 128, kernel_size=1,strides = 1, activation='relu'))
# Pool3:
model.add(keras.layers.GlobalMaxPooling1D())
# BN4:
model.add(keras.layers.BatchNormalization())

import tensorflow as tf
model.add(tf.keras.layers.Flatten())
# Dense1:
model.add(keras.layers.Dense(units=256, activation='relu',use_bias=True))
# Dense2:
model.add(keras.layers.Dense(units=128, activation='relu',use_bias=True))
# BN3:
model.add(keras.layers.BatchNormalization())
# Dense3:
model.add(keras.layers.Dense(units=16, activation='softmax',use_bias=True))
from keras.utils import np_utils
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()

scaler.fit(x_train)
model_history = model.fit(scaler.transform(x_train),y_train1, batch_size=32, epochs=43, callbacks=[callback], verbose=1)

The error showed is: ValueError: Input 0 of layer max_pooling1d is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 128, 1, 32)

How do I resolve this issue? What should be the parameters of the cnn layers?

Change your input_shape as

input_shape =(128,1)

or use batch_input_shape instead of input_shape and set to

batch_input_shape=(128, 1, 1)

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 ValueError: Input 0 of layer "max_pooling1d" is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 51644, 29, 32) ValueError: Input 0 of layer global_average_pooling2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 128] Input 0 of layer max_pooling2d is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [None, 4, 10, 8, 32] ValueError: Input 0 of layer "max_pooling2d" is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: (None, 3, 51, 39, 32) ValueError: Input 0 of layer sequential_16 is incompatible with the layer: expected ndim=5, found ndim=4. Full shape received: [None, 224, 224, 3] ValueError: Input 0 of layer bidirectional is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 120, 1024, 1024) ValueError: Input 0 of layer lstm_17 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 128] ValueError: Input 0 of layer conv3d_8 is incompatible with the layer: : expected min_ndim=5, found ndim=4. Full shape received: [None, 4, 150, 150] 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 lstm is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 18]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM