【问题标题】:How to plot accuracy and loss curves for train and test data in MLPClassifier using sklean?如何使用 sklean 在 MLPClassifier 中绘制训练和测试数据的准确度和损失曲线?
【发布时间】:2021-08-18 13:17:07
【问题描述】:

我正在使用这个非常简单的代码来训练 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)

它非常准确。现在我的问题是: 如何获得模型的最大精度,因为每次运行代码都会更改它?由于随机选择训练测试数据,有没有办法找到最大可能的准确度?

其次,我想绘制火车和 Val 数据的准确度和损失曲线。 我知道了

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

但不知道如何使用它们并尝试过,但为什么自启动以来 val loss 很低 enter image description here

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

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)

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

我做错了什么请一位指导。

【问题讨论】:

    标签: classification mlp


    【解决方案1】:

    只要放就得到结果

    early_stopping=False,warm_start=True
    

    在 MLPClassifier 中。对它了解不多,但解决了目的。 enter image description here

    【讨论】:

      猜你喜欢
      • 2019-03-07
      • 2021-04-23
      • 1970-01-01
      • 2021-02-07
      • 2021-09-24
      • 2022-10-05
      • 1970-01-01
      • 2020-09-05
      • 2021-05-30
      相关资源
      最近更新 更多