fredzzhang / pvic

[ICCV'23] Official PyTorch implementation for paper "Exploring Predicate Visual Context in Detecting Human-Object Interactions"
BSD 3-Clause "New" or "Revised" License
61 stars 8 forks source link

Get grad of inputs #55

Closed xiezouqu closed 1 month ago

xiezouqu commented 1 month ago

Hello, I respectfully appreciate the work you have done. I encountered the following issue during getting grad of inputs, and I would greatly appreciate your help in solving it.

function def _on_each_iteration(self): in util.py

self._state.loss = sum(loss for loss in loss_dict.values())
        self._state.optimizer.zero_grad(set_to_none=True)
        self._state.loss.backward()

        #To get inputs.grad
        grad = self._state.inputs[0][0].grad

but grad is None, I want to know how to get the correct the grad.and I would greatly appreciate your help in solving it. @fredzzhang

fredzzhang commented 1 month ago

Hi @xiezouqu,

The tensor.grad attribute only works when the tensor has requires_grad set to True. Normally it's only applied to the network parameters that will be updated. If you want to get the gradient of the parameters, you need to call .grad on those parameters.

Fred.

xiezouqu commented 1 month ago

Hi @fredzzhang Thank you very much for your response!