恢复具有不同损失功能的训练 [英] Resume training with different loss function
问题描述
我想实施两步学习过程,其中:
1)使用损失函数loss_1
对模型进行几个时期的预训练
2)将损失函数更改为loss_2
并继续进行微调训练
I want to implement a two-step learning process where:
1) pre-train a model for a few epochs using the loss function loss_1
2) change the loss function to loss_2
and continue the training for fine-tuning
目前,我的方法是:
model.compile(optimizer=opt, loss=loss_1, metrics=['accuracy'])
model.fit_generator(…)
model.compile(optimizer=opt, loss=loss_2, metrics=['accuracy’])
model.fit_generator(…)
请注意,优化器保持不变,仅损失函数发生变化.我想平稳地继续训练,但是损失功能有所不同.根据这篇文章,重新编译模型会失去优化器状态.问题:
Note that the optimizer remains the same, and only the loss function changes. I'd like to smoothly continue training, but with a different loss function. According to this post, re-compiling the model loses the optimizer state. Questions:
a)即使我使用相同优化器(例如Adam),我也会丢失优化器状态吗?
b)如果a)的答案是肯定的,那么关于如何在不重置优化器状态的情况下将损失函数更改为新函数的任何建议?
a) Will I lose the optimizer state even if I use the same optimizer, eg Adam?
b) if the answer to a) is yes, any suggestions on how to change the loss function to a new one without reseting the optimizer state?
根据Simon Caby的建议,并基于此线程,我创建了自定义损失函数具有两个取决于历元数的损失计算.但是,它对我不起作用.我的方法:
As suggested by Simon Caby and based on this thread, I created a custom loss function with two loss computations that depend on epoch number. However, it does not work for me. My approach:
def loss_wrapper(t_change, current_epoch):
def custom_loss(y_true, y_pred):
c_epoch = K.get_value(current_epoch)
if c_epoch < t_change:
# compute loss_1
else:
# compute loss_2
return custom_loss
初始化current_epoch
后,我将编译如下:
And I compile as follows, after initializing current_epoch
:
current_epoch = K.variable(0.)
model.compile(optimizer=opt, loss=loss_wrapper(5, current_epoch), metrics=...)
要更新current_epoch
,我创建一个回调:
To update the current_epoch
, I create a callback:
class NewCallback(Callback):
def __init__(self, current_epoch):
self.current_epoch = current_epoch
def on_epoch_end(self, epoch, logs={}):
K.set_value(self.current_epoch, epoch)
model.fit_generator(..., callbacks=[NewCallback(current_epoch)])
回调在每个时期正确更新self.current_epoch
.但是更新没有达到自定义丢失功能.相反,current_epoch
会永远保留初始化值,并且永远不会执行loss_2
.
The callback updates self.current_epoch
every epoch correctly. But the update does not reach the custom loss function. Instead, current_epoch
keeps the initialization value forever, and loss_2
is never executed.
欢迎任何建议,谢谢!
推荐答案
我的答案: a)是的,您可能应该制作自己的学习率调度程序以保持对它的控制:
My answers : a) yes, and you should probably make your own learning rate scheduler in order to keep control of it :
keras.callbacks.LearningRateScheduler(schedule, verbose=0)
b)是的,您可以创建自己的损失函数,包括在两种不同损失方法之间变动的函数.请参阅:高级Keras -构建复杂的自定义损失和指标" https://towardsdatascience.com/advanced-keras-构建复杂的自定义损失和指标c07ca130a618
b) yes you can create your own loss function, including one that flutuates between two different loss methods. see : "Advanced Keras — Constructing Complex Custom Losses and Metrics" https://towardsdatascience.com/advanced-keras-constructing-complex-custom-losses-and-metrics-c07ca130a618
这篇关于恢复具有不同损失功能的训练的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!