Explaining backward of y=x*x

It might be a silly question since I’m fairly new to pytorch but can you please explain me what following code is calculating?

x = torch.tensor([7.0], requires_grad=True)
y = x*x
y.backward(torch.tensor([2.0]))
print(x.grad)

output -> tensor([ 28.])

Tahaks

Hi,

Your network corresponds to the following function: f(x) = y = x^2 and df(x) = 2*x.
Your backward pass will compute dL/dx = dL/dy * dy/dx = 2.0 * df(x) = 2.0 * 2 * x = 4*7 = 28.

2 Likes