绘制和保存kfold训练历史



我正在尝试用kfold交叉验证训练模型,现在我想保留历史以绘制和保存历史。我该怎么做呢?

似乎有些问题张贴这个问题的答案,但我想保存和绘图所有的历史一次,而不是与parted文件

num_folds = 10
kfold = KFold(n_splits=num_folds, shuffle=True)
# K-fold Cross Validation model evaluation
fold_no = 1
a = []
for train, test in kfold.split(X, label):
print("---"*20)
history = siamese.fit(
[tf.gather(X[:,0], train),tf.gather(X[:,1], train)],
tf.gather(label, train),
validation_data=([tf.gather(X[:,0], test),tf.gather(X[:,1], test)], tf.gather(label, test)),
batch_size=batch_size,
epochs=epochs,
)
a.append(history)

通过在代码中添加字典,我可以一次使用所有的历史记录

num_folds = 10
kfold = KFold(n_splits=num_folds, shuffle=True)
# K-fold Cross Validation model evaluation
fold_no = 1
histories = {'accuracy':[], 'loss':[], 'val_accuracy':[], 'val_loss':[]}
for train, test in kfold.split(X, label):
print("---"*20)
history = siamese.fit(
[tf.gather(X[:,0], train),tf.gather(X[:,1], train)],
tf.gather(label, train),
validation_data=([tf.gather(X[:,0], test),tf.gather(X[:,1], test)], tf.gather(label, test)),
batch_size=batch_size,
epochs=epochs,
)
histories['accuracy'].append(history.history['accuracy'])
histories['loss'].append(history.history['loss'])
histories['val_accuracy'].append(history.history['val_accuracy'])
histories['val_loss'].append(history.history['val_loss'])
with open('./trainHistoryDict', 'wb') as file_pi:
pickle.dump(histories, file_pi)

最新更新