当我开始训练模型时,之前没有保存模型.我可以model.compile()安全使用.我现在已将模型保存在h5文件中以供进一步培训使用checkpoint.
说,我想进一步训练模型.我在这一点上很困惑:我可以model.compile()在这里使用吗?它应该在model = load_model()声明之前还是之后放置?如果model.compile()重新初始化所有权重和偏见,我应该在model = load_model()陈述之前放置它.
在发现一些讨论后,在我看来model.compile()只有在我之前没有保存模型时才需要.保存模型后,无需使用model.compile().这是真是假?当我想预测使用训练模型时,我应该model.compile()在预测之前使用吗?
是否可以model.loss在回调中进行设置而无需model.compile(...)在之后进行重新编译(因为自此优化器状态被重置),而只是重新编译model.loss,例如:
class NewCallback(Callback):
def __init__(self):
super(NewCallback,self).__init__()
def on_epoch_end(self, epoch, logs={}):
self.model.loss=[loss_wrapper(t_change, current_epoch=epoch)]
self.model.compile_only_loss() # is there a version or hack of
# model.compile(...) like this?
Run Code Online (Sandbox Code Playgroud)
要使用关于stackoverflow的先前示例进行更多扩展:
要实现取决于历元数的损失函数,例如(如在这个stackoverflow问题中):
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
Run Code Online (Sandbox Code Playgroud)
其中“ current_epoch”是使用回调更新的Keras变量:
current_epoch = K.variable(0.)
model.compile(optimizer=opt, loss=loss_wrapper(5, current_epoch),
metrics=...)
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) …Run Code Online (Sandbox Code Playgroud)