Why am I not getting a gradient with respect to my input data?

I’m running a model where I need to get the gradient of the loss function w.r.t my input data so I can use it to update previous networks that are in series. I thought I was calling everything right, but I have encounted a problem where the gradients of my loss function w.r.t input data is zero or very close to it. What am I doing wrong? The gradients exist throughout the network, loss is nonzero, etc.
Here’s the code for the backpropagation. after_graph is the output of a graph neural network, and net2 is my graph.

after_graph2 = Variable(after_graph.clone(), requires_grad=True)
        
        after_net2 = self.net2(after_graph2)
        # back prop
        loss = self.loss_func(after_net2, y)
        # find gradients of net 2
        loss.backward(retain_graph = True)
        
        loss_graph_output = after_graph2.grad

Here’s the net:

net2 = nn.Sequential(
    nn.Linear(input_size2, hidden_sizes2[0]),
    nn.Tanh(),
    nn.Linear(hidden_sizes2[0], output_size2),
    nn.Tanh()
)

What could be going wrong?

If the gradients are not None but close to zero, the gradient calculation was performed, but your gradient might vanish.
Using your model I get valid gradients for the input:

net2 = nn.Sequential(
    nn.Linear(10, 10),
    nn.Tanh(),
    nn.Linear(10, 10),
    nn.Tanh()
)

x = torch.randn(1, 10, requires_grad=True)
out = net2(x)
out.mean().backward()
print(x.grad)
> tensor([[-0.0204,  0.0356, -0.0084, -0.0122,  0.0018, -0.0206,  0.0270,  0.0311,
          0.0075,  0.0081]])

PS: Variables are deprecated since PyTorch 0.4.0, so you can use tensors now.