|TensorFlow 1 version||View source on GitHub|
Enable visualizations for 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 )
Used in the notebooks
|Used in the guide||Used in the tutorials|
TensorBoard is a visualization tool provided with TensorFlow.
This callback logs events for TensorBoard, including:
- Metrics summary plots
- Training graph visualization
- Activation histograms
- Sampled profiling
When used in
Model.evaluate, in addition to epoch summaries, there will be
a summary that records evaluation metrics vs
written. The metric names will be prepended with
Model.optimizer.iterations being the step in the visualized TensorBoard.
If you have installed TensorFlow with pip, you should be able to launch TensorBoard from the command line:
You can find more information about TensorBoard here.
||the path of the directory where to save the log files to be parsed by TensorBoard. e.g. log_dir = os.path.join(working_dir, 'logs') This directory should not be reused by any other callbacks.|
||frequency (in epochs) at which to compute activation and weight histograms for the layers of the model. If set to 0, histograms won't be computed. Validation data (or split) must be specified for histogram visualizations.|
||whether to visualize the graph in TensorBoard. The log file can become quite large when write_graph is set to True.|
||whether to write model weights to visualize as image in TensorBoard.|
||whether to log the training steps per second into Tensorboard. This supports both epoch and batch frequency logging.|
||Profile the batch(es) to sample compute characteristics. profile_batch must be a non-negative integer or a tuple of integers. A pair of positive integers signify a range of batches to profile. By default, profiling is disabled.|
||frequency (in epochs) at which embedding layers will be visualized. If set to 0, embeddings won't be visualized.|
||Dictionary which maps embedding layer names to the filename of a file in which to save metadata for the embedding layer. In case the same metadata file is to be used for all embedding layers, a single filename can be passed.|
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.
Custom batch-level summaries in a subclassed Model:
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` 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])
Custom batch-level summaries in a Functional API Model:
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` 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])