關於 keras.callbacks設定模型儲存策略
阿新 • • 發佈:2018-12-21
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.
確實很便利....