提问人:omar Radi 提问时间:11/8/2023 最后编辑:omar Radi 更新时间:11/8/2023 访问量:36
ValueError:图层“sequential_20”的输入 0 与图层不兼容:预期 shape=(None, 784),找到 shape=(50, 28, 28)
ValueError: Input 0 of layer "sequential_20" is incompatible with the layer: expected shape=(None, 784), found shape=(50, 28, 28)
问:
这是我第一次用 Python 编写程序,我正在遵循教授给我的一篇论文,但我在运行 model.fit() 时不断出现错误
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import keras
from keras.datasets import mnist
import seaborn as sns
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
m = X_train.shape[0]
X_train = X_train/255
X_train = X_train.reshape(X_train.shape[0],-1).T
X_test = X_test/255
X_test = X_test.reshape(X_test.shape[0],-1).T
X_train.shape,X_test.shape
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
for i in range(9):
plt.subplot(330 + 1 + i)
plt.imshow(X_train[i], cmap=plt.get_cmap('gray'))
plt.show()
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
from sklearn.metrics import confusion_matrix
from keras.optimizers import Adam
model = Sequential([
Dense(units=10, input_shape=(784,), activation='relu'),
Dense(units=10, activation='softmax')
])
model.compile(loss='categorical_crossentropy',optimizer=Adam(learning_rate=0.01), metrics=['accuracy'])
model.summary()
batch_size=50
epoch=5
model.fit(X_train,Y_train,batch_size,epoch)
答:
0赞
Chih-Hao Liu
11/8/2023
#1
这里的主要问题是您的输入大小是 ,而您已将输入形状设置为 。要解决此问题,您应该添加一个图层来重塑输入数据。修改后的代码如下:(28, 28)
input_shape=(784,)
Flatten()
model = Sequential([
Flatten(),
Dense(units=10, input_shape=(784,), activation='relu'),
Dense(units=10, activation='softmax')
])
另一点是关于损失函数。由于 MNIST 数据集中的标签只有一个元素,因此应替换为 。categorical_crossentropy
sparse_categorical_crossentropy
顺便说一句,您当前的模型架构在准确性方面可能表现不佳。下面是训练结果的示例:
Epoch 1/5
1200/1200 [==============================] - 4s 3ms/step - loss: 2.5401 - accuracy: 0.1108
Epoch 2/5
1200/1200 [==============================] - 3s 2ms/step - loss: 2.3023 - accuracy: 0.1088
Epoch 3/5
1200/1200 [==============================] - 3s 2ms/step - loss: 2.3024 - accuracy: 0.1098
Epoch 4/5
1200/1200 [==============================] - 3s 2ms/step - loss: 2.3023 - accuracy: 0.1110
Epoch 5/5
1200/1200 [==============================] - 2s 2ms/step - loss: 2.3022 - accuracy: 0.1087
但是,这不是编程问题。我建议在模型中添加和图层以提高性能。检查这个: https://machinelearningmastery.com/how-to-develop-a-convolutional-neural-network-from-scratch-for-mnist-handwritten-digit-classification/Conv2D
MaxPooling2D
评论