我正在 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])
我做了上面的检查点试验,因为我不确定我是否正确使用它。
现在,此训练以 0.001 的学习率完成。现在,在运行训练循环一段时间后,如果我决定将学习率更改为 .002,我是否必须运行与模型相关的所有代码(模型结构,然后是优化等)?并说我做到了,我如何从停止训练时的先前状态加载?另一个问题是,如果我重新启动 PC,并使用我之前在此处共享的检查点代码运行 jupyter 单元,这会替换之前保存的文件吗?加载保存的文件和权重并从那里恢复训练的理想方法是什么?我问的原因是因为当我遵循 Keras 文档时,它似乎只是从头开始。
最佳答案
Now after running the training loop for a while if I decide to change the learning rate to say .002, would I have to run all the codes that are related to the models (the model structure, then the optimization, etc)?
您可以在训练期间或加载模型后更新学习率。
请记住,学习率不属于模型架构,它属于优化器(在模型编译期间分配)。学习率是一个超参数,它调节梯度下降过程中权重更新的大小(在下面表示为 alpha):
因此,在初始训练后,您可以加载(保存的)模型,使用新的学习率更新优化器(并可能为编译器分配自定义对象),然后继续训练。请记住,长时间训练模型后更改优化器本身可能会产生较差的准确度结果,因为您的模型现在必须重新校准以适应新优化器的权重计算。
how do I load from the previous state from when I stopped the training?
在 Keras 中,您可以选择保存/加载整个模型(包括架构、权重、优化器状态;或仅权重;或仅架构 (source)。
要保存/加载整个模型:
from keras.models import load_model
model.save('my_model.h5')
model = load_model('my_model.h5')
仅保存/加载模型权重:
model.save_weights('my_model_weights.h5')
model.load_weights('my_model_weights.h5')
您还可以在模型加载期间分配自定义对象:
model = load_model(filepath, custom_objects={'loss': custom_loss})
Other question is, if I restart the PC, and run the jupyter cell with checkpoint codes that I shared here earlier, would that replace the previously saved file?
取决于检查点中使用的文件路径:“如果文件路径是 weights.{epoch:02d}-{val_loss:.2f}.hdf5,则模型检查点将与 epoch 编号和验证损失一起保存在文件名”。因此,如果您对文件路径使用独特的格式,则可以避免覆盖以前保存的模型。 source
What is an ideal way to load the saved files and weights and resume training from there?
例子:
# Define model
model = keras.models.Sequential()
model.add(L.InputLayer([None],dtype='int32'))
model.add(L.Embedding(len(all_words),50))
model.add(keras.layers.Bidirectional(L.SimpleRNN(5,return_sequences=True)))
# Define softmax layer for every time step (hence TimeDistributed layer)
stepwise_dense = L.Dense(len(all_words),activation='softmax')
stepwise_dense = L.TimeDistributed(stepwise_dense)
model.add(stepwise_dense)
import keras.backend as K
# compile model with adam optimizer
model.compile('adam','categorical_crossentropy')
# print learning rate
print(f"Model learning rate is: {K.get_value(model.optimizer.lr):.3f}")
# train model
model.fit_generator(generate_batches(train_data), len(train_data)/BATCH_SIZE,
callbacks=[EvaluateAccuracy()], epochs=1)
# save model (weights, architecture, optimizer state)
model.save('my_model.h5')
# delete existing model
del model
结果
Model learning rate is: 0.001 Epoch 1/1 1341/1343 [============================>.] - ETA: 0s - loss: 0.4288 Measuring validation accuracy... Validation accuracy: 0.93138
from keras.models import load_model
# create new adam optimizer with le-04 learning rate (previous: 1e-03)
adam = keras.optimizers.Adam(lr=1e-4)
# load model
model = load_model('my_model.h5', compile=False)
# compile model and print new learning rate
model.compile(adam, 'categorical_crossentropy')
print(f"Model learning rate is: {K.get_value(model.optimizer.lr):.4f}")
# train model for 3 more epochs with new learning rate
print("Training model: ")
model.fit_generator(generate_batches(train_data),len(train_data)/BATCH_SIZE,
callbacks=[EvaluateAccuracy()], epochs=3,)
结果:
Model learning rate is: 0.0001 Training model: Epoch 1/3 1342/1343 [============================>.] - ETA: 0s - loss: 0.0885 Measuring validation accuracy... Validation accuracy: 0.93568 1344/1343 [==============================] - 41s - loss: 0.0885 Epoch 2/3 1342/1343 [============================>.] - ETA: 0s - loss: 0.0768 Measuring validation accuracy... Validation accuracy: 0.93925 1344/1343 [==============================] - 39s - loss: 0.0768 Epoch 3/3 1343/1343 [============================>.] - ETA: 0s - loss: 0.0701 Measuring validation accuracy... Validation accuracy: 0.94180
更多信息请访问 Keras FAQ针对具体情况。
关于python - 从 Keras 检查点加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61045806/