Hello everyone. I really need to embed a .backward() Function in a .forward() of another Function, but failed, help!
x = np.array([[1.,2.], [3.,4.], [5.,6.]])
x = Variable(torch.from_numpy(x).float(), requires_grad=True)
y = var(x)
y.backward(y)     
# it works fine up to here
print(x.grad)       

# but -----------------
class Y_fun(Function):
    def forward(self, x):
        y = var(x)
        y.backward(y)
        print(x.grad)      # exact same thing, but in a forward method, failed
        return y

def y_fun(x):
    return Y_fun()(x)

y = y_fun(x)

in the code var() is also a Function which has .forward() and .backward()
the error is:
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
Actually, this piece of code should work well in Pytorch 0.1.10. But after a while, I want to continue my work in 0.4 it failed. I spent a whole day to figure out the reason, and I am despaired now.
Help, please.
Thank you!