TensorFlow回調(diào)函數(shù):tf.keras.callbacks.LambdaCallback

2019-03-27 15:18 更新

tf.keras.callbacks.LambdaCallback函數(shù)

LambdaCallback

繼承自: Callback

定義在:tensorflow/python/keras/callbacks.py。

用于動(dòng)態(tài)創(chuàng)建簡(jiǎn)單的自定義回調(diào)的回調(diào)。

此回調(diào)是使用將在適當(dāng)時(shí)間調(diào)用的匿名函數(shù)構(gòu)造的。請(qǐng)注意,回調(diào)需要位置參數(shù),如下所示:

  • on_epoch_begin和on_epoch_end要求兩個(gè)位置參數(shù): epoch,logs
  • on_batch_begin和on_batch_end要求兩個(gè)位置參數(shù): batch,logs
  • on_train_begin并on_train_end要求一個(gè)位置參數(shù): logs

參數(shù):

  • on_epoch_begin:在每個(gè)epoch開(kāi)始時(shí)調(diào)用。
  • on_epoch_end:在每個(gè)epoch結(jié)束時(shí)調(diào)用。
  • on_batch_begin:在每個(gè)批處理開(kāi)始時(shí)調(diào)用。
  • on_batch_end:在每個(gè)批處理結(jié)束時(shí)調(diào)用。
  • on_train_begin:在模型訓(xùn)練開(kāi)始時(shí)調(diào)用。
  • on_train_end:在模型訓(xùn)練結(jié)束時(shí)調(diào)用。

示例:

# 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)


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)