简体   繁体   English

ValueError: 层 max_pooling1d 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到的完整形状:(无、128、1、32)

[英]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.我正在尝试创建一个 CNN model。 The shape of the x_train data is (8040, 128) and that of y_train data is (8040, 1). x_train 数据的形状为 (8040, 128),y_train 数据的形状为 (8040, 1)。 Similarly the shape of the x_test data is (3960, 128) and that of y_test data is (3960, 1).类似地,x_test 数据的形状为 (3960, 128),y_test 数据的形状为 (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.我在 CNN model 的输入层出现错误。 Here is my model:这是我的 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.显示的错误是: ValueError: Input 0 of layer max_pooling1d is in compatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 128, 1, 32)收到的完整形状:(无、128、1、32)

How do I resolve this issue?我该如何解决这个问题? What should be the parameters of the cnn layers? cnn层的参数应该是什么?

Change your input_shape as将您的input_shape更改为

input_shape =(128,1)

or use batch_input_shape instead of input_shape and set to或使用batch_input_shape代替input_shape并设置为

batch_input_shape=(128, 1, 1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:层“max_pooling1d”的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到完整形状:(无,51644、29、32) - 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:global_average_pooling2d 层的输入 0 与该层不兼容:预期 ndim=4,发现 ndim=2。 收到的完整形状:[无,128] - ValueError: Input 0 of layer global_average_pooling2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 128] 层 max_pooling2d 的输入 0 与层不兼容:预期 ndim=4,发现 ndim=5。 收到完整形状:[无、4、10、8、32] - 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:层“max_pooling2d”的输入 0 与层不兼容:预期 ndim=4,发现 ndim=5。 收到的完整形状:(无、3、51、39、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:层 sequential_16 的输入 0 与层不兼容:预期 ndim=5,发现 ndim=4。 收到的完整形状:[无,224、224、3] - 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: 层双向的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到的完整形状:(无、120、1024、1024) - 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: 层 lstm_17 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:[无,128] - ValueError: Input 0 of layer lstm_17 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 128] ValueError:conv3d_8 层的输入 0 与层不兼容::预期 min_ndim=5,发现 ndim=4。 收到的完整形状:[None, 4, 150, 150] - 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:层顺序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[None, 32, 32] - 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: 层 lstm 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:[无,18] - 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