Backprop through Pytorch Element-Wise Operation if input contains NaNs
Suppose I'd like to compute the element-wise quotient between two tensors. If one of the tensors contains NaNs, the resulting quotient will also contain NaNs, and this I understand. But why does the gradient becomes non-existent for the entire operation? And how can I preserve the gradient for the non-NaN entries?

For example:

&gt;&gt;&gt; x = torch.tensor([1.0, np.NaN])
&gt;&gt;&gt; y = torch.tensor([2.0, 3.0])
&gt;&gt;&gt; z = torch.div(y, x)
&gt;&gt;&gt; z
tensor([2., nan])
&gt;&gt;&gt; z.backward()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/usr/local/lib/python3.6/dist-packages/torch/tensor.py", line 107, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "/usr/local/lib/python3.6/dist-packages/torch/autograd/__init__.py", line 93, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn


There are a lot of mistakes in your code, which I hereby address, in hope to enlighten you :)


RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn is NOT because of the NaN. It's because none of your input variables require a gradient, therefore z doesn't have the possibility to call backward(). You need to start the backpropagation tree somewhere.
You can't just .backward() on a tensor with multiple values. You need to sum() first, or some similar operation, but that would produce NaN in your case. You can backpropagate both parts of the z separately, by calling .backward(torch.Tensor([1.0,1.0])).


Therefore, if you fix all the bugs, it should work:

import torch
import numpy as np

x = torch.tensor([1.0, np.NaN], requires_grad=True)
y = torch.tensor([2.0, 3.0])
z = torch.div(y, x)

z.backward(torch.Tensor([1.0,1.0]))
print(x.grad)


tensor([-2., nan])

