是否有可能从特定时期继续训练?
Is it possible to continue training from a specific epoch?
我用来适应 Keras 模型的资源管理器将对服务器的访问限制为一次 1 天。这一天之后,我需要开始一份新工作。 Keras 是否可以在 epoch K 保存当前模型,然后加载该模型以继续训练 epoch K+1(即,使用新作业)?
您可以通过指定回调在每个纪元后保存权重:
weight_save_callback = ModelCheckpoint('/path/to/weights.{epoch:02d}-{val_loss:.2f}.hdf5', monitor='val_loss', verbose=0, save_best_only=False, mode='auto')
model.fit(X_train,y_train,batch_size=batch_size,nb_epoch=nb_epoch,callbacks=[weight_save_callback])
这将在每个纪元后保存权重。然后你可以加载它们:
model = Sequential()
model.add(...)
model.load('path/to/weights.hf5')
当然,您的模型在两种情况下都需要相同。
您可以添加 initial_epoch
参数。这将允许您从特定时期继续训练。
您可以在下一个纪元自动开始训练..!
您需要使用 训练日志文件 跟踪您的训练,如下所示:
from keras.callbacks import ModelCheckpoint, CSVLogger
if len(sys.argv)==1:
model=... # you start training normally, no command line arguments
model.compile(...)
i_epoch=-1 # you need this to start at epoch 0
app=False # you want to start logging from scratch
else:
from keras.models import load_model
model=load_model(sys.argv[1]) # you give the saved model as input file
with open(csvloggerfile) as f: # you use your training log to get the right epoch number
i_epoch=list(f)
i_epoch=int(i_epoch[-2][:i_epoch[-2].find(',')])
app=True # you want to append to the log file
checkpointer = ModelCheckpoint(savemodel...)
csv_logger = CSVLogger(csvloggerfile, append=app)
model.fit(X, Y, initial_epoch=i_epoch+1, callbacks=[checkpointer,csv_logger])
这就是所有的人!
我用来适应 Keras 模型的资源管理器将对服务器的访问限制为一次 1 天。这一天之后,我需要开始一份新工作。 Keras 是否可以在 epoch K 保存当前模型,然后加载该模型以继续训练 epoch K+1(即,使用新作业)?
您可以通过指定回调在每个纪元后保存权重:
weight_save_callback = ModelCheckpoint('/path/to/weights.{epoch:02d}-{val_loss:.2f}.hdf5', monitor='val_loss', verbose=0, save_best_only=False, mode='auto')
model.fit(X_train,y_train,batch_size=batch_size,nb_epoch=nb_epoch,callbacks=[weight_save_callback])
这将在每个纪元后保存权重。然后你可以加载它们:
model = Sequential()
model.add(...)
model.load('path/to/weights.hf5')
当然,您的模型在两种情况下都需要相同。
您可以添加 initial_epoch
参数。这将允许您从特定时期继续训练。
您可以在下一个纪元自动开始训练..!
您需要使用 训练日志文件 跟踪您的训练,如下所示:
from keras.callbacks import ModelCheckpoint, CSVLogger
if len(sys.argv)==1:
model=... # you start training normally, no command line arguments
model.compile(...)
i_epoch=-1 # you need this to start at epoch 0
app=False # you want to start logging from scratch
else:
from keras.models import load_model
model=load_model(sys.argv[1]) # you give the saved model as input file
with open(csvloggerfile) as f: # you use your training log to get the right epoch number
i_epoch=list(f)
i_epoch=int(i_epoch[-2][:i_epoch[-2].find(',')])
app=True # you want to append to the log file
checkpointer = ModelCheckpoint(savemodel...)
csv_logger = CSVLogger(csvloggerfile, append=app)
model.fit(X, Y, initial_epoch=i_epoch+1, callbacks=[checkpointer,csv_logger])
这就是所有的人!