简体   繁体   English

如何在 Keras 的每个时期保存训练历史?

[英]How to save training history on every epoch in Keras?

I can't keep my PC running all day long, and for this I need to save training history after every epoch.我不能让我的电脑整天运行,为此我需要在每个时期后保存训练历史。 For example, I have trained my model for 100 epochs in one day, and on the next day, I want to train it for another 50 epochs.例如,我在一天内训练了我的模型 100 个 epoch,第二天我想再训练它 50 个 epoch。 I need to generate the loss vs epoch and accuracy vs epoch graphs for the whole 150 epochs.我需要生成整个 150 个时代的损失与时代和准确度与时代的关系图。 I am using fit_generator method.我正在使用fit_generator方法。 Is there any way to save the training history after every epoch (most probably using Callback )?有没有办法在每个时期之后保存训练历史(最有可能使用Callback )? I know how to save the training history after the training has ended.我知道如何在培训结束后保存培训历史。 I am using Tensorflow backend.我正在使用 Tensorflow 后端。

Keras has the CSVLogger callback which appears to do exactly what you need; Keras 具有 CSVLogger 回调,它似乎完全符合您的需求; from the documentation :文档

Callback that streams epoch results to a CSV file.将纪元结果流式传输到 CSV 文件的回调。

It has an append parameter for adding to the file.它有一个附加参数用于添加到文件中。 Again, from the documentation:再次,从文档中:

append : Boolean.追加:布尔值。 True: append if file exists (useful for continuing training). True:如果文件存在则追加(用于继续培训)。 False: overwrite existing file错误:覆盖现有文件

from keras.callbacks import CSVLogger

csv_logger = CSVLogger("model_history_log.csv", append=True)
model.fit_generator(...,callbacks=[csv_logger])

I had a similar requirement, I went for a naive approach.我有一个类似的要求,我选择了一种天真的方法。

1.Python code to run for 50 Epochs: 1.运行 50 个 Epoch 的 Python 代码:
I saved the history of the model and the model itself trained for 50 epochs.我保存了模型的历史和模型本身训练了 50 个 epochs。 .history is used to store entire history of the trained model. .history用于存储训练模型的整个历史。

history = model.fit_generator(......) # training the model for 50 epochs
model.save("trainedmodel_50Epoch.h5") # saving the model
with open('trainHistoryOld', 'wb') as handle: # saving the history of the model
    dump(history.history, handle)

2.Python code for loading the trained model and training for another 50 epochs: 2. 加载训练好的模型并训练另外 50 个 epoch 的 Python 代码:

from keras.models import load_model
model = load_model('trainedmodel_50Epoch.h5')# loading model trained for 50 Epochs

hstry = model.fit_generator(......) # training the model for another 50 Epochs

model.save("trainedmodel_50Epoch.h5") # saving the model 

with open('trainHistoryOld', 'wb') as handle: # saving the history of the model trained for another 50 Epochs
    dump(hstry.history, handle)

from pickle import load
import matplotlib.pyplot as plt

with open('trainHistoryOld', 'rb') as handle: # loading old history 
    oldhstry = load(handle)

oldhstry['loss'].extend(hstry['loss'])
oldhstry['acc'].extend(hstry['acc'])
oldhstry['val_loss'].extend(hstry['val_loss'])
oldhstry['val_acc'].extend(hstry['val_acc'])

# Plotting the Accuracy vs Epoch Graph
plt.plot(oldhstry['acc'])
plt.plot(oldhstry['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

# Plotting the Loss vs Epoch Graphs
plt.plot(oldhstry['loss'])
plt.plot(oldhstry['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

You can create custom class too as mentioned in the answer provided earlier.您也可以创建自定义类,如前面提供的答案中所述。

To save model history you have two options.要保存模型历史,您有两个选择。

  1. Use keras ModelCheckPoint callback class使用keras ModelCheckPoint回调类
  2. Create custom class创建自定义类

Here is how to create custom checkpoint call back class.以下是如何创建自定义检查点回调类。

class CustomModelCheckPoint(keras.callbacks.Callback):
    def __init__(self,**kargs):
        super(CustomModelCheckPoint,self).__init__(**kargs)
        self.epoch_accuracy = {} # loss at given epoch
        self.epoch_loss = {} # accuracy at given epoch

    def on_epoch_begin(self,epoch, logs={}):
        # Things done on beginning of epoch. 
        return

    def on_epoch_end(self, epoch, logs={}):
        # things done on end of the epoch
        self.epoch_accuracy[epoch] = logs.get("acc")
        self.epoch_loss[epoch] = logs.get("loss")
        self.model.save_weights("name-of-model-%d.h5" %epoch) # save the model

Now to use the call back class现在使用回调类

checkpoint = CustomModelCheckPoint()
model.fit_generator(...,callbacks=[checkpoint])

now checkpoint.epoch_accuracy dictionary contains accuracies at a given epoch and checkpoint.epoch_loss dictionary contains losses at a given epoch现在checkpoint.epoch_accuracy字典包含给定时期的精度,而checkpoint.epoch_loss字典包含给定时期的损失

You can save the training history as follows 您可以按如下方式保存培训历史记录

hist = model.fit_generator(generator(features, labels, batch_size), samples_epoch=50, nb_epoch=10)
import pickle
with open('text3', 'wb') as f:
    pickle.dump(hist.history, f)

To save the training history after each epoch 在每个纪元后保存训练历史

import pickle
hist1 = []
for _ in range(10):
   hist = model.fit(X_train, y_train, epochs=1, batch_size=batch_size, validation_split=0.1)
   hist1.append(hist.history)

with open('text3', 'wb') as f:
    pickle.dump(hist1.history, f)

For the checkpoint 对于检查站

filepath="weights.best.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
# Fit the model
model.fit(X, Y, validation_split=0.33, epochs=150, batch_size=10, callbacks=callbacks_list, verbose=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 keras:如何保存历史对象的训练历史属性 - keras: how to save the training history attribute of the history object 如何保存Keras的培训历史以进行交叉验证(循环)? - How to save the training history of Keras for a cross valication(a loop)? 如何在 Keras 的每个训练周期后进行预测? - How to predict after each epoch of training in Keras? Keras:如何在每个 epoch 之后存储历史? - Keras: How to store history after each epoch? 我如何在每个时期保存检查点并加载随机保存的检查点以继续训练 - How I can save checkpoint every epoch and load an random saved checkpoint to continue training Keras:如何在Ipython控制台中检索训练历史记录 - Keras: how to retrieve training history in Ipython console 如何为每个时期保存 keras model 的权重? - How to save weights of keras model for each epoch? 如何在 Keras 的自定义批量训练中获得每个时期的损失? - How to get the loss for each epoch in custom batch training in Keras? 如何在 tf.keras 训练期间获得当前时代的进度? - How to get progress of current epoch during tf.keras training? 是否可以使用张量流回调将纪元结果记录在tf.keras模型中,以便在训练结束时保存? - Is it possible to log the epoch results in the tf.keras model using a tensorflow callback, in order to save at the end of training?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM