tensorboardX

A module for visualization with tensorboard

class tensorboardX.SummaryWriter(log_dir=None, comment='', **kwargs)[source]

Writes Summary directly to event files. The SummaryWriter class provides a high-level api to create an event file in a given directory and add summaries and events to it. The class updates the file contents asynchronously. This allows a training program to call methods to add data to the file directly from the training loop, without slowing down training.

__init__(log_dir=None, comment='', **kwargs)[source]
Parameters:
  • log_dir (string) – save location, default is: runs/CURRENT_DATETIME_HOSTNAME, which changes after each run. Use hierarchical folder structure to compare between runs easily. e.g. ‘runs/exp1’, ‘runs/exp2’
  • comment (string) – comment that appends to the default log_dir. If log_dir is assigned, this argument will no effect.
  • purge_step (int) – When logging crashes at step \(T+X\) and restarts at step \(T\), any events whose global_step larger or equal to \(T\) will be purged and hidden from TensorBoard. Note that the resumed experiment and crashed experiment should have the same log_dir.
  • filename_suffix (string) – Every event file’s name is suffixed with suffix. example: SummaryWriter(filename_suffix='.123')
  • kwargs – extra keyword arguments for FileWriter (e.g. ‘flush_secs’ controls how often to flush pending events). For more arguments please refer to docs for ‘tf.summary.FileWriter’.
add_audio(tag, snd_tensor, global_step=None, sample_rate=44100, walltime=None)[source]

Add audio data to summary.

Parameters:
  • tag (string) – Data identifier
  • snd_tensor (torch.Tensor) – Sound data
  • global_step (int) – Global step value to record
  • sample_rate (int) – sample rate in Hz
  • walltime (float) – Optional override default walltime (time.time()) of event
Shape:
snd_tensor: \((1, L)\). The values should lie between [-1, 1].
add_custom_scalars(layout)[source]

Create special chart by collecting charts tags in ‘scalars’. Note that this function can only be called once for each SummaryWriter() object. Because it only provides metadata to tensorboard, the function can be called before or after the training loop. See examples/demo_custom_scalars.py for more.

Parameters:layout (dict) – {categoryName: charts}, where charts is also a dictionary {chartName: ListOfProperties}. The first element in ListOfProperties is the chart’s type (one of Multiline or Margin) and the second element should be a list containing the tags you have used in add_scalar function, which will be collected into the new chart.

Examples:

layout = {'Taiwan':{'twse':['Multiline',['twse/0050', 'twse/2330']]},
             'USA':{ 'dow':['Margin',   ['dow/aaa', 'dow/bbb', 'dow/ccc']],
                  'nasdaq':['Margin',   ['nasdaq/aaa', 'nasdaq/bbb', 'nasdaq/ccc']]}}

writer.add_custom_scalars(layout)
add_custom_scalars_marginchart(tags, category='default', title='untitled')[source]

Shorthand for creating marginchart. Similar to add_custom_scalars(), but the only necessary argument is tags, which should have exactly 3 elements.

Parameters:tags (list) – list of tags that have been used in add_scalar()

Examples:

writer.add_custom_scalars_marginchart(['twse/0050', 'twse/2330', 'twse/2006'])
add_custom_scalars_multilinechart(tags, category='default', title='untitled')[source]

Shorthand for creating multilinechart. Similar to add_custom_scalars(), but the only necessary argument is tags.

Parameters:tags (list) – list of tags that have been used in add_scalar()

Examples:

writer.add_custom_scalars_multilinechart(['twse/0050', 'twse/2330'])
add_embedding(mat, metadata=None, label_img=None, global_step=None, tag='default', metadata_header=None)[source]

Add embedding projector data to summary.

Parameters:
  • mat (torch.Tensor or numpy.array) – A matrix which each row is the feature vector of the data point
  • metadata (list) – A list of labels, each element will be convert to string
  • label_img (torch.Tensor) – Images correspond to each data point
  • global_step (int) – Global step value to record
  • tag (string) – Name for the embedding
Shape:

mat: \((N, D)\), where N is number of data and D is feature dimension

label_img: \((N, C, H, W)\)

Examples:

import keyword
import torch
meta = []
while len(meta)<100:
    meta = meta+keyword.kwlist # get some strings
meta = meta[:100]

for i, v in enumerate(meta):
    meta[i] = v+str(i)

label_img = torch.rand(100, 3, 10, 32)
for i in range(100):
    label_img[i]*=i/100.0

writer.add_embedding(torch.randn(100, 5), metadata=meta, label_img=label_img)
writer.add_embedding(torch.randn(100, 5), label_img=label_img)
writer.add_embedding(torch.randn(100, 5), metadata=meta)
add_figure(tag, figure, global_step=None, close=True, walltime=None)[source]

Render matplotlib figure into an image and add it to summary.

