AttributeError: 'Tensor' object has no attribute 'items'

I want to log the loss of the train using the tensorboard in pytorch. I got an error there.

AttributeError: 'Tensor' object has no attribute 'items'

I want to solve this error and check the log using tensorboard. Here I show my code.

l_mse = mseloss(img,decoder_out)
writer.add_scalars("MSE",l_mse,n_iter)

then I have error blow.

Traceback (most recent call last):
  File "main.py", line 39, in <module>
    main()
  File "main.py", line 22, in main
    solover.train(dataloader)
  File "path to my file", line 239, in train
    writer.add_scalars("MSE",l_mse,n_iter)
  File "/~~/anaconda3/lib/python3.7/site-packages/torch/utils/tensorboard/writer.py", line 378, in add_scalars
    for tag, scalar_value in tag_scalar_dict.items():
AttributeError: 'Tensor' object has no attribute 'items'

I tried

writer.add_scalars("MSE",l_mse,n_iter).eval()
writer.add_scalars("MSE",l_mse.item(),n_iter)
writer.add_scalars("MSE",l_mse.detach().cpu().numpy(),n_iter)

but still not work well.
tensorboard:1.15.0
tenosrboard-plugin-wit 1.6.0.post3
pytorch:1.5.0

Iā€™m not deeply familiar with the usage of Tensorboard, but it seems that a dict is expected, which has the items() operation.
Could you wrap your data into a dict and retry the call?

There is add_scalar (singular, so no s at the end) that would seem to work roughly like you want (except for the .eval() in there).
You are calling add_scalars (plural) which takes name/value pairs in form of a dict if you want to add several.

Best regards

Thomas

1 Like