TensorFlow回调函数:tf.keras.callbacks.LambdaCallback
2019-03-27 15:18 更新
tf.keras.callbacks.LambdaCallback函数
类LambdaCallback
继承自: Callback
定义在:tensorflow/python/keras/callbacks.py。
用于动态创建简单的自定义回调的回调。
此回调是使用将在适当时间调用的匿名函数构造的。请注意,回调需要位置参数,如下所示:
- on_epoch_begin和on_epoch_end要求两个位置参数: epoch,logs
- on_batch_begin和on_batch_end要求两个位置参数: batch,logs
- on_train_begin并on_train_end要求一个位置参数: logs
参数:
- on_epoch_begin:在每个epoch开始时调用。
- on_epoch_end:在每个epoch结束时调用。
- on_batch_begin:在每个批处理开始时调用。
- on_batch_end:在每个批处理结束时调用。
- on_train_begin:在模型训练开始时调用。
- on_train_end:在模型训练结束时调用。
示例:
# 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])
__init__
__init__(
on_epoch_begin=None,
on_epoch_end=None,
on_batch_begin=None,
on_batch_end=None,
on_train_begin=None,
on_train_end=None,
**kwargs
)
初始化自我。
方法
on_batch_begin
on_batch_begin(
batch,
logs=None
)
on_batch_end
on_batch_end(
batch,
logs=None
)
on_epoch_begin
on_epoch_begin(
epoch,
logs=None
)
on_epoch_end
on_epoch_end(
epoch,
logs=None
)
on_train_batch_begin
on_train_batch_begin(
batch,
logs=None
)
on_train_batch_end
on_train_batch_end(
batch,
logs=None
)
on_train_begin
on_train_begin(logs=None)
on_train_end
on_train_end(logs=None)
set_model
set_model(model)
set_params
set_params(params)
以上内容是否对您有帮助:
更多建议: