我正在尝试使用以下ANN模型运行多类分类:

classifier = Sequential()
classifier.add(Dense(units = 9, kernel_initializer = 'uniform', activation = 'relu', input_dim = 18))
classifier.add(Dense(units = 9, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add(Dense(units = 9, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add(Dense(units = 6 ,kernel_initializer = 'uniform', activation = 'softmax'))
classifier.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
classifier.fit(X_train, y_train, batch_size = 10, epochs = 100)
y_pred = classifier.predict(X_test)

其中X_列的格式为:
[[31 8 27 ... 2 7 5]
 [31 8 11 ... 1 9 3]
 [6 0 4 ... 1 9 3]
 ...
 [55 55 134 ... 5 5 6]
 [41 9 111 ... 1 3 0]
 [19 9 28 ... 3 0 0]]

你的火车是:
[[0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1. 0.]
 ...
 [0. 0. 0. 0. 0. 1.]
 [0. 0. 0. 0. 0. 1.]
 [0. 0. 0. 0. 0. 1.]]

X_列的形状为(352,18),y_列的形状为(352,6),X_测试的形状为(152,18)。
运行时,会出现以下错误:
Traceback (most recent call last):
  File "H:\p36564\Project ZS\tst1.py", line 110, in <module>
    classifier.fit(X_train, y_train, batch_size = 10, epochs = 100)
  File "H:\p36564\lib\site-packages\keras\engine\training.py", line 950, in fit
    batch_size=batch_size)
  File "H:\p36564\lib\site-packages\keras\engine\training.py", line 787, in _standardize_user_data
    exception_prefix='target')
  File "H:\p36564\lib\site-packages\keras\engine\training_utils.py", line 137, in standardize_input_data
    str(data_shape))
ValueError: Error when checking target: expected dense_3 to have shape (1,) but got array with shape (6,)

这个错误可能的原因是什么?任何帮助都将不胜感激。

最佳答案

y_train按您提供的形状设置时,使用categorical_crossentropy作为损失函数而不是sparse_categorical_crossentropy。你的y_train是一个热编码而不是稀疏编码。在您的情况下,稀疏编码是一个数组,如下所示:

[3, 4, 4, ..., 5, 5, 5]

要亲自尝试,请将y_train转换为稀疏编码,如下所示:
y_train_ = np.argmax(y_train, axis=1)

这将使用sparse_categorical_crossentropy作为损失函数(不需要更改模型架构!)

10-08 02:09