1.保存訓(xùn)練中間值和加載中間值
使用callback可以在每個(gè)批次之后保存weights
filepath = 'saver/weights1.{epoch:02d}-{loss:.2f}.hdf5'
ck = keras.callbacks.ModelCheckpoint(filepath,monitor='loss',verbose=0)
h = model.fit(X, y,batch_size=128,epochs=100,verbose=2,callbacks =[ck])
加載
path = 'saver/weights.146-1.11.hdf5'
model.load_weights(path)
2.可視化這些文件名中的損失值
由于rnn可能要訓(xùn)練很多批次才有效果矮烹,會產(chǎn)生很多中間文件煞烫,文件名中有l(wèi)oss值蜘渣,這個(gè)值就可以用來畫出圖甲葬。
import os
import matplotlib.pyplot as plt
%matplotlib inline
filenames = os.listdir('saver')
gettime = os.path.getmtime#獲取時(shí)間函數(shù)
ft = {f:gettime('saver/'+f) for f in filenames}
ss = sorted(ft.items(),key = lambda item:item[1])#按照值排序
#匿名函數(shù)中item是參數(shù)瘾英,item[1]是返回參數(shù)
losses = [float(s[0][-9:-5]) for s in ss]#從文件名中截取損失值
x = [i for i in range(len(losses))]
plt.plot(x,losses)