xing lee
xing lee

Reputation: 51

TensorboardX input problem about add_scalar()

When I use the tensorboardX to plot my data loss, it show me that:

AssertionError                            Traceback (most recent call last)
<ipython-input-76-73419a51fcc9> in <module>
----> 1 writer.add_scalar('resnet34_loss', loss)

F:\Program Files\Python\lib\site-packages\tensorboardX\writer.py in add_scalar(self, tag, scalar_value, global_step, walltime)
    403             scalar_value = workspace.FetchBlob(scalar_value)
    404         self._get_file_writer().add_summary(
--> 405             scalar(tag, scalar_value), global_step, walltime)
    406 
    407     def add_scalars(self, main_tag, tag_scalar_dict, global_step=None, walltime=None):

F:\Program Files\Python\lib\site-packages\tensorboardX\summary.py in scalar(name, scalar, collections)
    145     name = _clean_tag(name)
    146     scalar = make_np(scalar)
--> 147     assert(scalar.squeeze().ndim == 0), 'scalar should be 0D'
    148     scalar = float(scalar)
    149     return Summary(value=[Summary.Value(tag=name, simple_value=scalar)])

AssertionError: scalar should be 0D

I have turn the loss from float into np.array, and I have read the doc of tensorboardX, it tell me that add_scalar() function must input the scalar data and I do it, but it shows me a bug. Thanks for your help!

Upvotes: 2

Views: 2799

Answers (1)

Melike
Melike

Reputation: 468

I had the same issue, here is a minimal sample to reproduce your error,

writer = SummaryWriter(osp.join('runs', 'hello'))
loss = np.random.randn(10)
writer.add_scalar(tag='Checking range', scalar_value=loss)
writer.close()

This returns,

Traceback (most recent call last):

  File "untitled0.py", line 26, in <module>
    writer.add_scalar(tag='Checking range', scalar_value=loss)

  File "/home/melike/anaconda2/envs/pooling/lib/python3.6/site-packages/torch/utils/tensorboard/writer.py", line 346, in add_scalar
    scalar(tag, scalar_value), global_step, walltime)

  File "/home/melike/anaconda2/envs/pooling/lib/python3.6/site-packages/torch/utils/tensorboard/summary.py", line 248, in scalar
    assert(scalar.squeeze().ndim == 0), 'scalar should be 0D'

AssertionError: scalar should be 0D

As indicated by the assertion error, scalar.squeeze().ndim should have 0-dimension. Let's check our scalar_value which is loss,

print(loss.squeeze().ndim)

This outputs

1

So, we found the reason of error, add_scalar expects 0-d scalar after squeeze operation and we gave it a 1-d scalar. Tensorboard page of PyTorch docs has add_scalar examples. Let's convert our code to that version.

writer = SummaryWriter(osp.join('runs', 'hello'))
loss = np.random.randn(10)
for i, val in enumerate(loss):
    writer.add_scalar(tag='Checking range', scalar_value=val, global_step=i)
writer.close()

And this is the output,

Loss Plot

Upvotes: 2

Related Questions