

x = torch.ones(5)
y = torch.zeros(3)
w = torch.randn(5, 3, requires_grad = True)
b = torch.randn(3, requires_grad = True)
z = torch.matmul(x, w) + b
loss = torch.nn.functional.binary_cross_entropy_with_logits(z, y)
print('input_tensor:', x)
print('output_tensor:', y)
print('parameter:', w)
print('bias:', b)
print('prediction:', z)
print('loss:', loss)

def f1(x):
return x**2
def diff(f, x):
h = 0.001
return (f(x+h) - f(x-h))/(2*h)
x = torch.tensor(2.0)
print('x=:', x)
dx = diff(f1, x)
print('dx=:', dx)
x=: tensor(2.)
dx=: tensor(3.9999)
def f2(x):
return torch.sum(x**2)
def gradient(f, x):
h = 0.001
grad = torch.zeros_like(x)
for i in range(x.shape[0]):
tmp = float(x[i])
x[i] = tmp + h
f1 = f(x)
x[i] = tmp - h
f2 = f(x)
grad[i] = (f1-f2)/(2*h)
return grad
x = torch.tensor([2.0])
grad1 = gradient(f2, x)
print('grad1:', grad1)
grad1: tensor([3.9999])
x2 = torch.tensor([2.0, 3.0])
grad2 = gradient(f2, x2)
print('grad2:', grad2)
grad2: tensor([4.0002, 5.9996])
x3 = torch.tensor([2.0, 3.0, 4.0, 5.0])
grad3 = gradient(f2, x3)
print('grad3:', grad3)
grad3: tensor([ 3.9997, 5.9986, 7.9994, 10.0002])
x = torch.tensor(2.0, requires_grad = True)
y = x**2
print('x=:', x)
print('y=:', y)
print('detached x:', x.detach().numpy())
print('detached y:', y.detach().numpy())
y.retain_grad()
y.backward()
print('x.grad=:', x.grad)
print('y.grad:', y.grad)
x=: tensor(2., requires_grad=True)
y=: tensor(4)
detached x: 2.0
detached y: 4.0
x = torch.tensor(2.0, requires_grad = True)
y = x**2
y.backward()
print('x=:', x)
print('x.grad=:', x.grad)
x.grad.zero_()
print('x.grad=:', x.grad)
z = x**3
z.backward()
print('x.grad=:', x.grad)
x=: tensor(2., requires_grad=True)
x.grad=: tensor(4.)
x.grad=: tensor(0.)
x.grad=: tensor(12.)
x = torch.tensor(2.0, requires_grad = True)
y = torch.tensor(3.0, requires_grad = True)
z = x**2 + y **2
z.backward()
print('x=:', x)
print('y=:', y)
print('z=:', z)
print('x.grad=:', x.grad)
print('y.grad=:', y.grad)
x=: tensor(2., requires_grad=True)
y=: tensor(3., requires_grad=True)
z=: tensor(13.)
x.grad=: tensor(4.)
y.grad=: tensor(6.)
z = torch.tensor(4.0, requires_grad=True)
f = (x-y)*z
x.grad.zero_()
y.grad.zero_()
f.backward()
print('x.grad:', x.grad)
print('y.grad:', y.grad)
print('z.grad:', z.grad)
x.grad: tensor(4.)
y.grad: tensor(-4.)
z.grad: tensor(-1.)
import torch
x = torch.tensor([2.0, 3.0, 4.0], requires_grad=True)
print('x=:', x)
y = x.sum()
print('y=:', y)
y.backward()
print('x.grad=:', x.grad)
z = x.mean()
print('z=:', z)
x.grad.zero_()
z.backward()
print('x.grad=:', x.grad)
x.grad.zero_()
w = (x**2).mean()
print('w=:', w)
w.backward()
print('x.grad=:', x.grad)
x=: tensor([2., 3., 4.], requires_grad=True)
y=: tensor(9.,)
x.grad=: tensor([1., 1., 1.])
z=: tensor(3.,)
x.grad=: tensor([0.3333, 0.3333, 0.3333])
w=: tensor(9.6667)
x.grad=: tensor([1.3333, 2.0000, 2.6667])
x = torch.tensor([2.0, 3.0, 4.0], requires_grad = True)
print('x=:', x)
print('detached x :', x.detach().numpy())
y = x**2
y.sum().backward(retain_graph = True)
print('y=:', y)
print('y.sum()=', y.sum())
print('x.grad=', x.grad)


