关于 keras.callbacks设置模型保存策略

keras.callbacks.ModelCheckpoint(self.checkpoint_path,
                                verbose=0, save_weights_only=True,mode="max",save_best_only=True),

默认是每一次poch,但是这样硬盘空间很快就会被耗光. 

将save_best_only 设置为True使其只保存最好的模型,值得一提的是其记录的acc是来自于一个monitor_op,其默认为"val_loss",其实现是取self.best为 -np.Inf.  所以,第一次的训练结果总是被保存.

mode模式自动为auto 和 max一样,还有一个min的选项...应该是loss没有负号的时候用的....

https://keras.io/callbacks/  浏览上面的文档.

# Print the batch number at the beginning of every batch.
batch_print_callback = LambdaCallback(
    on_batch_begin=lambda batch,logs: print(batch))

# Stream the epoch loss to a file in JSON format. The file content
# is not well-formed JSON but rather has a JSON object per line.
import json
json_log = open('loss_log.json', mode='wt', buffering=1)
json_logging_callback = LambdaCallback(
    on_epoch_end=lambda epoch, logs: json_log.write(
        json.dumps({'epoch': epoch, 'loss': logs['loss']}) + '\n'),
    on_train_end=lambda logs: json_log.close()
)

# Terminate some processes after having finished model training.
processes = ...
cleanup_callback = LambdaCallback(
    on_train_end=lambda logs: [
        p.terminate() for p in processes if p.is_alive()])

model.fit(...,
          callbacks=[batch_print_callback,
                     json_logging_callback,
                     cleanup_callback])

Keras的callback 一般在model.fit函数使用,由于Keras的便利性.有很多模型策略以及日志的策略.

比如 当loss不再变化时停止训练

keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto', baseline=None, restore_best_weights=False)

比如日志传送远程服务器等,以及自适应的学习率scheduler.

确实很便利....

 

你可能感兴趣的:(深度模型)