如何解决如何使用 sklean 在 MLPClassifier 中绘制训练和测试数据的准确性和损失曲线?
我正在使用这个非常简单的代码来训练 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 损失很低 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',label='Test')
plt.title("Accuracy over epochs",fontsize=14)
plt.xlabel('Epochs')
plt.legend(loc='upper left')
plt.show()
但它在第 1 行抛出错误:model.partial_fit(x_train[indices],classes=7)
Error: only integer scalar arrays can be converted to a scalar index
我做错了什么,请一位指导。
解决方法
只需放置即可获得结果
early_stopping=False,warm_start=True
在 MLPClassifier 中。对它了解不多,但解决了目的。 enter image description here
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。