Я пытаюсь настроить параметры нейронной сети с помощью GridSearch, но продолжаю получать следующую ошибку: ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets
Мне кажется, я не знаю, как использовать GridSearchCV в нейронной сети с несколькими выходами.
Я уже пробовал сеть без поиска по сетке, и она работает просто отлично.
Кроме того, я попробовал GridSearch с одной выходной нейронной сетью, и он также работал нормально.
Я искал код ошибки, и в некоторых результатах упоминалось, что y_train должен быть закодирован горячим способом, как в моем случае.
# Encode the y values in one hot
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelEncoder_y = LabelEncoder()
oneHotEncoder = OneHotEncoder(categorical_features=[0])
y[:, 0] = labelEncoder_y.fit_transform(y[:, 0])
y = oneHotEncoder.fit_transform(y).toarray()
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 0)
def build_classifier(optimizer = 'adam'):
classifier = Sequential()
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu', input_dim = 21))
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add(Dense(units = 51, kernel_initializer = 'uniform', activation = 'sigmoid'))
classifier.compile(optimizer = optimizer, loss = 'binary_crossentropy', metrics = ['accuracy'])
return classifier
classifier = KerasClassifier(build_fn = build_classifier, epochs=20, batch_size=128)
parameters = {'batch_size': [128, 256],
'epochs': [100, 500],
'optimizer': ['adam', 'rmsprop']}
grid_search = GridSearchCV(estimator = classifier,
param_grid = parameters,
scoring = 'accuracy',
cv = 10)
grid_search = grid_search.fit(X_train, y_train)
Не удается продолжить работу, когда я пытаюсь подогнать данные обучения, дающие трассировку ошибок снизу.
File "C:\Users\Laur\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\Laur\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "E:/Workspace/master-projects/keystroke-dynamics/py/ann.py", line 52, in <module>
grid_search = grid_search.fit(X_train, y_train)
File "C:\Users\Laur\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 687, in fit
self._run_search(evaluate_candidates)
File "C:\Users\Laur\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 1148, in _run_search
evaluate_candidates(ParameterGrid(self.param_grid))
File "C:\Users\Laur\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 666, in evaluate_candidates
cv.split(X, y, groups)))
File "C:\Users\Laur\Anaconda3\lib\site-packages\joblib\parallel.py", line 921, in __call__
if self.dispatch_one_batch(iterator):
File "C:\Users\Laur\Anaconda3\lib\site-packages\joblib\parallel.py", line 759, in dispatch_one_batch
self._dispatch(tasks)
File "C:\Users\Laur\Anaconda3\lib\site-packages\joblib\parallel.py", line 716, in _dispatch
job = self._backend.apply_async(batch, callback=cb)
File "C:\Users\Laur\Anaconda3\lib\site-packages\joblib\_parallel_backends.py", line 182, in apply_async
result = ImmediateResult(func)
File "C:\Users\Laur\Anaconda3\lib\site-packages\joblib\_parallel_backends.py", line 549, in __init__
self.results = batch()
File "C:\Users\Laur\Anaconda3\lib\site-packages\joblib\parallel.py", line 225, in __call__
for func, args, kwargs in self.items]
File "C:\Users\Laur\Anaconda3\lib\site-packages\joblib\parallel.py", line 225, in <listcomp>
for func, args, kwargs in self.items]
File "C:\Users\Laur\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 554, in _fit_and_score
test_scores = _score(estimator, X_test, y_test, scorer, is_multimetric)
File "C:\Users\Laur\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 597, in _score
return _multimetric_score(estimator, X_test, y_test, scorer)
File "C:\Users\Laur\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 627, in _multimetric_score
score = scorer(estimator, X_test, y_test)
File "C:\Users\Laur\Anaconda3\lib\site-packages\sklearn\metrics\scorer.py", line 97, in __call__
**self._kwargs)
File "C:\Users\Laur\Anaconda3\lib\site-packages\sklearn\metrics\classification.py", line 176, in accuracy_score
y_type, y_true, y_pred = _check_targets(y_true, y_pred)
File "C:\Users\Laur\Anaconda3\lib\site-packages\sklearn\metrics\classification.py", line 81, in _check_targets
"and {1} targets".format(type_true, type_pred))
ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets