如何用 gridsearchCV 实现 Python 的 MLPClassifier?

数据挖掘 Python 神经网络 scikit-学习 交叉验证 超参数
2021-09-21 01:56:59

我正在尝试使用gridsearchCV函数实现具有10 倍交叉验证的 Python 的MLPClassifier 。这是我的一段代码:

parameters={
'learning_rate': ["constant", "invscaling", "adaptive"],
'hidden_layer_sizes': [(100,1), (100,2), (100,3)],
'alpha': [10.0 ** -np.arange(1, 7)],
'activation': ["logistic", "relu", "Tanh"]
}

clf
= gridSearchCV(estimator=MLPClassifier,param_grid=parameters,n_jobs=-1,verbose=2,cv=10)

虽然,我不确定是否hidden_layer_sizes: [(100,1), (100,2), (100,3)]正确。在这里,我正在尝试调整“隐藏层大小”“神经元数量”我想为hidden_​​layer_sizez: 1, 2, 3和神经元提供这个“元组”参数: 10, 20, 30,...,100

但我不知道这是否是正确的方法。因此,我选择默认神经元为每层100 个。

有人可以建议吗?

2个回答

形式的元组(i1,i2,i3,...,in)给你一个网络n隐藏层,其中ik给你神经元的数量k第隐藏层。

如果你想要三个隐藏层10,3020神经元,你的元组需要看起来像(10,30,20).

(100,1)意味着第二个隐藏层只有一个神经元。

您可以在 scikit-learn 中使用 GridSearchCV 实现 MLPClassifier,如下所示(其他参数也可用):

GRID = [
    {'scaler': [StandardScaler()],
     'estimator': [MLPClassifier(random_state=RANDOM_SEED)],
     'estimator__solver': ['adam'],
     'estimator__learning_rate_init': [0.0001],
     'estimator__max_iter': [300],
     'estimator__hidden_layer_sizes': [(500, 400, 300, 200, 100), (400, 400, 400, 400, 400), (300, 300, 300, 300, 300), (200, 200, 200, 200, 200)],
     'estimator__activation': ['logistic', 'tanh', 'relu'],
     'estimator__alpha': [0.0001, 0.001, 0.005],
     'estimator__early_stopping': [True, False]
     }
]

PIPELINE = Pipeline([('scaler', None), ('estimator', MLPClassifier())])

然后,您可以按以下方式运行 GridSearch:

grid_search = GridSearchCV(estimator=PIPELINE, param_grid=GRID, 
                            scoring=make_scorer(accuracy_score),# average='macro'), 
                            n_jobs=-1, cv=split, refit=True, verbose=1, 
                            return_train_score=False)

grid_search.fit(X, y)