AttributeError: 'SummaryWriter' object has no attribute 'add_hparams'
Author: zhuqunxi
Pull request: no
Labels: name: Need more information

# 🐛 Bug

## Information

Traceback (most recent call last):
  File "F:/Kaggle/Hug/Colab/main.py", line 105, in <module>
    trainer.train()
  File "c:\programdata\anaconda3\lib\site-packages\transformers\trainer.py", line 359, in train
    self.tb_writer.add_hparams(self.args.to_sanitized_dict(), metric_dict={})
AttributeError: 'SummaryWriter' object has no attribute 'add_hparams'


Comments:
Please fill out the template. It is there for a reason. It isn't even clear whether you use your own scripts or ours. _Fill out the template._

See this question, which might help: https://github.com/lanpa/tensorboardX/issues/502

hello when i use 
`python run_language_modeling.py \
    --output_dir=chinese_finetuned_lm \
    --model_type=bert \
    --model_name_or_path=bert-base-chinese \
    --do_train \
    --train_data_file=$TRAIN_FILE \
    --do_eval \
    --eval_data_file=$TEST_FILE \
    --mlm
`
 i find the same error
`Traceback (most recent call last):
  File "run_language_modeling.py", line 281, in <module>
    main()
  File "run_language_modeling.py", line 245, in main
    trainer.train(model_path=model_path)
  File "/home/zhongqi/anaconda3/envs/transformers_bert/lib/python3.6/site-packages/transformers/trainer.py", line 418, in train
    self.tb_writer.add_hparams(self.args.to_sanitized_dict(), metric_dict={})
AttributeError: 'SummaryWriter' object has no attribute 'add_hparams'`
how to deal with it? and my protobuf is 3.12.1

@Mozen Can you update to the latest transformers? Many things have changed - we now use a custom trainer class for the example scripts. Let me know whether that helps!

@BramVanroy  i used is already the latest version, and my torch version is 1.1.0， Related to this?

> hello when i use
> `python run_language_modeling.py \ --output_dir=chinese_finetuned_lm \ --model_type=bert \ --model_name_or_path=bert-base-chinese \ --do_train \ --train_data_file=$TRAIN_FILE \ --do_eval \ --eval_data_file=$TEST_FILE \ --mlm `
> i find the same error
> `Traceback (most recent call last): File "run_language_modeling.py", line 281, in <module> main() File "run_language_modeling.py", line 245, in main trainer.train(model_path=model_path) File "/home/zhongqi/anaconda3/envs/transformers_bert/lib/python3.6/site-packages/transformers/trainer.py", line 418, in train self.tb_writer.add_hparams(self.args.to_sanitized_dict(), metric_dict={}) AttributeError: 'SummaryWriter' object has no attribute 'add_hparams'`
> how to deal with it? and my protobuf is 3.12.1

I found  'add_hparams'` only exsiting in torch >1.3.1,  so I update the version of torch, the problem is solved!  Moreover, when torch >1.3.1, you should update the version of cuda, at least >= cuda 9.2.

> @BramVanroy i used is already the latest version, and my torch version is 1.1.0， Related to this?

should update torch >1.3.1

@zhuqunxi OK thanks

> @Mozen Can you update to the latest transformers? Many things have changed - we now use a custom trainer class for the example scripts. Let me know whether that helps!

Thanks for helping me. I have fixed this problem by myself.

If you had followed the template, and posted all the requested information such as your environment, this would have been solved much more quickly.

> If you had followed the template, and posted all the requested information such as your environment, this would have been solved much more quickly.

Awesome，thanks for your advice. I really need to learn how to ask questions.

> > @BramVanroy i used is already the latest version, and my torch version is 1.1.0， Related to this?
> 
> should update torch >1.3.1

Upgrading `torch` should not be the ideal solution. The issue arises because of differences in `SummaryWriter` from `torch.utils.tensorboard` and `tensorboardX` in [transformers/trainer.py](https://github.com/huggingface/transformers/blob/master/src/transformers/trainer.py#L46). With following environment:
```
protobuf                 3.12.1
tensorboard              2.2.1
tensorboard-plugin-wit   1.6.0.post3
tensorboardX             2.0+022f060
torch                    1.1.0
transformers             2.10.0
```
it is easy to see:
```
>>> from tensorboardX import SummaryWriter as SummaryWriter_tbX
>>> from torch.utils.tensorboard import SummaryWriter
>>>
>>> writer = SummaryWriter_tbX()
>>> writer.add_hparams({'lr': 1e-5, 'bsize': 20, 'n_hidden': 100}, {'accuracy': 0, 'loss': 0})
>>>
>>> writer = SummaryWriter()
>>> writer.add_hparams({'lr': 1e-5, 'bsize': 20, 'n_hidden': 100}, {'accuracy': 0, 'loss': 0})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'SummaryWriter' object has no attribute 'add_hparams'
```
(minimal code from: https://github.com/lanpa/tensorboardX/issues/502#issue-486036833)
Also, passing `tb_writer=None` explicitly to `Trainer` does not ignore using the tensorboard because of [this](https://github.com/huggingface/transformers/blob/master/src/transformers/trainer.py#L202). I think it might be more convenient if user is allowed the option to use/ignore tensorboard and further, `tensorboardX` should probably be first in `try:except` block when importing the `SummaryWriter` as it is easier to upgrade it than `torch` (@BramVanroy ).

@suamin The `Trainer` requires torch 1.3.1+, we'll make sure to mention this in the README.

Hello, I got this error even if I have a torch version = 1.5.1, I don't know why

I0717 09:08:45.343556 139953119131392 trainer.py:208] You are instantiating a Trainer but W&B is not installed. To use wandb logging, run `pip install wandb; wandb login` see https://docs.wandb.com/huggingface.
Traceback (most recent call last):
  File "run_ner.py", line 304, in <module>
    main()
  File "run_ner.py", line 229, in main
    model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path) else None
  File "/opt/conda/lib/python3.7/site-packages/transformers/trainer.py", line 429, in train
    self.tb_writer.add_hparams(self.args.to_sanitized_dict(), metric_dict={})
AttributeError: 'SummaryWriter' object has no attribute 'add_hparams'

see the source code: trainer.py
you will find that using "from torch.utils.tensorboard import SummaryWriter" first, if not in current torch, then use "from tensorboardX import SummaryWriter".
So, U need check your pytorch version. my torch 1.2.0, has "torch.utils.tensorboard.SummaryWriter", but it didn't has add_hparams. So you should update your pytorch.
Also, U can change "trainer.py" source code, force import SummaryWriter from tensorboardX



I fixed the error, thank you !