如何获得 MLPRegressor 每次迭代的训练和测试分数?

How can I get the train and test scores for each iteration of a MLPRegressor?

这个答案似乎正是我需要的,但它是回归器而不是分类器。

我对 link 中 sascha 提供的代码进行了非常小的修改,如下所示。我认为将它用于我的 MLPRegressior 会相当简单...但是我收到一条错误消息我不知道如何修复任何帮助将不胜感激:

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


estimator_reg = MLPRegressor(
    solver='adam',
    activation='relu',
    learning_rate='adaptive',
    learning_rate_init=.01,
    hidden_layer_sizes=[100],
    alpha=0.01,
    max_iter=1000,
    random_state=42,
    tol=0.0001,
    early_stopping=False,
    warm_start=True,
    beta_1=0.7,
    beta_2=0.98,
    epsilon=0.0000000001,
    verbose=10,
)

""" Home-made mini-batch learning
    -> not to be used in out-of-core setting!
"""
N_TRAIN_SAMPLES = train_data.shape[0]
N_EPOCHS = 25
N_BATCH = 128


scores_train = []
scores_test = []

# EPOCH
epoch = 0
while epoch < N_EPOCHS:
    print('epoch: ', epoch)
    # SHUFFLING
    random_perm = np.random.permutation(train_data.shape[0])
    mini_batch_index = 0
    while True:
        # MINI-BATCH
        indices = random_perm[mini_batch_index:mini_batch_index + N_BATCH]
        estimator_reg.partial_fit(train_data[indices], train_labels[indices])
        mini_batch_index += N_BATCH

        if mini_batch_index >= N_TRAIN_SAMPLES:
            break

    # SCORE TRAIN
    scores_train.append(estimator_reg.score(train_data, train_labels))

    # SCORE TEST
    scores_test.append(estimator_reg.score(test_data, test_labels))

    epoch += 1

""" Plot """
fig, ax = plt.subplots(2, sharex=True, sharey=True)
ax[0].plot(scores_train)
ax[0].set_title('Train')
ax[1].plot(scores_test)
ax[1].set_title('Test')
fig.suptitle("Accuracy over epochs", fontsize=14)
plt.show()

我得到这个错误:
KeyError Traceback(最后一次调用)
在 ()
---> 46 estimator_reg.partial_fit(train_data[指数], train_labels[指数])
......
......
KeyError:'[789 1493 353 33 1011 2029 1696 1649 1649 653 1648 22 2477 2120 1000\n 2481 2448 1704 1962 2291 2291 1995 2085 710 967 1839 461 461 461 461 461 504 1650 [= 25 = 25 = 25 =] 1286 448 2049 1791\n 141 1168 1249 159 2061 2456 431 1799 2249 2379 1169 1044 1010 120\n 2503 316 1070 671 1005 2164 975 2371 811 1555 1193 1316 487 1867\n 1262 1395 135 2224 32 1509 2132 997 263 233 1614 2317 1432 49\n 1251 2227 2536 1955 359 650 2287 792 1900 606 763 1837 742 965\n 1190 53 910 2486 738 103 1965 99 1084 123 1061 806 384 2261\n 2284 2114 360 1075 1479 1446 455 2294 221 1856 979 1078 2106 189\n 2153 1183] 不在索引中'

我猜您的索引不在 (0,N_TRAIN_SAMPLES) 范围内。
如果您删除或过滤了一些行,或者索引从一开始就包含一些不在该范围内的数字,则可能会发生这种情况。

尝试更改此行:

random_perm = np.random.permutation(train_data.shape[0])

进入这个:

random_perm = np.random.permutation(train_data.index.values)