0

我正在测试使用 hyperopt 库调整 SVM 的参数。通常,当我执行此代码时,进度条会停止并且代码会卡住。我不明白为什么。

这是我的代码:

from hyperopt import fmin, tpe, hp, STATUS_OK, Trials

X_train = normalize(X_train)

def hyperopt_train_test(params):

    if 'decision_function_shape' in params:
        if params['decision_function_shape'] == "ovo":
            params['break_ties'] = False


    clf = svm.SVC(**params)
    y_pred = clf.fit(X_train, y_train).predict(X_test)
    return precision_recall_fscore_support(y_test, y_pred, average='macro')[0]

space4svm = {
    'C': hp.uniform('C', 0, 20),
    'kernel': hp.choice('kernel', ['linear', 'sigmoid', 'poly', 'rbf']),
    'degree': hp.uniform('degree', 10, 30),
    'gamma': hp.uniform('gamma', 10, 30),
    'coef0': hp.uniform('coef0', 15, 30),
    'shrinking': hp.choice('shrinking', [True, False]),
    'probability': hp.choice('probability', [True, False]),
    'tol': hp.uniform('tol', 0, 3),
    'decision_function_shape': hp.choice('decision_function_shape', ['ovo', 'ovr']),
    'break_ties': hp.choice('break_ties', [True, False])
    }

def f(params):
    print(params)
    precision = hyperopt_train_test(params)
    return {'loss': -precision, 'status': STATUS_OK}

trials = Trials()
best = fmin(f, space4svm, algo=tpe.suggest, max_evals=35, trials=trials)
print('best:')
print(best)
4

1 回答 1

1

我建议限制参数的空间,看看是否有效。将参数固定probability为 False 并查看模型是否训练。此外,伽玛需要{‘scale’, ‘auto’}根据文档。

同样在每次迭代时打印出您的信息params,以便更好地了解导致模型卡住的组合。

于 2020-02-06T16:16:53.803 回答