forked from deeplearningzerotoall/PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab-10-X1-mnist_back_prop.py
96 lines (77 loc) · 3 KB
/
lab-10-X1-mnist_back_prop.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Lab 10 MNIST and softmax
import torch
import torchvision.datasets as dsets
import torchvision.transforms as transforms
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# for reproducibility
torch.manual_seed(777)
if device == 'cuda':
torch.cuda.manual_seed_all(777)
# parameters
learning_rate = 0.5
batch_size = 10
# MNIST dataset
mnist_train = dsets.MNIST(root='MNIST_data/',
train=True,
transform=transforms.ToTensor(),
download=True)
mnist_test = dsets.MNIST(root='MNIST_data/',
train=False,
transform=transforms.ToTensor(),
download=True)
# dataset loader
data_loader = torch.utils.data.DataLoader(dataset=mnist_train,
batch_size=batch_size,
shuffle=True,
drop_last=True)
w1 = torch.nn.Parameter(torch.Tensor(784, 30)).to(device)
b1 = torch.nn.Parameter(torch.Tensor(30)).to(device)
w2 = torch.nn.Parameter(torch.Tensor(30, 10)).to(device)
b2 = torch.nn.Parameter(torch.Tensor(10)).to(device)
torch.nn.init.normal_(w1)
torch.nn.init.normal_(b1)
torch.nn.init.normal_(w2)
torch.nn.init.normal_(b2)
def sigma(x):
# sigmoid function
return 1.0 / (1.0 + torch.exp(-x))
# return torch.div(torch.tensor(1), torch.add(torch.tensor(1.0), torch.exp(-x)))
def sigma_prime(x):
# derivative of the sigmoid function
return sigma(x) * (1 - sigma(x))
X_test = mnist_test.test_data.view(-1, 28 * 28).float().to(device)[:1000]
Y_test = mnist_test.test_labels.to(device)[:1000]
i = 0
while not i == 10000:
for X, Y in data_loader:
i += 1
# forward
X = X.view(-1, 28 * 28).to(device)
Y = torch.zeros((batch_size, 10)).scatter_(1, Y.unsqueeze(1), 1).to(device) # one-hot
l1 = torch.add(torch.matmul(X, w1), b1)
a1 = sigma(l1)
l2 = torch.add(torch.matmul(a1, w2), b2)
y_pred = sigma(l2)
diff = y_pred - Y
# Back prop (chain rule)
d_l2 = diff * sigma_prime(l2)
d_b2 = d_l2
d_w2 = torch.matmul(torch.transpose(a1, 0, 1), d_l2)
d_a1 = torch.matmul(d_l2, torch.transpose(w2, 0, 1))
d_l1 = d_a1 * sigma_prime(l1)
d_b1 = d_l1
d_w1 = torch.matmul(torch.transpose(X, 0, 1), d_l1)
w1 = w1 - learning_rate * d_w1
b1 = b1 - learning_rate * torch.mean(d_b1, 0)
w2 = w2 - learning_rate * d_w2
b2 = b2 - learning_rate * torch.mean(d_b2, 0)
if i % 1000 == 0:
l1 = torch.add(torch.matmul(X_test, w1), b1)
a1 = sigma(l1)
l2 = torch.add(torch.matmul(a1, w2), b2)
y_pred = sigma(l2)
acct_mat = torch.argmax(y_pred, 1) == Y_test
acct_res = acct_mat.sum()
print(acct_res.item())
if i == 10000:
break