进行Scikit学习的MLPRegressor训练直至适合与逐步训练,使用相同的设置和数据集,结果截然不同

时间:2019-03-25 14:36:34

标签: python scikit-learn neural-network

我很难理解以下两种方法为何产生明显不同的结果:

    1.方案A(EVS .9,过度拟合):将MLPRegressor拟合到训练数据集,以极低的容差停止。
    2.场景B(最终EVS .4):除了以下内容外,将MLPRegressor拟合到具有相同设置的相同训练数据集:
                -warm_start =第一次迭代后为真
            -max_iter = 1
            -循环迭代到
场景B中停止的迭代之后的次数,迭代了500步,场景A在444步时自动停止了

方案B的EVS永远不会接近方案A(即使我将方案B进行5万次迭代)也是如此。我在某种程度上不能正确地完成训练迭代吗?

方案A:

    from sklearn.neural_network import MLPRegressor

    modelnm = 'MLPRegressor_simple'
    estimator_reg = MLPRegressor(
        solver='adam',
        activation='relu',
        learning_rate='adaptive',
        learning_rate_init=.001,
        hidden_layer_sizes=[400, 300, 200],
        alpha=0.000000001,
        max_iter=100000,
        random_state=42,
        tol=0.0000000000000001,
        batch_size='auto',
        shuffle=True,
        early_stopping=False, #doesn't seem to do anything
        warm_start=False,
        #max_iter=1,
        beta_1=0.9,
        beta_2=0.95,
        epsilon=0.0000000001,
        verbose=0,
    )

    estimator_reg.fit(train_data, train_labels)
    model_fit = estimator_reg

    bp = estimator_reg.get_params()

    data_predictions = model_fit.predict(train_data)

    mymsg = 'n_iter=' + str(model_fit.n_iter_)
    print(mymsg)
    dxs = 'Train'
    fin_001(modelnm, data_predictions, train_labels, model_fit, bp, dxs, pd_train_IDS)

    data_predictions = model_fit.predict(test_data)
    dxs = 'Test'
    fin_001(modelnm, data_predictions, test_labels, model_fit, bp, dxs, pd_test_IDS)

场景A的结果:

    train_data.shape=(19700, 395)
    n_iter=444
    ('MLPRegressor_simpleTrain EVS = ', 0.8905082198798907)

场景B:

    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.neural_network import MLPRegressor

    scores_train = []
    scores_test = []
    scores_train_b = []
    scores_test_b = []

    estimator_reg = MLPRegressor(
        solver='adam',
        activation='relu',
        learning_rate='adaptive',
        learning_rate_init=.001,
        hidden_layer_sizes=[400, 300, 200],
        alpha=0.000000001,
        max_iter=1,
        random_state=42,
        tol=0.0000000000000001,
        batch_size='auto',
        shuffle=True,
        early_stopping=False,
        warm_start=False,
        beta_1=0.9,
        beta_2=0.95,
        epsilon=0.0000000001,
        verbose=0,
    )
    estimator_reg.fit(train_data, train_labels)
    res= 0.001
    estimator_reg.set_params(learning_rate_init=res. warm_start=True)
    last_stm = 0
    bestiter = 0
    bestiterfnd = 0
    besttestscore = 0.0
    for i in range(500):
        estimator_reg.fit(train_data, train_labels)
        model_fit = estimator_reg
        data_predictions_tr = model_fit.predict(train_data)
        evs_tr = explained_variance_score(train_labels, data_predictions_tr)
        scores_train.append(model_fit.score(train_data, train_labels))

        data_predictions_ts = model_fit.predict(test_data)
        evs_ts = explained_variance_score(test_labels, data_predictions_ts)
        scores_test.append(model_fit.score(test_data, test_labels))

        Xc = 5 # Span of mean
        if len(scores_test) - Xc < 0:
            mymin = 0
        else:
            mymin = len(scores_test) - Xc

        mymax = len(scores_test)
        #print (scores_test[mymin:mymax])
        print('LR=' + str(res) + ' rnd=00 iter=' + str(i) + ' moving avg=' + str(np.average(scores_test[mymin:mymax])))
        scores_test_mean = np.average(scores_test[mymin:mymax])

        #if bestiterfnd == 0:
        if scores_test_mean > last_stm:
            bestiterfnd = 1
            bestiter = i
            besttestscore = model_fit.score(test_data, test_labels)
            last_stm = scores_test_mean
    r0i= i
    for pos in range(len(scores_train)):     # by index
        print(str(pos) + '.    ' + str(scores_train[pos])[:5])

场景B结果:

    train_data.shape=(19700, 395)
    0.    -0.04
    1.    0.016
    2.    0.043
    3.    0.046
    ...
    442.    0.410
    443.    0.422
    444.    0.447
    445.    0.474
    ...
    498.    0.426
    499.    0.414

0 个答案:

没有答案