PyTorch Tutorial - Backpropagation

Tensor

1
2
3
4
5
6
7
8
9
10
11
12
13
import torch

a = torch.tensor([1.0])
a.requires_grad = True
print(a) #tensor([1.], requires_grad=True)
print(a.data) #tensor([1.])
print(a.type()) #torch.FloatTensor
print(a.data.type()) #torch.FloatTensor
print(a.grad) #None
print(type(a.grad))
print(a.item()) # 1.0
print(type(a.item())) #<class 'float'>

Backpropagation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import torch
import matplotlib.pyplot as plt

# prepare the training data
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]

# initialize weights
w = torch.tensor([1.0])
w.requires_grad = True # need to calculate the gradient

# define a linear model
def forward(x):
return w*x # this is a tensor

# define a loss function
def loss(x, y):
y_pred = forward(x)
return (y_pred-y)**2

# training process
epoch_list = []
loss_list = []
print("prediction (before training):", 4, forward(4).item())
for epoch in range(100):
for x, y in zip(x_data, y_data):
l = loss(x, y) # l is a tensor. Tensor is to build computational graph
l.backward() # backward, compute grad for Tensor whose requires_grad == True
# in the previous lecture: grad = 2*x*(x*w-y)
print("\tgrad: ", x, y, w.grad.item())
w.data -= 0.01*w.grad.data # update the tensor, grad is a tensor here

w.grad.data.zero_()# set the grad to zero
print("progress: ", epoch, l.item())
epoch_list.append(epoch)
loss_list.append(l.item())
print("predict (after training): ", 4, forward(4).item())

# visualization
plt.plot(epoch_list, loss_list)
plt.ylabel("loss")
plt.xlabel("epoch")
plt.show()


Exercise

Exercise 4-1:

  • Calculate the partial derivative of the loss with respect to the weight

Exercise 4-2:

  • Calculate the partial derivative of the loss with respect to the weight

Assignment

Exercise 4-3:

  • Calculate the partial derivative of the loss with respect to the weight
  • PyTorch Implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import numpy as np
import matplotlib.pyplot as plt
import torch

# prepare training data
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]

# initialize w1, w2, b
w1 = torch.Tensor([1.0])
w1.requires_grad = True
w2 = torch.Tensor([1.0])
w2.requires_grad = True
b = torch.Tensor([1.0])
b.requires_grad = True

# define linear model
def forward(x):
return w1*x**2 + w2*x+b

# define loss function -> build up the computational graph
def loss(x, y):
y_pred = forward(x)
return (y_pred - y) **2

## training process
epoch_list = []
loss_list = []
print('Predict (before training):', 4, forward(4))
for epoch in range(100):
for x,y in zip(x_data, y_data):
l = loss(x, y)
l.backward()
print("\tgradient: ", x, y, w1.grad.item(), w2.grad.item(), b.grad.item())
w1.data -= 0.01 * w1.grad.data
w2.data -= 0.01 * w2.grad.data
b.data -= 0.01 * b.grad.data

# set tensor to zero
w1.grad.data.zero_()
w2.grad.data.zero_()
b.grad.data.zero_()
print('Epoch: ', epoch, l.item())
epoch_list.append(epoch)
loss_list.append(l.item())
print("Predoct (after training):", 4, forward(4).item())

# visualization
plt.plot(epoch_list, loss_list)
plt.ylabel("loss")
plt.xlabel("epoch")
plt.show()