我无法让我的电脑一整天都在运行,为此我需要在每个时代之后保存培训历史记录。例如,我在一天内训练了100个时代的模型,第二天,我想再训练50个时代。我需要为整个150个时期生成损失与时代和精确度与时代图的关系。我正在使用fit_generator
方法。有没有办法在每个纪元后保存训练历史记录(最有可能使用Callback
)?我知道如何在培训结束后保存培训历史。我正在使用Tensorflow后端。
答案 0 :(得分:2)
我有类似的要求,我采取了一种天真的方法。
1.Python代码运行50个时代:
我保存了模型的历史和模型本身训练了50个时代。 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)
用于存储受过训练的模型的完整历史记录。
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()
2.Python代码,用于加载经过训练的模型和另外50个时期的训练:
UITableView
您也可以像前面提供的答案中提到的那样创建自定义类。
答案 1 :(得分:2)
Keras具有CSVLogger回调,该回调似乎完全可以满足您的需要:Callbacks - Keras Documentation
从文档中:
“将历元结果传输到csv文件的回调。”
它具有用于添加到文件的附加参数。再次,从文档中:
“追加:True:如果文件存在则追加(用于继续训练)。False:覆盖现有文件”
from keras.callbacks import CSVLogger
csv_logger = CSVLogger("model_history_log.csv", append=True)
model.fit_generator(...,callbacks=[csv_logger])
答案 2 :(得分:1)
要保存模型历史记录,您有两种选择。
以下是如何创建自定义检查点回调类。
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
现在使用回拨类
checkpoint = CustomModelCheckPoint()
model.fit_generator(...,callbacks=[checkpoint])
现在checkpoint.epoch_accuracy
字典包含给定时期的准确度,checkpoint.epoch_loss
字典包含给定时期的损失
答案 3 :(得分:0)
您可以按如下方式保存培训历史记录
function delRows51() {
var ss = SpreadsheetApp.getActiveSheet();
ss.deleteRows(51, ss.getMaxRows() - 50);
};
要保存每次训练后的训练历史记录
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)
对于检查点
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)