值:sequential_40的输入0与层不兼容:预期min_ndim = 3,找到ndim = 2。收到完整的形状:(无,58)

发布于 2025-02-10 09:02:24 字数 2102 浏览 0 评论 0原文

我正在研究一门课程中的有关学生表现的数据集,并且我想根据他们上一年的分数来预测学生级别(低,中,高)。我正在为此目的使用CNN,但是当我构建和安装模型时,我会收到此错误:

ValueError: Input 0 of layer sequential_40 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 58)

这是代码:

#reshaping data
X_train = X_train.reshape((X_train.shape[0], X_train.shape[1]))
X_test = X_test.reshape((X_test.shape[0], X_test.shape[1])) 

#checking the shape after reshaping
print(X_train.shape)
print(X_test.shape)

#normalizing the pixel values
X_train=X_train/255
X_test=X_test/255

#defining model
model=Sequential()

#adding convolution layer
model.add(Conv1D(32,3, activation='relu',input_shape=(28,1)))

#adding pooling layer
model.add(MaxPool1D(pool_size=2))

#adding fully connected layer
model.add(Flatten())
model.add(Dense(100,activation='relu'))

#adding output layer
model.add(Dense(10,activation='softmax'))

#compiling the model
model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

model.summary()

#fitting the model
model.fit(X_train,y_train,epochs=10)

这是输出:

Model: "sequential_40"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_23 (Conv1D)           (None, 9, 32)             128       
_________________________________________________________________
max_pooling1d_19 (MaxPooling (None, 4, 32)             0         
_________________________________________________________________
flatten_15 (Flatten)         (None, 128)               0         
_________________________________________________________________
dense_30 (Dense)             (None, 100)               12900     
_________________________________________________________________
dense_31 (Dense)             (None, 10)                1010      
=================================================================
Total params: 14,038
Trainable params: 14,038
Non-trainable params: 0

I am working on a dataset about student performance in a course, and I want to predict student level (low, mid, high) according to their previous year's marks. I'm using a CNN for this purpose, but when I build and fit the model I get this error:

ValueError: Input 0 of layer sequential_40 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 58)

This is the code:

#reshaping data
X_train = X_train.reshape((X_train.shape[0], X_train.shape[1]))
X_test = X_test.reshape((X_test.shape[0], X_test.shape[1])) 

#checking the shape after reshaping
print(X_train.shape)
print(X_test.shape)

#normalizing the pixel values
X_train=X_train/255
X_test=X_test/255

#defining model
model=Sequential()

#adding convolution layer
model.add(Conv1D(32,3, activation='relu',input_shape=(28,1)))

#adding pooling layer
model.add(MaxPool1D(pool_size=2))

#adding fully connected layer
model.add(Flatten())
model.add(Dense(100,activation='relu'))

#adding output layer
model.add(Dense(10,activation='softmax'))

#compiling the model
model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

model.summary()

#fitting the model
model.fit(X_train,y_train,epochs=10)

This is the output:

Model: "sequential_40"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_23 (Conv1D)           (None, 9, 32)             128       
_________________________________________________________________
max_pooling1d_19 (MaxPooling (None, 4, 32)             0         
_________________________________________________________________
flatten_15 (Flatten)         (None, 128)               0         
_________________________________________________________________
dense_30 (Dense)             (None, 100)               12900     
_________________________________________________________________
dense_31 (Dense)             (None, 10)                1010      
=================================================================
Total params: 14,038
Trainable params: 14,038
Non-trainable params: 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水中月 2025-02-17 09:02:24

您应该按照以下方式重塑培训数据:

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

通过执行此操作,您只需在输入中添加一个维度即可。然后您的输入形状就像(无,x_train.shape [1],1)。

you should reshape your training data as below:

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

by doing this you just add a dimension to the inputs. Then your input shape will be like (None, X_train.shape[1], 1).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文