Assuming the following forward pass: x --> y --> z, in which
x is a scalar
y = x * x
z = 2 * y
I want to track the following derivatives/gradients, including the gradient for the intermediate function y:

dz/dy, gradient of z with respect to y, which should be “2”
dy/dx, gradient of y with respect to x, which should be “2x”
dz/dx, gradient of z with respect to x, which should be “4x”

So, I initiated both x and y with the requires_grad=True argument. However, I can only get y.grad, which is “2”, and x.grad returned “None”, as shown below.
May I ask:

Why I’m unable to get x.grad (dz/dx) in this case?
How to get the gradients for both the input and intermediate variables via .backward()?

Screenshot 2020-12-10 at 13.58.04669×970 49.6 KB