将GridSearchCV与具有多个输出的神经网络架构配合使用

时间:2019-05-23 12:18:28

标签: scikit-learn neural-network grid-search

我正在尝试使用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

0 个答案:

没有答案