TensorBoard
类tf_keras.callbacks.TensorBoard(
log_dir="logs",
histogram_freq=0,
write_graph=True,
write_images=False,
write_steps_per_second=False,
update_freq="epoch",
profile_batch=0,
embeddings_freq=0,
embeddings_metadata=None,
**kwargs
)
启用 TensorBoard 的可视化。
TensorBoard 是 TensorFlow 提供的可视化工具。
此回调记录 TensorBoard 的事件,包括
在 Model.evaluate
或常规验证(on_test_end)中使用时,除了 epoch 摘要外,还会有一个摘要记录评估指标与 Model.optimizer.iterations
的对比。指标名称将以 evaluation
为前缀,Model.optimizer.iterations
是可视化 TensorBoard 中的步数。
如果您已使用 pip 安装 TensorFlow,则应该可以从命令行启动 TensorBoard
tensorboard --logdir=path_to_your_logs
您可以在此处找到有关 TensorBoard 的更多信息。
参数
'batch'
或 'epoch'
或整数。当使用 'epoch'
时,会在每个 epoch 后将损失和指标写入 TensorBoard。如果使用整数,例如 1000
,则所有指标和损失(包括 Model.compile
添加的自定义指标和损失)将每 1000 个批次记录到 TensorBoard。'batch'
是 1
的同义词,表示它们将写入每个批次。但请注意,过于频繁地写入 TensorBoard 会减慢您的训练速度,尤其是在与 tf.distribute.Strategy
一起使用时,因为它会产生额外的同步开销。不支持与 ParameterServerStrategy
一起使用。批次级摘要写入也可通过 train_step
覆盖获得。有关更多详细信息,请参阅 TensorBoard 标量教程 # noqa: E501。示例
基本用法
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir="./logs")
model.fit(x_train, y_train, epochs=2, callbacks=[tensorboard_callback])
# Then run the tensorboard command to view the visualizations.
子类模型中的自定义批次级摘要
class MyModel(tf.keras.Model):
def build(self, _):
self.dense = tf.keras.layers.Dense(10)
def call(self, x):
outputs = self.dense(x)
tf.summary.histogram('outputs', outputs)
return outputs
model = MyModel()
model.compile('sgd', 'mse')
# Make sure to set `update_freq=N` to log a batch-level summary every N
# batches. In addition to any [`tf.summary`](https://tensorflowcn.cn/api_docs/python/tf/summary) contained in `Model.call`,
# metrics added in `Model.compile` will be logged every N batches.
tb_callback = tf.keras.callbacks.TensorBoard('./logs', update_freq=1)
model.fit(x_train, y_train, callbacks=[tb_callback])
函数式 API 模型中的自定义批次级摘要
def my_summary(x):
tf.summary.histogram('x', x)
return x
inputs = tf.keras.Input(10)
x = tf.keras.layers.Dense(10)(inputs)
outputs = tf.keras.layers.Lambda(my_summary)(x)
model = tf.keras.Model(inputs, outputs)
model.compile('sgd', 'mse')
# Make sure to set `update_freq=N` to log a batch-level summary every N
# batches. In addition to any [`tf.summary`](https://tensorflowcn.cn/api_docs/python/tf/summary) contained in `Model.call`,
# metrics added in `Model.compile` will be logged every N batches.
tb_callback = tf.keras.callbacks.TensorBoard('./logs', update_freq=1)
model.fit(x_train, y_train, callbacks=[tb_callback])
分析
# Profile a single batch, e.g. the 5th batch.
tensorboard_callback = tf.keras.callbacks.TensorBoard(
log_dir='./logs', profile_batch=5)
model.fit(x_train, y_train, epochs=2, callbacks=[tensorboard_callback])
# Profile a range of batches, e.g. from 10 to 20.
tensorboard_callback = tf.keras.callbacks.TensorBoard(
log_dir='./logs', profile_batch=(10,20))
model.fit(x_train, y_train, epochs=2, callbacks=[tensorboard_callback])