Note that this requires the matplotlib package.

Parameters:
  • tag (string) – Data identifier
  • figure (matplotlib.pyplot.figure) – figure or a list of figures
  • global_step (int) – Global step value to record
  • close (bool) – Flag to automatically close the figure
  • walltime (float) – Optional override default walltime (time.time()) of event
add_graph(model, input_to_model=None, verbose=False, **kwargs)[source]

Add graph data to summary.

Parameters:
  • model (torch.nn.Module) – model to draw.
  • input_to_model (torch.autograd.Variable) – a variable or a tuple of variables to be fed.
add_histogram(tag, values, global_step=None, bins='tensorflow', walltime=None)[source]

Add histogram to summary.

Parameters:
add_image(tag, img_tensor, global_step=None, walltime=None)[source]

Add image data to summary.

Note that this requires the pillow package.

Parameters:
  • tag (string) – Data identifier
  • img_tensor (torch.Tensor, numpy.array, or string/blobname) – Image data
  • global_step (int) – Global step value to record
  • walltime (float) – Optional override default walltime (time.time()) of event
Shape:
img_tensor: \((3, H, W)\). Use torchvision.utils.make_grid() to prepare it is a good idea.
add_image_with_boxes(tag, img_tensor, box_tensor, global_step=None, walltime=None, **kwargs)[source]

Add image boxes data to summary (useful for models such as Detectron).

Parameters:
  • tag (string) – Data identifier
  • img_tensor (torch.Tensor, numpy.array, or string/blobname) – Image data
  • box_tensor (torch.Tensor, numpy.array, or string/blobname) – Box data (for detected objects)
  • global_step (int) – Global step value to record
  • walltime (float) – Optional override default walltime (time.time()) of event
add_pr_curve(tag, labels, predictions, global_step=None, num_thresholds=127, weights=None, walltime=None)[source]

Adds precision recall curve.

Parameters:
  • tag (string) – Data identifier
  • labels (torch.Tensor, numpy.array, or string/blobname) – Ground truth data. Binary label for each element.
  • predictions (torch.Tensor, numpy.array, or string/blobname) –
  • probability that an element be classified as true. Value should in [0, 1] (The) –
  • global_step (int) – Global step value to record
  • num_thresholds (int) – Number of thresholds used to draw the curve.
  • walltime (float) – Optional override default walltime (time.time()) of event
add_pr_curve_raw(tag, true_positive_counts, false_positive_counts, true_negative_counts, false_negative_counts, precision, recall, global_step=None, num_thresholds=127, weights=None, walltime=None)[source]

Adds precision recall curve with raw data.

Parameters:
add_scalar(tag, scalar_value, global_step=None, walltime=None)[source]

Add scalar data to summary.

Parameters:
  • tag (string) – Data identifier
  • scalar_value (float or string/blobname) – Value to save
  • global_step (int) – Global step value to record
  • walltime (float) – Optional override default walltime (time.time()) of event
add_scalars(main_tag, tag_scalar_dict, global_step=None, walltime=None)[source]

Adds many scalar data to summary.

Note that this function also keeps logged scalars in memory. In extreme case it explodes your RAM.

Parameters:
  • main_tag (string) – The parent name for the tags
  • tag_scalar_dict (dict) – Key-value pair storing the tag and corresponding values
  • global_step (int) – Global step value to record
  • walltime (float) – Optional override default walltime (time.time()) of event

Examples:

writer.add_scalars('run_14h', {'xsinx':i*np.sin(i/r),
                               'xcosx':i*np.cos(i/r),
                               'arctanx': numsteps*np.arctan(i/r)}, i)
# This call adds three values to the same scalar plot with the tag
# 'run_14h' in TensorBoard's scalar section.
add_text(tag, text_string, global_step=None, walltime=None)[source]

Add text data to summary.

Parameters:
  • tag (string) – Data identifier
  • text_string (string) – String to save
  • global_step (int) – Global step value to record
  • walltime (float) – Optional override default walltime (time.time()) of event

Examples:

writer.add_text('lstm', 'This is an lstm', 0)
writer.add_text('rnn', 'This is an rnn', 10)
add_video(tag, vid_tensor, global_step=None, fps=4, walltime=None)[source]

Add video data to summary.

Note that this requires the moviepy package.

Parameters:
  • tag (string) – Data identifier
  • vid_tensor (torch.Tensor) – Video data
  • global_step (int) – Global step value to record
  • fps (float or int) – Frames per second
  • walltime (float) – Optional override default walltime (time.time()) of event
Shape:
vid_tensor: \((B, C, T, H, W)\).
export_scalars_to_json(path)[source]

Exports to the given path an ASCII file containing all the scalars written so far by this instance, with the following format: {writer_id : [[timestamp, step, value], …], …}

The scalars saved by add_scalars() will be flushed after export.