Derivative

$$\frac{d f(x)}{dx}$$

$\frac{d}{dx}[9x^4+2x^3+3x^2+6x+1]$

$\quad =36x^3+6x^2+6x+6$

$f'(2)=36\times 2^3 + 6 \times 2^2 + 6 \times 2 + 6$

$\quad = 330$

In [1]:
import torch
In [2]:
x = torch.tensor(2.0, requires_grad = True)
y = 9*x**4 + 2*x**3 + 3*x**2 + 6*x + 6
y.backward()
x.grad
Out[2]:
tensor(330.)

Partial Derivatives

$y=x^2+z^3$

$\quad f'(x,z)=f'(1,2)$

$\frac{\partial}{\partial x}(z^3 + x^2) = 2x$

$\quad \frac{\partial(f(x)=x^2)}{\partial x} \text{where}\: x =1$

$\quad y'(1)=2$

$\frac{\partial}{\partial z}(z^3 + x^2) = 3z^2$

$\quad \frac{\partial(f(z)=z^3)}{\partial z} \text{where}\: z =2$

$\quad y'(2)=12$

In [3]:
x = torch.tensor(1.0, requires_grad = True)
z = torch.tensor(2.0, requires_grad = True)
y = x**2 + z**3
y.backward()
x.grad
Out[3]:
tensor(2.)
In [4]:
z.grad
Out[4]:
tensor(12.)