# How to add graphs to hparams in tensorboard?

**URL:** <https://discuss.pytorch.org/t/how-to-add-graphs-to-hparams-in-tensorboard/109349>\
**Category:** tensorboard\
**Created:** [January 19, 2021, 4:15pm UTC](https://discuss.pytorch.org/t/how-to-add-graphs-to-hparams-in-tensorboard/109349 "2021-01-19T16:15:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aledjackson](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/aledjackson/32/25226_2.png) [@aledjackson](https://discuss.pytorch.org/u/aledjackson)\
**Post date:** [January 19, 2021, 4:15pm UTC](https://discuss.pytorch.org/t/how-to-add-graphs-to-hparams-in-tensorboard/109349/1 "2021-01-19T16:15:09Z")

</div>

I’m trying to use tensorboard with pytorch and cannot find any clear documentation as to how to add graphs to the hparams dashboard. The image below shows what I want

 ![image](https://discuss.pytorch.org/uploads/default/original/3X/5/0/50c5b2b9d0c6848f41600e3632f51e87ef78f9d5.png)  
however I can only add singular values for the metrics:  
 ![image](https://discuss.pytorch.org/uploads/default/original/3X/b/f/bf80a10adee2a65c843386938f9d7f808e53397c.png)  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![aledjackson](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/aledjackson/32/25226_2.png) [@aledjackson](https://discuss.pytorch.org/u/aledjackson)\
**Post date:** [January 19, 2021, 6:40pm UTC](https://discuss.pytorch.org/t/how-to-add-graphs-to-hparams-in-tensorboard/109349/2 "2021-01-19T18:40:53Z")

</div>

Having dug a little deeper into `torch.utils.tensorboard`, I can’t seem to find how to use this functionality. However I can achieve it by modifying the `SummaryWriter.add_hparams()` function as such:

```python
			def add_hparams(self, hparam_dict, metric_dict, hparam_domain_discrete=None, run_name=None):
				torch._C._log_api_usage_once("tensorboard.logging.add_hparams")
				if type(hparam_dict) is not dict or type(metric_dict) is not dict:
					raise TypeError('hparam_dict and metric_dict should be dictionary.')
				exp, ssi, sei = hparams(hparam_dict, metric_dict, hparam_domain_discrete)

				self.file_writer.add_summary(exp)
				self.file_writer.add_summary(ssi)
				self.file_writer.add_summary(sei)
				for k, v in metric_dict.items():
					if v is not None:
						self.add_scalar(k, v)

```

To use this, when you create the `SummaryWriter` you should set `log_dir=log_dir+'/[run_name]'`. I have some example code and pictures to show how to use this below.

```python3
s = SummaryWriter(log_dir=f'{log_dir}/run1')
h_params = {'This': 1, 'is': 2, 'a': 3, 'test': 4}
metrics = {'accuracy/accuracy': None, 'loss/loss': None}

accuracy_values = [(1, 0.6), (2, 0.8), (3, 0.9), (4, 0.95)]
loss_values = [(1, 3), (2, 1), (3, 0.5), (4, 0.11)]
for step, v in accuracy_values:
	s.add_scalar('accuracy/accuracy', v, step)
for step, v in loss_values:
	s.add_scalar('loss/loss', v, step)

s.add_hparams(h_params, metrics)
s.close()

```

 ![image](https://discuss.pytorch.org/uploads/default/original/3X/c/2/c20cbf2a1f0de0ce67df806e608f128bdec188fa.png)

Something that I’m not sure if it’s a bug or not is that in the original code, any scalars are logged to the log\_dir whilst hyper parameters are logged in their own directory and given a run\_name.

(I’ve just posted this as hopefully this is useful to someone)

---

<div class="post-metadata">

**Author:** ![rajathsalegame](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rajathsalegame/32/35579_2.png) [@rajathsalegame](https://discuss.pytorch.org/u/rajathsalegame)\
**Post date:** [March 9, 2021, 10:25am UTC](https://discuss.pytorch.org/t/how-to-add-graphs-to-hparams-in-tensorboard/109349/3 "2021-03-09T10:25:25Z")

</div>

Thanks, this is a massive lifesaver. I think there would definitely be a benefit to rewriting the PyTorch Tensorboard interface to make it more intuitive and increasing the detail of documentation. It shouldn’t be that hard to be able to log metrics on a per-epoch basis with hyperparameters.

“Something that I’m not sure if it’s a bug or not is that in the original code, any scalars are logged to the log\_dir whilst hyper parameters are logged in their own directory and given a run\_name.” As for this part, this also seems to be a weird default behavior of the add\_hyperparameters option to use the log\_dir along with the current timestamp (time.time()). See [pytorch/writer.py at bf88a4dad5b3c06f43a9ce07a51ec0ab0dbfe2d3 · pytorch/pytorch · GitHub](https://github.com/pytorch/pytorch/blob/bf88a4dad5b3c06f43a9ce07a51ec0ab0dbfe2d3/torch/utils/tensorboard/writer.py#L305-L307). A quick fix is to just delete those lines and just use the original log\_dir.

---

<div class="post-metadata">

**Author:** ![kyeongjun1007](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kyeongjun1007/32/53615_2.png) [@kyeongjun1007](https://discuss.pytorch.org/u/kyeongjun1007)\
**Post date:** [March 5, 2023, 7:47am UTC](https://discuss.pytorch.org/t/how-to-add-graphs-to-hparams-in-tensorboard/109349/4 "2023-03-05T07:47:27Z")

</div>

How can I plot multiple graph in one plot?

I tried these below.

```auto
    def add_hparams(self, param_dict, metrics_dict, step=None):
        exp, ssi, sei = hparams(param_dict, metrics_dict)

        self.writer.file_writer.add_summary(exp)
        self.writer.file_writer.add_summary(ssi)
        self.writer.file_writer.add_summary(sei)

        if step is not None:
            for k, v in metrics_dict.item() :
                self.writer.add_scalar(k, v, step)

```

but if I set the metrics\_dict = {‘train\_loss’ : train\_loss, ‘valid\_loss’ : valid\_loss}, than there are two plot in board.

```auto
    def add_hparams(self, param_dict, metrics_dict, step=None):
        exp, ssi, sei = hparams(param_dict, metrics_dict)

        self.writer.file_writer.add_summary(exp)
        self.writer.file_writer.add_summary(ssi)
        self.writer.file_writer.add_summary(sei)

        if step is not None:
            self.writer.add_scalars('loss', metrics_dict, step)

```

and if I set the metrics\_dict = {‘loss’ : {‘train\_loss’ : train\_loss, 'valid\_loss : valid\_loss}} than It not works… (board can show valid loss only)
