-
Notifications
You must be signed in to change notification settings - Fork 89
/
weight_init.py
155 lines (126 loc) · 4.76 KB
/
weight_init.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from sklearn.datasets import make_moons
# Params
lr = 0.3
steps = 100
seed = 123
np.random.seed(seed)
torch.manual_seed(seed)
# Define the model
class MLP(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(2, 20)
self.fc_out = nn.Linear(20, 1)
def forward(self, x):
"""
Forward propagation
y1 = W_1 * X + b
a1 = relu(y1)
y2 = W_2 * a1 + b
probs = sigmoid(y2)
"""
x = self.fc1(x)
x = F.relu(x)
x = self.fc_out(x)
return torch.sigmoid(x)
if __name__ == '__main__':
# Loss
criterion = torch.nn.BCELoss()
# Datasets
X, y = make_moons(n_samples=50, shuffle=True, noise=0.0, random_state=seed)
X = torch.from_numpy(X).float()
y = torch.from_numpy(y).float().unsqueeze(-1)
# Experiments
name = ['Weight Init = 0.1\n [Gradient vanish]',
'Weight Init = 0\n [No learning]',
'Kaiming Init',
'Weight Init = 10\n [Gradient vanish due to Sigmoid]']
init_funcs = [lambda x: nn.init.constant_(x, 0.1),
lambda x: nn.init.constant_(x, 0),
lambda x: x, # By default kaiming uniform
lambda x: nn.init.constant_(x, 10)]
# Decision boundary visualization
h = 0.02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
Xmesh = np.c_[xx.ravel(), yy.ravel()]
Xmesh = torch.from_numpy(Xmesh).float()
fig = plt.figure(figsize=(14, 10))
idx = 1
for name, init_fn in zip(name, init_funcs):
model = MLP()
# Init weights and biased
for layer in model.modules():
if type(layer) == nn.Linear:
init_fn(layer.weight)
init_fn(layer.bias)
# Optimizer
optimizer = torch.optim.SGD(model.parameters(), momentum=0.9, lr=lr)
grad_trajectories = []
for p in model.parameters():
grad_trajectories.append([[] for i in range(p.numel())])
# Init figures
ax1 = plt.subplot(len(init_funcs), 5, idx)
ax2 = plt.subplot(len(init_funcs), 5, idx + 1)
ax3 = plt.subplot(len(init_funcs), 5, idx + 2)
ax4 = plt.subplot(len(init_funcs), 5, idx + 3)
ax5 = plt.subplot(len(init_funcs), 5, idx + 4)
# Train loop
for step in range(steps):
model.train()
# Clear gradient
optimizer.zero_grad()
probs = model(X)
loss = criterion(probs, y)
# also get accuracy
preds = (probs > 0.5).float()
accuracy = (preds == y).sum() / y.shape[0]
# Compute gradients
loss.backward()
optimizer.step()
with torch.no_grad():
model.eval()
# Plot weights and gradients
for i, p in enumerate(model.parameters()):
grads = p.grad.detach().flatten()
params = p.detach().flatten()
for j, (per_layer_params, per_layer_grad) in enumerate(zip(params, grads)):
grad_trajectories[i][j].append(per_layer_grad.item())
[ax1.plot(range(step + 1), x) for x in grad_trajectories[0]]
ax1.set_title('Layer 1: dL/dw')
ax1.grid()
[ax2.plot(range(step + 1), x) for x in grad_trajectories[1]]
ax2.set_title('Layer 1: dL/db')
ax2.grid()
[ax3.plot(range(step + 1), x) for x in grad_trajectories[2]]
ax3.set_title('Layer 2: dL/dw')
ax3.grid()
[ax4.plot(range(step + 1), x) for x in grad_trajectories[3]]
ax4.set_title('Layer 2: dL/db')
ax4.grid()
# Draw decision boundary
probs_mesh = model(Xmesh)
Z = (probs_mesh > 0.5).float().reshape(xx.shape)
ax5.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8)
ax5.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=plt.cm.coolwarm, edgecolors='k', alpha=1)
ax5.set_title(name)
ax5.set_xticks([])
ax5.set_yticks([])
ax5.set_xlabel(f'Step: {step + 1}')
plt.tight_layout()
plt.pause(0.00001)
if step != steps - 1:
ax1.cla()
ax2.cla()
ax3.cla()
ax4.cla()
ax5.cla()
idx += 5
plt.show()