ValueError: 检查输入时出错:期望dense_10_input具有2维,但得到形状为(60000, 28, 28)的数组

我试图训练我的深度神经网络来识别手写数字,但我一直遇到标题中提到的错误。它显示的错误是:“ValueError: 检查输入时出错:期望dense_7_input具有2维,但得到形状为(60000, 28, 28)的数组”,我不知道为什么。我已经查看了这个问题之前的答案,但都没有用。更新:当我尝试代码的最后一段时,它给我这个错误:ValueError: 输入数组的样本数应与目标数组的样本数相同。发现60000个输入样本和10000个目标样本。

输入图片描述

# Importsimport kerasfrom keras.datasets import mnistfrom keras.models import Sequentialfrom keras.layers import Densefrom keras.utils import to_categorical# Configuration optionsfeature_vector_length = 784num_classes = 60000# Load the data(X_train, Y_train), (X_test, Y_test) = mnist.load_data()# Reshape the data - MLPs do not understand such things as '2D'.# Reshape to 28 x 28 pixels = 784 featuresX_train = X_train.reshape(X_train.shape[0], feature_vector_length)X_test = X_test.reshape(X_test.shape[0], feature_vector_length)# Convert into greyscaleX_train = X_train.astype('float32')X_test = X_test.astype('float32')X_train /= 255X_test /= 255# Convert target classes to categorical onesY_train = to_categorical(Y_train, num_classes)Y_test = to_categorical(Y_test, num_classes)# Importsimport kerasfrom keras.datasets import mnistfrom keras.models import Sequentialfrom keras.layers import Densefrom keras.utils import to_categorical# Configuration optionsfeature_vector_length = 784num_classes = 60000# Load the data(X_train, Y_train), (X_test, Y_test) = mnist.load_data()# Visualize one sampleimport matplotlib.pyplot as pltplt.imshow(X_train[0], cmap='Greys')plt.show()# Set the input shapeinput_shape = (feature_vector_length,)print(f'Feature shape: {input_shape}')# Create the modelmodel = Sequential()model.add(Dense(350, input_shape=input_shape, activation='relu'))model.add(Dense(50, activation='relu'))model.add(Dense(num_classes, activation='softmax'))# Configure the model and start trainingmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics= ['accuracy'])model.fit(X_train, Y_train, epochs=10, batch_size=250, verbose=1, validation_split=0.2)# New one ######## Test the model after trainingtest_results = model.evaluate(X_test, Y_test, verbose=1)print(f'Test results - Loss: {test_results[0]} - Accuracy: {test_results[1]}%')

回答:

我看了你的代码,并让它工作了。我添加了Flatten层,这将把输入转换为可用的格式。我还使用了tensorflow.keras而不是keras,因为我认为那个版本更好,但你可以用两者。我还发现你数据类型有问题,所以我将你的X_train和X_test转换成了float32类型的numpy数组。

# Importsimport tensorflowfrom tensorflow.keras.datasets import mnistfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Densefrom tensorflow.keras.utils import to_categorical# Configuration optionsfeature_vector_length = 784num_classes = 60000# Load the data(X_train, Y_train), (X_test, Y_test) = mnist.load_data()# Reshape the data - MLPs do not understand such things as '2D'.# Reshape to 28 x 28 pixels = 784 featuresX_train = X_train.reshape(X_train.shape[0], feature_vector_length)X_test = X_test.reshape(X_test.shape[0], feature_vector_length)# Convert into greyscaleX_train = X_train.astype('float32')X_test = X_test.astype('float32')X_train /= 255X_test /= 255# Convert target classes to categorical onesY_train = to_categorical(Y_train, num_classes)Y_test = to_categorical(Y_test, num_classes)# Importsimport tensorflowfrom tensorflow.keras.datasets import mnistfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Dense,Flattenfrom tensorflow.keras.utils import to_categoricalimport numpy as np# Configuration optionsfeature_vector_length = 784num_classes = 60000# Load the data(X_train, Y_train), (X_test, Y_test) = mnist.load_data()# Visualize one sampleimport matplotlib.pyplot as pltplt.imshow(X_train[0], cmap='Greys')plt.show()# Set the input shapeinput_shape = (feature_vector_length,)print(f'Feature shape: {input_shape}')X_train=np.array(X_train,dtype="float32")X_test=np.array(X_train,dtype="float32")# Create the modelmodel = Sequential()model.add(Flatten())model.add(Dense(350, input_shape=input_shape, activation='relu'))model.add(Dense(50, activation='relu'))model.add(Dense(num_classes, activation='softmax'))# Configure the model and start trainingmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics= ['accuracy'])model.fit(X_train, Y_train, epochs=10, batch_size=250, verbose=1, validation_split=0.2)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注