ValueError:检查目标时出错:期望dense_8有4个维度,但得到的数组有形状(37800,10,10)

问题描述 投票:0回答:1

我是机器学习的初学者。我正在研究从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])
machine-learning deep-learning mnist
1个回答
0
投票

您犯了一些错误导致您的网络失败。

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

_________________________________________________________________
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激活(互斥类),它将是每个类总和为1的概率向量

你可以尝试与压扁一起运行,看看这是不是你的问题。然后我强烈建议你研究处理Keras中的图像,因为在这里使用Dense图层(仅限于Dense)并不是最佳的(例如,你可以看到this Kaggle tuto

© www.soinside.com 2019 - 2024. All rights reserved.