question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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'

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
MingLunWucommented, May 19, 2020

@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.

1reaction
vkrishnamurthy11commented, Apr 20, 2020

@MingLunWu My model is training now. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

pytorch+tensorboard error " AttributeError: 'Tensor' object has ...
I want to log the loss of the train using the tensorboard in pytorch. I got an error there. AttributeError: 'Tensor' object has...
Read more >
AttributeError: 'Tensor' object has no attribute 'items' #1288
Hi, I'm not sure what's going on. I tried to follow tutorial to organized my code into a LightningModule. Can anyone help?
Read more >
AttributeError: 'Tensor' object has no attribute 'items'
I got an error there. AttributeError: 'Tensor' object has no attribute 'items'. I want to solve this error and check the log using...
Read more >
Attributeerror: tensor object has no attribute numpy : Tricks to Fix
attributeerror : tensor object has no attribute numpy error occurs because of a parameter run_eagerly value if it is set as False.
Read more >
AttributeError: 'Tensor' object has no attribute 'numpy' with ...
If you want a numpy array you can always use np.array(your_tensor). That error message is telling you that there is no numpy() method...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found