ValueError:检查目标时出错:预期density_8具有4个维度,但数组的形状为(37800、10、10)

时间:2019-04-01 03:52:36

标签: machine-learning deep-learning mnist

我是机器学习的初学者。我正在研究从kaggle下载的mnist数据集。我正在借助教程进行第一个项目。但是我正面临这个无法解决的问题。请帮忙。以下是。

import keras 
import keras.preprocessing
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
import pandas as pd
from keras.layers import Dense
from keras.optimizers import SGD
from sklearn.model_selection import ShuffleSplit
from sklearn.metrics import accuracy_score, confusion_matrix

X = pd.read_csv(r'C:\Users\faizan\Desktop\ML\Kaggle\MNIST\train.csv')

Y = pd.read_csv(r'C:\Users\faizan\Desktop\ML\Kaggle\MNIST\test.csv')

y = X["label"]

X = X.drop(["label"], 1)
#x = Y.drop(['label'], 1)

print(y.shape)
print(X.shape)
print(Y.shape)

y = keras.utils.to_categorical(y, num_classes = 10)
X = X / 255.0
X = X.values.reshape(-1,28,28,1)
# Shuffle Split Train and Test from original dataset
seed=2
train_index, valid_index = ShuffleSplit(n_splits=1,
                                        train_size=0.9,
                                        test_size=None,
                                        random_state=seed).split(X).__next__()
x_train = X[train_index]
Y_train = y[train_index]
x_test = X[valid_index]
Y_test = y[valid_index]

model = Sequential()
model.add(Dense(units=128,activation="relu", input_shape=(28, 28, 1)))
model.add(Dense(units=128,activation="relu"))
model.add(Dense(units=128,activation="relu"))
model.add(Dense(units=10,activation="softmax"))

## Compiling Model
model.compile(optimizer=SGD(0.001),loss="categorical_crossentropy",metrics=["accuracy"])


## Training

model.fit(x_train,Y_train,batch_size=32, epochs=10,verbose=1)
accuracy = model.evaluate(x=x_test, y=Y_test, batch_size=32)

## Checking Accuracy
print("Accuracy: ", accuracy[1])

1 个答案:

答案 0 :(得分:0)

您正在犯一些错误,导致您的网络失败。

首先,我假设您正在使用NMIST数据集,并且您正在尝试将每个图像分类为一个类。您的网络如下:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 28, 28, 128)       256       
_________________________________________________________________
dense_2 (Dense)              (None, 28, 28, 128)       16512     
_________________________________________________________________
dense_3 (Dense)              (None, 28, 28, 128)       16512     
_________________________________________________________________
dense_4 (Dense)              (None, 28, 28, 10)        1290      
=================================================================
Total params: 34,570
Trainable params: 34,570
Non-trainable params: 0
_________________________________________________________________

所以:您在网络的输出中有四个维度。这对于分类任务是不合适的。如果您在最后一层之前添加展平层:

    _________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_5 (Dense)              (None, 28, 28, 128)       256       
_________________________________________________________________
dense_6 (Dense)              (None, 28, 28, 128)       16512     
_________________________________________________________________
dense_7 (Dense)              (None, 28, 28, 128)       16512     
_________________________________________________________________
flatten_1 (Flatten)          (None, 100352)            0         
_________________________________________________________________
dense_8 (Dense)              (None, 10)                1003530   
=================================================================
Total params: 1,036,810
Trainable params: 1,036,810
Non-trainable params: 0
_________________________________________________________________

在这里您会看到我们有您想要的十个课程。而且您只有两个维度:一个维度用于批处理大小(无),另一维度用于类(10)。对于一个样本,由于softmax激活(互斥类),每个类的概率向量总和为一个

能否请您尝试与展平一起运行,看看这是否是您的问题。 然后,我强烈建议您考虑在Keras中处理图像,因为此处使用密集层(仅adn密集)不是最佳选择(例如,您可以看到this Kaggle tuto