保存模型的历史记录。适合不同时期

问题描述 投票:0回答:1

我正在用epoch = 10训练我的模型。我再次用epoch = 3进行了训练。并再次进入时代5。因此,每次我用epoch = 10、3、5训练模型时,我都想结合所有3个的历史记录。对于例子,假设h1 = model.fit的历史记录为epoch = 10,h2 = model.fit的历史记录对于epoch = 3,h3 =模型的历史记录,适合时代= 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)}")

tensorflow keras neural-network epoch
1个回答
0
投票

每次调用model.fit()时,它都会返回一个keras.callback.History对象,其history属性包含dictionary。字典的键是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是其字典键,它们的键像往常一样损失val_loss其他度量,并且值列表显示每个时期的损失/度量。 (列表的长度将是对[[model.fit的所有三个调用中的纪元数的总和)。

© www.soinside.com 2019 - 2024. All rights reserved.