Skip to content

Have to change data[0] to data.item() to make main.py work #1

@Ron025

Description

@Ron025

I was trying to implement and run the code. Everything seems to be fine unless I met the following two errors when running the code.

I am new to deep learning & PyTorch. I googled the issue and practiced the solution on StackOverFlow . I don't understand the problem or the solution much but it works.

My environment: Nvidia Tesla P4, cuda 10.0.130, cudnn 7.4.2, pytorch 1.0.1.

Hope it helps.

Traceback (most recent call last):
File "main.py", line 131, in
train(epoch)
File "main.py", line 107, in train
(epoch, batch_ix, loss.data[0]))
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

Traceback (most recent call last):
File "./main.py", line 131, in
train(epoch)
File "./main.py", line 107, in train
(epoch, batch_ix, loss.data[0]))
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions