gpt4 book ai didi

python - 在 Sklearn 中再次绘制测试、有效和训练 Acc 的 Epoch

转载 作者:太空宇宙 更新时间:2023-11-03 21:47:53 24 4
gpt4 key购买 nike

是否有任何内置方法可以在 Sklearn 中为 MLP 分类器绘制每个时期的训练图、有效图、测试图?

最佳答案

此解决方案(代码取自 here )应该可以帮助您:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_mldata
from sklearn.neural_network import MLPClassifier
np.random.seed(1)

""" Example based on sklearn's docs """
mnist = fetch_mldata("MNIST original")
# rescale the data, use the traditional train/test split
X, y = mnist.data / 255., mnist.target
X_train, X_test = X[:60000], X[60000:]
y_train, y_test = y[:60000], y[60000:]

mlp = MLPClassifier(hidden_layer_sizes=(50,), max_iter=10, alpha=1e-4,
solver='adam', verbose=0, tol=1e-8, random_state=1,
learning_rate_init=.01)

""" Home-made mini-batch learning
-> not to be used in out-of-core setting!
"""
N_TRAIN_SAMPLES = X_train.shape[0]
N_EPOCHS = 25
N_BATCH = 128
N_CLASSES = np.unique(y_train)

scores_train = []
scores_test = []

# EPOCH
epoch = 0
while epoch < N_EPOCHS:
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 + N_BATCH]
mlp.partial_fit(X_train[indices], y_train[indices], classes=N_CLASSES)
mini_batch_index += N_BATCH

if mini_batch_index >= N_TRAIN_SAMPLES:
break

# SCORE TRAIN
scores_train.append(mlp.score(X_train, y_train))

# SCORE TEST
scores_test.append(mlp.score(X_test, y_test))

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()

关于python - 在 Sklearn 中再次绘制测试、有效和训练 Acc 的 Epoch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52349169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com