Understanding backpropagation in PyTorch
I am exploring PyTorch, and I do not understand the output of the following  example:
# Initialize x, y and z to values 4, -3 and 5
x = torch.tensor(4., requires_grad = True)
y = torch.tensor(-3., requires_grad = True)
z = torch.tensor(5., requires_grad = True)

# Set q to sum of x and y, set f to product of q with z
q = x + y
f = q * z

# Compute the derivatives
f.backward()

# Print the gradients
print(&quot;Gradient of x is: &quot; + str(x.grad))
print(&quot;Gradient of y is: &quot; + str(y.grad))
print(&quot;Gradient of z is: &quot; + str(z.grad))

Output
Gradient of x is: tensor(5.)
Gradient of y is: tensor(5.)
Gradient of z is: tensor(1.)

I have little doubt that my confusion originates with a minor misunderstanding. Can someone explain in a stepwise manner?

I hope you understand that When you do f.backward(), what you get in x.grad is .
In your case
.
So, simply (with preliminary calculus)

If you put your values for x, y and z, that explains the outputs.
But, this isn't really &quot;Backpropagation&quot; algorithm. This is just partial derivatives (which is all you asked in the question).
Edit:
If you want to know about the Backpropagation machinery behind it, please see @Ivan's answer.
