So is it the creating of new variables causing the problem with the graph?
In my case, the modification that would be the equivalent of deepcode’s are the making a new variable (from the x.scatter_() in code below) on the output of matrix multiplication. If I don’t do this, it works fine of course. It tells me the same error as @deepcode presumably because I have requires_grad=False on the new variables. Still, there is w1 and w2 with requires_grad=True so there should be graph nodes to compute.
Example Code:
w1 = Variable(torch.randn(shape[0], shape[1]).type(dtype), requires_grad=True)
w2 = Variable(torch.randn(shape[1], shape[2]).type(dtype), requires_grad=True)

x = Variable(torch.randn(N, shape[0]).type(dtype), requires_grad=False)
y = Variable(torch.randn(N, shape[2]).type(dtype), requires_grad=False)

learning_rate = 0.001
for t in range(epochs):
    l2 = x.mm(w1)
    #these next two lines cause problems
    topk, indices = torch.topk(l2, k[1])
    l2_sp = Variable(torch.zeros([N, H]).scatter_(1, indices.data, topk.data), requires_grad=False)
    l3 = l2.mm(w2)
    #same here
    topk, indices = torch.topk(l3, k[2])
    l3_sp = Variable(torch.zeros([N, D_out]).scatter_(1, indices.data, topk.data),requires_grad=False)
    
    loss = (l3_sp - y).abs().sum()
    loss.backward()

    w1.data -= learning_rate * w1.grad.data
    w2.data -= learning_rate * w2.grad.data

    w1.grad.data.zero_()
    w2.grad.data.zero_()

However, If I set the new variables that are created to have requires_grad=True, there’s no more of the first error, but rather the weights I’m actually interested in performing backprop on, w1 and w2 which originally had requires_grad=True never get their gradients initialized after loss.backward(). This is indicated by the error on the line

—> w1.data -= learning_rate * w1.grad.data


AttributeError: ‘NoneType’ object has no attribute ‘data’

I’ve also tried using my own class from nn.Module, and putting all that in the forward() but still causes the same problem.