如何使用 sklean 在 MLPClassifier 中绘制训练和测试数据的精度和损失曲线?

How to plot accuracy and loss curves for train and test data in MLPClassifier using sklean?

我正在使用这个非常简单的代码来训练 MLPClassifier。

x_train, x_test, y_train, y_test = load_data(test_size=0.25)
model = MLPClassifier(alpha=0.01,
                      batch_size=128,
                      epsilon=1e-08,
                      hidden_layer_sizes=(300,),
                      learning_rate='adaptive',
                      max_iter=500,
                      early_stopping=True)
model.fit(x_train, y_train)
y_pred = model.predict(x_test)
accuracy = accuracy_score(y_true=y_test, y_pred=y_pred)

它非常准确。现在我的问题是:

其次,我想绘制 train 和 Val 数据的准确性和损失曲线。 我开始了解

plt.plot(model.loss_curve_)
plt.plot(model.validation_scores_)

但不知道如何使用它们并尝试了这个,但为什么 val_loss 自开始以来一直很低:

我只尝试过来自这个社区的以下代码

scores_train = []
scores_test = []

# EPOCH
epoch = 0
while epoch < n_epoch:
    print('epoch: ', epoch)
    # SHUFFLING
    random_perm = np.random.permutation(x_train.shape[0])
    mini_batch_index = 0
    while True:
        # MINI-BATCH
        indices = random_perm[mini_batch_index:mini_batch_index + 128]
        model.partial_fit(x_train[indices], y_train[indices], classes=7)
        mini_batch_index += 128

        if mini_batch_index >= x_train.shape[0]:
            break

    # SCORE TRAIN
    scores_train.append(model.score(x_train, y_train))

    # SCORE TEST
    scores_test.append(model.score(x_test, y_test))

    epoch += 1

""" Plot """

plt.plot(scores_train, color='green', alpha=0.8, label='Train')
plt.plot(scores_test, color='magenta', alpha=0.8, label='Test')
plt.title("Accuracy over epochs", fontsize=14)
plt.xlabel('Epochs')
plt.legend(loc='upper left')
plt.show()

但它在第 :

行抛出错误
model.partial_fit(x_train[indices], y_train[indices], classes=7)

returns:

Error: only integer scalar arrays can be converted to a scalar index

我做错了什么请指导。

只要输入就可以得到结果

MLPClassifier(early_stopping=False, warm_start=True)

MLPClassifier() 中。不太了解,但解决了目的。