3

我使用了 pima-indians-diabetes.csv 数据集。我已经使用 Keras 构建了一个包含架构 12-8-1 的神经网络,并且能够完美地可视化训练历史。接下来,我尝试使用来自 scikit learn 的 MLPCLassifier 实现相同的模型。在这种情况下是否可以像我对 Keras 所做的那样实现训练历史曲线?我只需要可视化我的训练历史,即训练准确率、验证准确率、训练损失和验证损失,就像我对 Keras 所做的那样。我使用 Keras 的代码和曲线:

from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import StratifiedKFold
import numpy
numpy.random.seed(42)

# load pima indians dataset
dataset = numpy.loadtxt("/content/gdrive/My Drive/pima-indians-diabetes.csv", delimiter=",")

# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]

model = Sequential()
model.add(Dense(12, input_dim=8, kernel_initializer= 'uniform' , activation= 'relu' ))
model.add(Dense(8, kernel_initializer= 'uniform' , activation= 'relu' ))
model.add(Dense(1, kernel_initializer= 'uniform' , activation= 'sigmoid' ))
# Compile model
model.compile(loss= 'binary_crossentropy' , optimizer= 'adam' , metrics=[ 'accuracy' ])

history= model.fit(X, Y, validation_split=0.33, epochs=150, batch_size=10)

import matplotlib.pyplot as plt
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history[ 'accuracy' ])
plt.plot(history.history[ 'val_accuracy' ])
plt.title( 'model accuracy' )
plt.ylabel( 'accuracy' )
plt.xlabel( 'epoch' )
plt.legend([ 'train' , 'test' ], loc= 'lower right' )
plt.show()

import matplotlib.pyplot as plt
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history[ 'loss' ])
plt.plot(history.history[ 'val_loss' ])
plt.title( 'model loss' )
plt.ylabel( 'loss' )
plt.xlabel( 'epoch' )
plt.legend([ 'train' , 'test' ], loc= 'upper left' )
plt.show()

在此处输入图像描述 在此处输入图像描述

我的代码使用 sklearn 的 MLP 分类器:

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=42)

#Using MLPclassifier from sklearn
from sklearn.neural_network import MLPClassifier

clf1 = MLPClassifier(batch_size=10, max_iter=150, hidden_layer_sizes=(12, 8), verbose=True, 
early_stopping=True, random_state=42)

clf1.fit(X_train, y_train)

from sklearn.metrics import classification_report
#Predicting y for X_val
y_pred = clf1.predict(X_test)

print(classification_report(y_test, y_pred))

我能够生成分类报告,但没有图形可视化。如何像使用 Keras 一样使用 sklearn 的 MLPClassifier 生成曲线?

4

0 回答 0