1
对于交叉验证,如何保存不同训练集和交叉验证集的训练历史记录?我认为pickle写入的一种附加模式会起作用,但实际上它不起作用。如果可能的话,您能否请您指导保存所有模型的方法,现在我只能使用
model.save(file)
保存上次训练过的模型。如何保存Keras的训练历史作为交叉验证(循环)?
historyfile = 'history.pickle'
f = open(historyfile,'w')
f.close()
ind = 0
save = {}
for train, test in kfold.split(input,output):
ind = ind+1
#create model
model = model_FCN()
# fit the model
history = model.fit(input[list(train)], output[list(train)], batch_size = 16, epochs = 100, verbose =1, validation_data =(input[list(test)],output[list(test)]))
#save to file
try:
f = open(historyfile,'a') ## appending mode??
save['cv'+ str(ind)]= history.history
pickle.dump(save, f, pickle.HIGHEST_PROTOCOL)
f.close()
except Exception as e:
print('Unable to save data to', historyfile, ':', e)
scores = model.evaluate(MR_patch[list(test)], CT_patch[list(test)], verbose=0)
print("%s: %.2f" % (model.metrics_names[1], scores[1]))
cvscores.append(scores[1])
print("cross validation stage: " + str(ind))
print("%.2f (+/- %.2f)" % (np.mean(cvscores), np.std(cvscores)))
非常感谢您耐心的回答! –