保存不同时期 model.fit 的历史记录

Save history of model.fit for different epochs

我正在用 epoch=10 训练我的模型。我再次使用 epoch=3 重新训练。又是纪元 5。 所以每次我用 epoch=10、3、5 训练模型时。我想结合所有 3 的历史。例如,对于 epoch=10,让 h1 = model.fit 的历史,h2 = [ 的历史=19=] 对于纪元=3, h3 = model.fit 时期=5 的历史记录。

现在在变量 h 中,我想要 h1 + h2 + h3。所有历史记录都附加到单个变量,以便我可以绘制一些图表。

密码是,

start_time = time.time()

model.fit(x=X_train, y=y_train, batch_size=32, epochs=10, validation_data=(X_val, y_val), callbacks=[tensorboard, checkpoint])

end_time = time.time()
execution_time = (end_time - start_time)
print(f"Elapsed time: {hms_string(execution_time)}")


start_time = time.time()

model.fit(x=X_train, y=y_train, batch_size=32, epochs=3, validation_data=(X_val, y_val), callbacks=[tensorboard, checkpoint])

end_time = time.time()
execution_time = (end_time - start_time)
print(f"Elapsed time: {hms_string(execution_time)}")

start_time = time.time()

model.fit(x=X_train, y=y_train, batch_size=32, epochs=5, validation_data=(X_val, y_val), callbacks=[tensorboard, checkpoint])

end_time = time.time()
execution_time = (end_time - start_time)
print(f"Elapsed time: {hms_string(execution_time)}")

每次调用 model.fit() 时,它 returns 一个 keras.callbacks.History 对象,其 history 属性包含一个 字典 。字典的键是用于训练的 loss,用于验证损失的 val_loss,以及任何其他 metrics 你可能在编译时设置了。

因此,对于您的情况,您可以这样做:

hist1 = model.fit(...)

# other code lines

hist2 = model.fit(...)

# other code lines

hist3 = model.fit(...)

# create an empty dict to save all three history dicts into
total_history_dict = dict()

for some_key in hist1.keys():
    current_values = [] # to save values from all three hist dicts
    for hist_dict in [hist1.history, hist2.history, hist3.history]:
        current_values += hist_dict[some_key]
    total_history_dict[some_key] = current_values

现在,total_history_dict 是一个字典键,它的键和往常一样 loss, val_loss, 其他指标 和值列表显示每个时期的 loss/metrics。 (列表的长度将是对 model.fit 的所有三个调用中的纪元数的总和)

您现在可以使用字典通过 matplotlib 绘制事物或将其保存到 pandas 数据框等...

您可以通过创建一个 class 来实现此功能,该 class 是 tf.keras.callbacks.Callback 的子 class 并使用该 class 的对象作为对 [=13= 的回调].

import csv
import tensorflow.keras.backend as K
from tensorflow import keras
import os

model_directory='./xyz' # directory to save model history after every epoch 

class StoreModelHistory(keras.callbacks.Callback):

  def on_epoch_end(self,batch,logs=None):
    if ('lr' not in logs.keys()):
      logs.setdefault('lr',0)
      logs['lr'] = K.get_value(self.model.optimizer.lr)

    if not ('model_history.csv' in os.listdir(model_directory)):
      with open(model_directory+'model_history.csv','a') as f:
        y=csv.DictWriter(f,logs.keys())
        y.writeheader()

    with open(model_directory+'model_history.csv','a') as f:
      y=csv.DictWriter(f,logs.keys())
      y.writerow(logs)


model.fit(...,callbacks=[StoreModelHistory()])

然后您可以加载 csv 文件并绘制模型的损失、学习率、指标等。

import pandas as pd
import matplotlib.pyplot as plt

EPOCH = 10 # number of epochs the model has trained for

history_dataframe = pd.read_csv(model_directory+'model_history.csv',sep=',')


# Plot training & validation loss values
plt.style.use("ggplot")
plt.plot(range(1,EPOCH+1),
         history_dataframe['loss'])
plt.plot(range(1,EPOCH+1),
         history_dataframe['val_loss'],
         linestyle='--')
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()

2020 年,您可以在 CSVLogger 中使用 append=True

保存示例:

epoch,accuracy,loss,val_accuracy,val_loss
0,0.7649424076080322,0.49990198016166687,0.6675007939338684,0.8114446401596069
1,0.8209356665611267,0.406406044960022,0.7569596767425537,0.5224416851997375