如何绘制学习率与准确性sklearn?

时间:2019-01-03 15:01:00

标签: python scikit-learn

我正在使用sklearn.neural_network MLPClassifier,想要准确绘制学习率(0.0001:10)。我得到的数据代码如下。

from sklearn.neural_network import MLPClassifier  
import numpy as np

X_train = Input_Data['Train_Input']
X_test = Input_Data['Test_Input']
Y_train = Input_Data['Train_Target']
Y_test = Input_Data['Test_Target']
Y_train = Y_train.astype('int')
Y_test = Y_test.astype('int')

classifier = svm.SVC(kernel='linear', C=0.01)
Y_pred = classifier.fit(X_train, Y_train).predict(X_test)

for lr in np.r_[0.001:10:0.002]:
    mlp = MLPClassifier(hidden_layer_sizes=(8, 8), max_iter=10, alpha=1e-4,
            solver='sgd', verbose=10, tol=1e-4, random_state=1,
            learning_rate=lr)

    print(mlp.fit(X_train, Y_train))
    Acc = accuracy_score(Y_test, Y_pred)
    plt(learning_rate, Acc)

,错误如下:

ValueError: learning rate 0.001 is not supported. 

1 个答案:

答案 0 :(得分:0)

您是否需要以从0.0001到10的每个学习速率运行MLPClassifier?如果是这样,则您必须循环运行分类器,每次都更改学习率。如果您需要以一定的时间间隔获得学习率,也必须将步长定义在0.001到10之间,例如0.0001、0.0005、0.0010,.... 10。

假设您有一个在这些时间间隔内的学习率列表,

learning_rates = [0.001, 0.005, ..., 10]

for lr in learning_rates:
    mlp = MLPClassifier(hidden_layer_sizes=(8, 8), max_iter=10, alpha=1e-4,
                solver='sgd', verbose=10, tol=1e-4, random_state=1,
                learning_rate=lr)

    print(mlp.fit(X_train, Y_train))
    print("Training set score: %f" % mlp.score(X_train, Y_train))
    print("Test set score: %f" % mlp.score(X_test, Y_test))

您现在可以将用于训练和测试集的mlp.score收集到单独的列表中,并根据matplotlib中的学习率对其进行绘图。

希望这会有所帮助!

我认为您的困惑在于与算法相关的max_iter参数,而不是学习率的上限。