x.grad.zero_()
y = x**2
y.backward(gradient = torch.ones_like(y))
print('x.grad=:', x.grad)
x.grad.zero_()
y = torch.tensor([-1.0, -2.0, -3.0], requires_grad = True)
z = x**2 + y**2
z.backward(gradient = torch.ones_like(z))
print('x.grad=:', x.grad)
print('y.grad=:', y.grad)
x.grad=: tensor([4., 6., 8.])
x.grad=: tensor([4., 6., 8.])
y.grad=: tensor([-2., -4., -6.])

x = torch.tensor(2.0, requires_grad = True)
y = torch.tensor(3.0, requires_grad = True)
z = x**2 + y**2
dz_dx = torch.autograd.grad(z, x)
print('dz_dx=:', dz_dx)
print('dz_dx[0]=:', dz_dx[0])
print('x.grad=:', x.grad)
dz_dx=: (tensor(4.),)
dz_dx[0]=: tensor(4.)
x.grad=: None
dz_dy = torch.autograd.grad(z, y)
print('dz_dy=:', dz_dy)
print('dz_dy[0]=:', dz_dy[0])
print('y.grad=:', y.grad)
dz_dy=: (tensor(6.),)
dz_dy[0]=: tensor(6.)
y.grad=: None
import torch
x = torch.tensor([2.0, 3.0, 4.0], requires_grad=True)
y = torch.tensor([-1.0, -2.0, -3.0], requires_grad=True)
z = x**2 + y**2
print('x=:', x)
print('y=:', y)
print('z=:', z)
print('detached x:', x.detach().numpy())
print('detached y:', y.detach().numpy())
print('detached z:', z.detach().numpy())
dz_dx = torch.autograd.grad(outputs = z,
inputs = x,
grad_outputs=torch.ones_like(z),
retain_graph=True)
print('dz_dx=:', dz_dx)
print('x.grad=:', x.grad)
x=: tensor([2., 3., 4.], requires_grad=True)
y=: tensor([-1., -2., -3.], requires_grad=True)
z=: tensor([ 5., 13., 25.])
detached x: [2. 3. 4.]
detached y: [-1. -2. -3.]
detached z: [ 5. 13. 25.]
dz_dx=: (tensor([4., 6., 8.]),)
x.grad=: None
dz_dy = torch.autograd.grad(outputs = z,
inputs = y,
grad_outputs=torch.ones_like(z),
retain_graph=True)
print('dz_dx=:', dz_dy)
print('y.grad=:', y.grad)
# [43]번 셀 코드
v = grad_outputs = torch.ones_like(z)
w = 2*x + 3*y
dzw_dxy = torch.autograd.grad(outputs= [z,w],
inputs = [x,y],
grad_outputs = [v,v])
print('dzw_dxy[0]=:', dzw_dxy[0])
print('dzw_dxy[1]=:', dzw_dxy[1])
dz_dx=: (tensor([-2., -4., -6.]),)
y.grad=: None
dzw_dxy[0]=: tensor([ 6., 8., 10.])
dzw_dxy[1]=: tensor([ 1., -1., -3.])
x = torch.linspace(0, 2*torch.pi, 100, requires_grad = True)
y = x.sin()
y.sum().backward(retain_graph = True)
dydx = x.grad
print(torch.allclose(x.cos(), dydx))
x.grad.zero_()
y.backward(torch.ones_like(y), retain_graph = True)
dydx2 = x.grad
print(torch.allclose(dydx, dydx2))
dydx3 = torch.autograd.grad(outputs=y, inputs = x, grad_outputs = torch.ones_like(y))[0]
print(torch.allclose(dydx, dydx3))
True
True
True
xa = x.detach().numpy()
ya = y.detach().numpy()
plt.plot(xa, ya, label = 'y=sin(x)')
plt.plot(xa, dydx, label = 'dy/dx = cos(x)')
plt.legend()
plt.show()

from torch.autograd.functional import jacobian
def f(x1, x2, x3):
return (x1**2, x2**2, x3**2)
x1 = torch.tensor(2.0)
x2 = torch.tensor(3.0)
x3 = torch.tensor(4.0)
J1 = jacobian(f, (x1, x2, x3))
print('J1=:', J1)
J1=: ((tensor(4.), tensor(0.), tensor(0.)), (tensor(0.), tensor(6.), tensor(0.)), (tensor(0.), tensor(0.), tensor(8.)))
def f(x,y):
return x**2 + y**2
x = torch.tensor([2.0, 3.0, 4.0])
y = torch.tensor([-1.0, -2.0, -3.0])
J = jacobian(f, (x,y))
print('Jx =:', J[0])
print('Jy =:', J[1])
Jx =: tensor([[4., 0., 0.],
[0., 6., 0.],
[0., 0., 8.]])
Jy =: tensor([[-2., -0., -0.],
[-0., -4., -0.],
[-0., -0., -6.]])