a=torch.tensor([3.0],requires_grad=True)
def sqrt(x):
return x*x
b=sqrt(a)
print(b)
c=sqrt(b)
c.backward()
print(c.grad)
how can i see gradient of b tensor with respect c? it should be 2*x, and if b==9, gradient should be 18
a=torch.tensor([3.0],requires_grad=True)
def sqrt(x):
return x*x
b=sqrt(a)
print(b)
c=sqrt(b)
c.backward()
print(c.grad)
how can i see gradient of b tensor with respect c? it should be 2*x, and if b==9, gradient should be 18
You can use b.retain_grad()
to instruct PyTorch to store the gradients of intermediates like b
.
Best regards
Thomas