Skip to content

Commit

Permalink
Adds summary_description and display_name for add_scalar() call (#570)
Browse files Browse the repository at this point in the history
* fixes #565
  • Loading branch information
lanpa committed Apr 10, 2020
1 parent 47e1ad9 commit a31c121
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions examples/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
s1 = torch.rand(1) # value to keep
s2 = torch.rand(1)
# data grouping by `slash`
writer.add_scalar('data/scalar_systemtime', s1[0], n_iter)
writer.add_scalar('data/scalar_systemtime', s1[0], n_iter, summary_description="# markdown is supported!")
# data grouping by `slash`
writer.add_scalar('data/scalar_customtime', s1[0], n_iter, walltime=n_iter)
writer.add_scalar('data/scalar_customtime', s1[0], n_iter, walltime=n_iter, display_name="dudubird")
writer.add_scalars('data/scalar_group', {"xsinx": n_iter * np.sin(n_iter),
"xcosx": n_iter * np.cos(n_iter),
"arctanx": np.arctan(n_iter)}, n_iter)
Expand Down
12 changes: 8 additions & 4 deletions tensorboardX/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ def hparams(hparam_dict=None, metric_dict=None):
return exp, ssi, sei


def scalar(name, scalar, collections=None):
def scalar(name, scalar, display_name="", summary_description=""):
"""Outputs a `Summary` protocol buffer containing a single scalar value.
The generated Summary has a Tensor.proto containing the input Tensor.
Args:
name: A name for the generated node. Will also serve as the series name in
TensorBoard.
tensor: A real numeric Tensor containing a single value.
collections: Optional list of graph collections keys. The new summary op is
added to these collections. Defaults to `[GraphKeys.SUMMARIES]`.
display_name: The title of the plot. If empty string is passed, `name` will be used.
summary_description: The comprehensive text that will showed by clicking the information icon on TensorBoard.
Returns:
A scalar `Tensor` of type `string`. Which contains a `Summary` protobuf.
Raises:
Expand All @@ -144,7 +144,11 @@ def scalar(name, scalar, collections=None):
scalar = make_np(scalar)
assert(scalar.squeeze().ndim == 0), 'scalar should be 0D'
scalar = float(scalar)
return Summary(value=[Summary.Value(tag=name, simple_value=scalar)])
if display_name == "" and summary_description == "":
return Summary(value=[Summary.Value(tag=name, simple_value=scalar)])

metadata = SummaryMetadata(display_name=display_name, summary_description=summary_description)
return Summary(value=[Summary.Value(tag=name, simple_value=scalar, metadata=metadata)])


def histogram_raw(name, min, max, num, sum, sum_squares, bucket_limits, bucket_counts):
Expand Down
10 changes: 7 additions & 3 deletions tensorboardX/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,19 @@ def add_hparams(self, hparam_dict=None, metric_dict=None, name=None, global_step
for k, v in metric_dict.items():
w_hp.add_scalar(k, v, global_step)

def add_scalar(self, tag, scalar_value, global_step=None, walltime=None):
def add_scalar(self, tag, scalar_value, global_step=None, walltime=None,
display_name="", summary_description=""):
"""Add scalar data to summary.
Args:
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
display_name (string): The title of the plot. If empty string is passed,
`tag` will be used.
summary_description (string): The comprehensive text that will showed
by clicking the information icon on TensorBoard.
Examples::
from tensorboardX import SummaryWriter
Expand All @@ -408,7 +412,7 @@ def add_scalar(self, tag, scalar_value, global_step=None, walltime=None):
if self._check_caffe2_blob(scalar_value):
scalar_value = workspace.FetchBlob(scalar_value)
self._get_file_writer().add_summary(
scalar(tag, scalar_value), global_step, walltime)
scalar(tag, scalar_value, display_name, summary_description), global_step, walltime)

def add_scalars(self, main_tag, tag_scalar_dict, global_step=None, walltime=None):
"""Adds many scalar data to summary.
Expand Down

0 comments on commit a31c121

Please sign in to comment.