tsuijenk:

But in my custom dataset class, I specifically set inputs’s require_grad=False. If I don’t reset it to True before passing into the network, doesn’t it mean that there will be no grad_fn being tracked?


I assume you are concerned about the grad_fn of the intermediate activations and thus the computation graph: no, this won’t be a problem since you are only changing if the input should get gradients and are not changing anything else in the model. Here is a small example:
# standard use case
x = torch.randn(1, 1)
print(x.requires_grad)
# > False

lin = nn.Linear(1, 1)
out = lin(x)
print(out.grad_fn)
# > <AddmmBackward0 object at 0x7fcea08c5610>
out.backward()
print(lin.weight.grad)
# > tensor([[-0.9785]])
print(x.grad)
# > None

# input requires grad
x = torch.randn(1, 1, requires_grad=True)
print(x.requires_grad)
# > True

lin = nn.Linear(1, 1)
out = lin(x)
print(out.grad_fn)
# > <AddmmBackward0 object at 0x7fcea08d4640>
out.backward()
print(lin.weight.grad)
# > tensor([[1.6739]])
print(x.grad)
# >tensor([[0.0300]])

Both approaches work with the difference that the second one will calculate gradients for the input, which is usually not needed.



 tsuijenk:

Then we convert it to a Torch Float Tensor vector and then we take average.


Use torch.stack.