模型拟合错误

我在尝试编写数字识别程序。我有一个数据集,包含60000张图像的像素数据,每张图像的尺寸为28×28像素。

import numpy as npimport matplotlib.pyplot as pltimport pandas as pdfrom keras.datasets import mnist(x_train, y_train), (x_test, y_test) = mnist.load_data()x_train= x_train.reshape(60000, 28, 28, 1).astype('float32')x_test= x_test.reshape(10000, 28, 28, 1).astype('float32')from keras.models import Sequentialfrom keras.layers import Convolution2Dfrom keras.layers import MaxPooling2Dfrom keras.layers import Flattenfrom keras.layers import Denseclassifier= Sequential()classifier.add(Convolution2D(32, 3, 3, input_shape= (28, 28, 1), activation= 'relu'))classifier.add(MaxPooling2D(pool_size= (2, 2)))classifier.add(Flatten())classifier.add(Dense(output_dim = 128, activation= 'relu'))classifier.add(Dense(output_dim = 10, activation= 'softmax'))classifier.compile(optimizer= 'adam', loss='binary_crossentropy', metrics = ['accuracy'])classifier.fit(x_train, y_train, validation_data= (x_test, y_test), nb_epoch= 15, verbose= 2, batch_size= 100)

我遇到了以下错误。

classifier.fit(x_train, y_train, validation_data= (x_test, y_test), nb_epoch= 15, verbose= 2, batch_size= 100)Traceback (most recent call last):  File "<ipython-input-4-9425b6d029dc>", line 1, in <module>    classifier.fit(x_train, y_train, validation_data= (x_test, y_test), nb_epoch= 15, verbose= 2, batch_size= 100)  File "C:\Users\SHUBHAM\Anaconda3\lib\site-packages\keras\models.py", line 672, in fit    initial_epoch=initial_epoch)  File "C:\Users\SHUBHAM\Anaconda3\lib\site-packages\keras\engine\training.py", line 1117, in fit    batch_size=batch_size)  File "C:\Users\SHUBHAM\Anaconda3\lib\site-packages\keras\engine\training.py", line 1034, in _standardize_user_data    exception_prefix='model target')  File "C:\Users\SHUBHAM\Anaconda3\lib\site-packages\keras\engine\training.py", line 124, in standardize_input_data    str(array.shape))ValueError: Error when checking model target: expected dense_2 to have shape (None, 10) but got array with shape (60000, 1)

我不明白问题出在哪里。请帮助我。


回答:

看起来是与输出形状相关的错误。从我的神经网络代码 classifier.add(Dense(output_dim = 10, activation= 'softmax'))来看,输出应该具有形状 [recordCount, 10]。但当我在Python控制台中运行并输入以下命令时,我看到 y_train 的形状不对

>>> from keras.datasets import mnistUsing Theano backend.Using gpu device 0: GeForce GT 730 (CNMeM is enabled with initial size: 70.0% of memory, cuDNN not available)>>> (x_train, y_train), (x_test, y_test) = mnist.load_data()Downloading data from https://s3.amazonaws.com/img-datasets/mnist.pkl.gz15253504/15296311 [============================>.] - ETA: 0s>>>>>> x_train.shape(60000, 28, 28)>>> y_train.shape(60000,)

y_train 的值在0到9的范围内。因此,我建议你进行以下转换:

>>> import numpy>>> y_train_new = numpy.zeros([60000, 10])>>> for i in range(0, 10):...     y_train_new[:, i] = (y_train == i).astype(numpy.int32)

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中创建了一个多类分类项目。该项目可以对…

发表回复

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