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

RuntimeError with customized loss while running loss.backward() #216

Open
nowangry opened this issue Jul 19, 2022 · 2 comments
Open

RuntimeError with customized loss while running loss.backward() #216

nowangry opened this issue Jul 19, 2022 · 2 comments

Comments

@nowangry
Copy link

nowangry commented Jul 19, 2022

I wrote a simple loss function in {eval_rcnn.py → eval_one_epoch_joint() → for loop (for data in dataloader: ...)}.
I have enabled all gradient propagation by replacing "torch.no_grad()" with "torch.set_grad_enabled(True)" and set "inputs.requires_grad = True". Here is the use of my loss:

    Z_rcnn_t = rcnn_cls.float()
    loss_adv_cls = - Z_rcnn_t.sum()
    model.zero_grad()
    loss_adv_cls.backward()

The ".backward()" end up with a Runtime Error:

File "/home/jqwu/Codes/PointRCNN/tools/simple_attack.py", line 974, in
eval_single_ckpt(root_result_dir)
File "/home/jqwu/Codes/PointRCNN/tools/simple_attack.py", line 832, in eval_single_ckpt
eval_one_epoch(model, test_loader, epoch_id, root_result_dir, logger)
File "/home/jqwu/Codes/PointRCNN/tools/simple_attack.py", line 759, in eval_one_epoch
ret_dict = eval_one_epoch_joint(model, dataloader, epoch_id, result_dir, logger)
File "/home/jqwu/Codes/PointRCNN/tools/simple_attack.py", line 580, in eval_one_epoch_joint
loss_adv_cls.backward()
File "/home/jqwu/.conda/envs/PointRCNN-py37/lib/python3.7/site-packages/torch/tensor.py", line 195, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "/home/jqwu/.conda/envs/PointRCNN-py37/lib/python3.7/site-packages/torch/autograd/init.py", line 99, in backward
allow_unreachable=True) # allow_unreachable flag
RuntimeError: Expected isFloatingType(grads[i].type().scalarType()) to be true, but got false. (Could this error message be improved? If so, please report an enhancement request to PyTorch.)

I have tried the following solutions, but they didn't work in my case:

qiqihaer/votenet-kitti#5
HaozheQi/P2B#8

I am using CUDA 10.1, torch 1.4, python 3.7.
I have spent a lot of time on this. Anyone has solved a similar problem? 😿

@zqy411470859
Copy link

Have you solved this problem?解决了么

@nowangry
Copy link
Author

Have you solved this problem?解决了么

Not yet....

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

2 participants