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)))
非常感謝您耐心的回答! –