Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalid index of a 0-dim tensor #18

Open
MartinHHF opened this issue Sep 23, 2020 · 3 comments
Open

invalid index of a 0-dim tensor #18

MartinHHF opened this issue Sep 23, 2020 · 3 comments

Comments

@MartinHHF
Copy link

When I am running the code, here is an error for the program.

exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 141, in
train(epoch)
File "", line 95, in train
error, _ = model.calculate_classification_error(data, bag_label)
File "/home/huafeng/AttentionDeepMIL-master/model.py", line 60, in calculate_classification_error
error = 1. - Y_hat.eq(Y).cpu().float().mean().data[0]
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

And I have tried to change .data[0] into .data or .item(), but neither worked, could anyone give me some suggestions?

Pytorch version:1.1.0

@sadeel
Copy link

sadeel commented Sep 26, 2020

Change line to
error = 1. - Y_hat.eq(Y).cpu().float().mean().data.item()

@MartinHHF
Copy link
Author

Change line to
error = 1. - Y_hat.eq(Y).cpu().float().mean().data.item()

Thx for your suggestions. I try to use it, but nothing changed.

@xavierad
Copy link

Simply use: error = 1. - Y_hat.eq(Y).cpu().float().mean().item()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants