-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathunit_activation_reinitializer.py
More file actions
215 lines (168 loc) · 5.64 KB
/
unit_activation_reinitializer.py
File metadata and controls
215 lines (168 loc) · 5.64 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
import numpy as np
import math
def compute_activation_std(
model, dataset, device="cpu", batch_size=32, num_workers=0, layer_names=None
):
activations = {}
handles = []
def save_activation(name):
def hook(module, input, output):
if isinstance(output, tuple):
output = output[0]
activations[name].append(output.detach())
return hook
for name, module in model.named_modules():
if name in layer_names:
activations[name] = []
handle = module.register_forward_hook(save_activation(name))
handles.append(handle)
loader = torch.utils.data.DataLoader(
dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers
)
model.to(device)
model.eval()
with torch.no_grad():
for batch in loader:
if isinstance(batch, (list, tuple)):
inputs = batch[0].to(device)
else:
inputs = batch.to(device)
_ = model(inputs)
break
layer_activation_std = {}
for name in layer_names:
try:
act = torch.cat(activations[name], dim=0)
except:
print(activations[name])
break
act_std = act.std().item()
layer_activation_std[name] = act_std
for handle in handles:
handle.remove()
return layer_activation_std
def adjust_weight_init(
model,
dataset,
device="cpu",
batch_size=32,
num_workers=0,
tol=0.2,
max_iters=10,
exclude_layers=None,
):
if exclude_layers is None:
exclude_layers = []
layers_to_adjust = []
for name, module in model.named_modules():
if isinstance(module, (nn.Linear, nn.Conv2d)) and not isinstance(
module, tuple(exclude_layers)
):
layers_to_adjust.append((name, module))
print(f"Layers to adjust: {layers_to_adjust}")
initial_std = {}
layer_weight_std = {}
for name, module in layers_to_adjust:
print(f"Adjusting layer: {name}")
initial_std[name] = module.weight.std().item()
fan_in = np.prod(module.weight.shape[1:])
weight_std = np.sqrt(1 / fan_in) # use muP for initialization.
for i in range(max_iters):
nn.init.normal_(module.weight, std=weight_std)
activation_std = compute_activation_std(
model, dataset, device, batch_size, num_workers, layer_names=[name]
)[name]
print(f"Iteration {i+1}: Activation std = {activation_std:.4f}")
if abs(activation_std - 1.0) < tol:
print(
f"Layer {name} achieved near unit activation of {activation_std:.4f} with weight std = {weight_std:.4f}"
)
layer_weight_std[name] = weight_std / activation_std
break
else:
weight_std = weight_std / activation_std
else:
print(f"Layer {name} did not converge within {max_iters} iterations.")
layer_weight_std[name] = weight_std
return initial_std, layer_weight_std
#### HOW TO USE
# 1. define dataset
# 2. define model
# 3. launch.
transform = transforms.Compose(
[
transforms.ToTensor(),
]
)
train_dataset = datasets.MNIST(
root="mnist_data", train=True, transform=transform, download=True
)
class CustomActivation(nn.Module):
def forward(self, x):
return x * torch.sigmoid(x)
class ResBlock(nn.Module):
def __init__(self, in_channels, out_channels, reduction_ratio=2):
super(ResBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = out.mean(dim=[-1, -2])
return out
class MLPModel(nn.Module):
def __init__(self):
super(MLPModel, self).__init__()
self.block1 = ResBlock(1, 256)
self.fc1 = nn.Linear(256, 256)
self.act1 = CustomActivation()
self.ln1 = nn.LayerNorm(256)
self.fc2 = nn.Linear(256, 128)
self.act2 = nn.ReLU()
self.ln2 = nn.LayerNorm(128)
self.fc3 = nn.Linear(128, 64)
self.act3 = nn.Tanh()
self.fc_residual = nn.Linear(256, 64)
self.fc4 = nn.Linear(64, 10)
def forward(self, x):
out1 = self.block1(x)
out1 = out1.view(out1.shape[0], -1)
out1 = self.act1(out1)
out1 = self.fc1(out1)
out1 = self.ln1(out1)
out2 = self.act2(self.fc2(out1))
out2 = self.ln2(out2)
out3 = self.act3(self.fc3(out2))
res = self.fc_residual(out1)
out3 += res
logits = self.fc4(out3)
return logits
model = MLPModel()
exclude_layers = [nn.LayerNorm]
initial_std, layer_weight_std = adjust_weight_init(
model,
dataset=train_dataset,
device="cuda:0",
batch_size=64,
num_workers=0,
tol=0.1,
max_iters=10,
exclude_layers=exclude_layers,
)
print("\nAdjusted Weight Standard Deviations. Before -> After:")
for layer_name, std in layer_weight_std.items():
print(
f"Layer {layer_name}, Changed STD from \n {initial_std[layer_name]:.4f} -> STD {std:.4f}\n"
)
print(layer_weight_std)