The gradient for this tensor will be accumulated into .grad attribute.
import torch
x = torch.ones(1, requires_grad=True); x
tensor([ 1.])
y = x**2
z = x**3
y.backward()
then z.backward()
¶y.backward()
x.grad
tensor([ 2.])
z.backward()
x.grad
tensor([ 5.])
z
then y
¶x = torch.ones(1, requires_grad=True)
y = x**2
z = x**3
z.backward()
x.grad
tensor([ 3.])
y.backward()
x.grad
tensor([ 5.])
y.backward()
¶x = torch.ones(1, requires_grad=True)
y = x**2
z = x**3
y.backward()
x.grad
tensor([ 2.])
x.grad.zero_()
tensor([ 0.])
z.backward()
x.grad
tensor([ 3.])
x = torch.rand((2, 1), requires_grad = True); x
tensor([[ 0.3725], [ 0.4378]])
y = torch.zeros(3, 1)
y[0] = x[0]**2
y[1] = x[1]**3
y[2] = x[1]**4
y.backward(gradient=torch.ones(y.size()))
Cummulative grad of x[0]
and x[1]
respectively.
x.grad
tensor([[ 0.7450], [ 0.9105]])
2*x[0], 3*x[1]**2, 4*x[1]**3
(tensor([ 0.7450]), tensor([ 0.5749]), tensor([ 0.3356]))
2*x[0], 3*x[1]**2 + 4*x[1]**3
(tensor([ 0.7450]), tensor([ 0.9105]))