Machine learning 多层感知器:收敛警告:随机优化器:达到最大迭代次数且优化尚未';还没有会合。警告?

Machine learning 多层感知器:收敛警告:随机优化器:达到最大迭代次数且优化尚未';还没有会合。警告?,machine-learning,scikit-learn,neural-network,Machine Learning,Scikit Learn,Neural Network,我写了一个基本的程序来理解MLP分类器中发生了什么 from sklearn.neural_network import MLPClassifier 数据:标记为男性或女性的身体指标(身高、宽度和鞋码)数据集: X = [[181, 80, 44], [177, 70, 43], [160, 60, 38], [154, 54, 37], [166, 65, 40], [190, 90, 47], [175, 64, 39], [177, 70, 40], [159, 55

我写了一个基本的程序来理解MLP分类器中发生了什么

from sklearn.neural_network import MLPClassifier
数据:标记为男性或女性的身体指标(身高、宽度和鞋码)数据集:

X = [[181, 80, 44], [177, 70, 43], [160, 60, 38], [154, 54, 37], [166, 65, 40],
     [190, 90, 47], [175, 64, 39],
     [177, 70, 40], [159, 55, 37], [171, 75, 42], [181, 85, 43]]
y = ['male', 'male', 'female', 'female', 'male', 'male', 'female', 'female',
     'female', 'male', 'male']
准备模型:

 clf= MLPClassifier(hidden_layer_sizes=(3,), activation='logistic',
                       solver='adam', alpha=0.0001,learning_rate='constant', 
                      learning_rate_init=0.001)
训练

学习分类器的属性:

print('current loss computed with the loss function: ',clf.loss_)
print('coefs: ', clf.coefs_)
print('intercepts: ',clf.intercepts_)
print(' number of iterations the solver: ', clf.n_iter_)
print('num of layers: ', clf.n_layers_)
print('Num of o/p: ', clf.n_outputs_)
试验

计算精度

print( 'accuracy: ',clf.score( [ [179, 69, 40],[175, 72, 45] ], ['female','male'], sample_weight=None ))
运行1 RUN2 RUN3 运行4: ----------------------------------------------------------------- 我有以下问题:

1.Why in the RUN1 the optimizer did not converge?
2.Why in RUN3 the number of iteration were suddenly becomes so low and in the RUN4 so high?
3.What else can be done to increase the accuracy which I get in RUN1.? 

1:您的MLP没有收敛: 该算法通过逐步收敛到最小值进行优化,在运行1中未找到最小值

2运行差异: 您的MLP有一些随机的起始值,因此您不会得到与您在数据中看到的相同的结果。似乎你在第四次跑步时就已经非常接近最低水平了。您可以将MLP的
random_state
参数更改为常量,例如
random_state=0
,以反复获得相同的结果

3是最难的一点。 可以使用优化参数

from sklearn.model_selection import GridSearchCV
Gridsearch将您的测试集分成大小相等的部分,使用一部分作为测试数据,其余部分作为训练数据。因此,它优化了尽可能多的分类器,就像您将数据分割成的部分一样

您需要指定(您的数据很小,所以我建议2或3)您拆分的部分数量、分类器(您的MLP)以及您希望优化的参数网格,如下所示:

param_grid = [
        {
            'activation' : ['identity', 'logistic', 'tanh', 'relu'],
            'solver' : ['lbfgs', 'sgd', 'adam'],
            'hidden_layer_sizes': [
             (1,),(2,),(3,),(4,),(5,),(6,),(7,),(8,),(9,),(10,),(11,), (12,),(13,),(14,),(15,),(16,),(17,),(18,),(19,),(20,),(21,)
             ]
        }
       ]
因为你曾经使用三个神经元的隐藏层获得了100%的准确率,你可以尝试优化参数,比如学习速率和动量,而不是隐藏层

像这样使用Gridsearch:

clf = GridSearchCV(MLPClassifier(), param_grid, cv=3,
                           scoring='accuracy')
clf.fit(X,y)


print("Best parameters set found on development set:")
print(clf.best_params_)
current loss computed with the loss function:  0.691966937074
coefs:  [array([[ 0.21882191, -0.48037975, -0.11774392],
       [-0.15890357,  0.06887471, -0.03684797],
       [-0.28321762,  0.48392007,  0.34104955]]), array([[ 0.08672174],
       [ 0.1071615 ],
       [-0.46085333]])]
intercepts:  [array([-0.36606747,  0.21969636,  0.10138625]), array([-0.05670653])]
 number of iterations the solver:  4
num of layers:  3
Num of o/p:  1
prediction:  ['male' 'male']
accuracy:  0.5
current loss computed with the loss function:  0.697102567593
coefs:  [array([[ 0.32489731, -0.18529689, -0.08712877],
       [-0.35425908,  0.04214241,  0.41249622],
       [-0.19993622, -0.38873908, -0.33057999]]), array([[ 0.43304555],
       [ 0.37959392],
       [ 0.55998979]])]
intercepts:  [array([ 0.11555407, -0.3473817 , -0.16852093]), array([ 0.31326347])]
 number of iterations the solver:  158
num of layers:  3
Num of o/p:  1
prediction:  ['male' 'male']
accuracy:  0.5
1.Why in the RUN1 the optimizer did not converge?
2.Why in RUN3 the number of iteration were suddenly becomes so low and in the RUN4 so high?
3.What else can be done to increase the accuracy which I get in RUN1.? 
from sklearn.model_selection import GridSearchCV
param_grid = [
        {
            'activation' : ['identity', 'logistic', 'tanh', 'relu'],
            'solver' : ['lbfgs', 'sgd', 'adam'],
            'hidden_layer_sizes': [
             (1,),(2,),(3,),(4,),(5,),(6,),(7,),(8,),(9,),(10,),(11,), (12,),(13,),(14,),(15,),(16,),(17,),(18,),(19,),(20,),(21,)
             ]
        }
       ]
clf = GridSearchCV(MLPClassifier(), param_grid, cv=3,
                           scoring='accuracy')
clf.fit(X,y)


print("Best parameters set found on development set:")
print(clf.best_params_)