我正在Keras培训一个模型,在那里我使用以下代码保存了所有内容
filepath = "project_model.hdh5"
checkpoint = ModelCheckpoint("project_model.hdf5", monitor='loss', verbose=1,
save_best_only=False, mode='auto', period=1)
然后我使用以下代码来运行培训
for _ in range(20):
for j in range(len(mfcc_data_padded_transposed[j])):
batch_input=[mfcc_data_padded_transposed[j]]
batch_input = np.array(batch_input)
batch_input = batch_input/np.max(batch_input)
batch_output = [y_labels_mfcc[j]]
batch_output = np.array(batch_output)
input_lengths2 = input_lengths_mfcc[j]
label_lengths2 = label_lengths_mfcc[j]
input_lengths2 = np.array(input_lengths2)
label_lengths2 = np.array(label_lengths2)
inputs = {'the_input': batch_input,
'the_labels': batch_output,
'input_length': input_lengths2,
'label_length': label_lengths2}
outputs = {'ctc': np.zeros([1])}
model.fit(inputs, outputs, epochs=1, verbose =1, callbacks=[checkpoint])
我做上述操作是为了试验检查点,因为我不确定我是否正确使用了它
现在,这个培训是以.001的学习率完成的。现在,在运行了一段时间的训练循环之后,如果我决定将学习速率更改为.002,我是否必须运行与模型相关的所有代码(模型结构,然后是优化,等等)?如果我这样做了,我如何从停止训练时的前一个状态加载?另一个问题是,如果我重新启动电脑,并使用我之前在这里共享的检查点代码运行jupyter计算单元,这会替换以前保存的文件吗?加载保存的文件和重量并从那里恢复训练的理想方法是什么?我问这个问题的原因是,当我遵循Keras文档时,它似乎只是从一开始就开始了
您可以在培训期间或加载模型后更新学习率
记住,学习率不属于模型体系结构,它属于优化器(在模型编译期间分配)。学习率是一个超参数,在梯度下降过程中调节权重更新的幅度(如下所示α):
因此,在初始培训之后,您可以加载(保存的)模型,以新的学习速率更新优化器(可能还可以为编译器分配自定义对象),然后继续培训。请记住,在长时间训练模型后更改优化器本身可能会产生较差的精度结果,因为您的模型现在必须根据新优化器的权重计算重新校准
在Keras中,您可以选择保存/加载整个模型(包括体系结构、权重、优化器状态;或者只保存权重;或者只保存体系结构(source))
要保存/加载整个模型,请执行以下操作:
要仅保存/加载模型权重:
也可以在模型加载期间指定自定义对象:
取决于检查点中使用的文件路径:“如果文件路径为weights.{epoch:02d}-{val_loss:.2f}.hdf5,则模型检查点将与文件名中的历元号和验证丢失一起保存”。因此,如果您对文件路径使用唯一格式,则可以避免覆盖以前保存的模型。source
例如:
结果
结果:
有关具体案例的更多信息,请访问Keras FAQ
相关问题 更多 >
编程相关推荐