Pytorch-lightning: AttributeError: 'Tensor' object has no attribute 'items'

Created on 30 Mar 2020  Â·  5Comments  Â·  Source: PyTorchLightning/pytorch-lightning

Hi, I'm not sure what's going on. I tried to follow tutorial to organized my code into a LightningModule. Can anyone help?
During model.fit(), I got this error :

Epoch 1:   0%|                                                                                                                               | 0/12831 [00:00<?, ?it/s]Traceback (most recent call last):
  File "/home/allen_wu/miniconda3/envs/pytorch/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/allen_wu/miniconda3/envs/pytorch/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/allen_wu/.vscode-server-insiders/extensions/ms-python.python-2020.3.69010/pythonFiles/lib/python/debugpy/wheels/debugpy/__main__.py", line 45, in <module>
    cli.main()
  File "/home/allen_wu/.vscode-server-insiders/extensions/ms-python.python-2020.3.69010/pythonFiles/lib/python/debugpy/wheels/debugpy/../debugpy/server/cli.py", line 427, in main
    run()
  File "/home/allen_wu/.vscode-server-insiders/extensions/ms-python.python-2020.3.69010/pythonFiles/lib/python/debugpy/wheels/debugpy/../debugpy/server/cli.py", line 264, in run_file
    runpy.run_path(options.target, run_name="__main__")
  File "/home/allen_wu/miniconda3/envs/pytorch/lib/python3.7/runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/home/allen_wu/miniconda3/envs/pytorch/lib/python3.7/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/home/allen_wu/miniconda3/envs/pytorch/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/allen_wu/sota_lm_dev/codebase/gpt2/Gpt2SeqClassifier.py", line 169, in <module>
    trainer.fit(model)
  File "/home/allen_wu/miniconda3/envs/pytorch/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 630, in fit
    self.run_pretrain_routine(model)
  File "/home/allen_wu/miniconda3/envs/pytorch/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 830, in run_pretrain_routine
    self.train()
  File "/home/allen_wu/miniconda3/envs/pytorch/lib/python3.7/site-packages/pytorch_lightning/trainer/training_loop.py", line 343, in train
    self.run_training_epoch()
  File "/home/allen_wu/miniconda3/envs/pytorch/lib/python3.7/site-packages/pytorch_lightning/trainer/training_loop.py", line 413, in run_training_epoch
    output = self.run_training_batch(batch, batch_idx)
  File "/home/allen_wu/miniconda3/envs/pytorch/lib/python3.7/site-packages/pytorch_lightning/trainer/training_loop.py", line 562, in run_training_batch
    loss = optimizer_closure()
  File "/home/allen_wu/miniconda3/envs/pytorch/lib/python3.7/site-packages/pytorch_lightning/trainer/training_loop.py", line 529, in optimizer_closure
    split_batch, batch_idx, opt_idx, self.hiddens)
  File "/home/allen_wu/miniconda3/envs/pytorch/lib/python3.7/site-packages/pytorch_lightning/trainer/training_loop.py", line 703, in training_forward
    output = self.process_output(output, train=True)
  File "/home/allen_wu/miniconda3/envs/pytorch/lib/python3.7/site-packages/pytorch_lightning/trainer/logging.py", line 107, in process_output
    for k, v in output.items():
AttributeError: 'Tensor' object has no attribute 'items'
bug / fix help wanted

Most helpful comment

@vkrishnamurthy11 , In my case, I checked my returned value of training_step(), It should contain "loss" and "log" keys like this document. But I only returned "loss" so the error message happened.

After I added log key and data into my returned value, it's worked~
I hope this would be useful for you.

All 5 comments

Hi! thanks for your contribution!, great first issue!

Hello, I received the same error as above while trying to do the MNIST project. Were you able to fix it?

@vkrishnamurthy11 , In my case, I checked my returned value of training_step(), It should contain "loss" and "log" keys like this document. But I only returned "loss" so the error message happened.

After I added log key and data into my returned value, it's worked~
I hope this would be useful for you.

@MingLunWu
My model is training now. Thanks!

update to latest version of pytorch-lightening solves the problem here

Was this page helpful?
0 / 5 - 0 ratings

Related issues

justusschock picture justusschock  Â·  3Comments

as754770178 picture as754770178  Â·  3Comments

edenlightning picture edenlightning  Â·  3Comments

versatran01 picture versatran01  Â·  3Comments

anthonytec2 picture anthonytec2  Â·  3Comments