size ( int… ) – a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
import math
from d2l import torch as d2l
x=torch.arange(0,2*math.pi,0.01,requires_grad=True)
y=torch.sin(x)
y.backward(gradient=torch.ones(len(x)))
d2l.plot(x.detach(),[y.detach(),x.grad],‘x’,‘f(x)’,legend=[‘y’,‘df(x)/dx’])
# using plot function defined in 2.4
x = torch.arange(-2*torch.pi, torch.pi*2, 0.1,requires_grad=True)
f=torch.sin(x)
f.sum().backward()
f1=x.grad
x.requires_grad_(False)
plot(x, [torch.sin(x), f1], 'x', 'f(x)', legend=['f(x)=sin(x)', "f'(x)=cos(x)"])