【发布时间】: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