我正在嘗試使用以下 ANN 模型運(yùn)行多類分類: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_train 的格式是:[[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]]y_train 是:[[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_train 的形狀為 (352, 18),y_train 的形狀為 (352, 6),X_test 的形狀為 (152, 18)。當(dāng)它運(yùn)行時(shí),它給出了以下錯(cuò)誤: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,)此錯(cuò)誤的可能原因是什么?任何幫助,將不勝感激。
ValueError:檢查目標(biāo)時(shí)出錯(cuò):預(yù)期dense_3具有形狀(1,)但得到形狀為(6,)的數(shù)組
ibeautiful
2021-09-11 19:31:27