-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastfood_fly_old.py
231 lines (174 loc) · 6.95 KB
/
fastfood_fly_old.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import torch
from torch import nn
import numpy as np
from torch.nn import functional as F
class FastfoodWrap(nn.Module):
def __init__(self, module, intrinsic_dimension, device=0):
"""
Wrapper to estimate the intrinsic dimensionality of the
objective landscape for a specific task given a specific model using FastFood transform
:param module: pytorch nn.Module
:param intrinsic_dimension: dimensionality within which we search for solution
:param device: cuda device id
"""
super(FastfoodWrap, self).__init__()
# Hide this from inspection by get_parameters()
self.m = [module]
self.name_base_localname = []
# Stores the initial value: \theta_{0}^{D}
self.initial_value = dict()
# Fastfood parameters
self.fastfood_params = {}
# Parameter vector that is updated
# Initialised with zeros as per text: \theta^{d}
V = nn.Parameter(torch.zeros((intrinsic_dimension)).to(device))
self.register_parameter("V", V)
v_size = (intrinsic_dimension,)
# Iterate over layers in the module
for name, param in module.named_parameters():
# If param requires grad update
if param.requires_grad:
# Saves the initial values of the initialised parameters from param.data and sets them to no grad.
# (initial values are the 'origin' of the search)
self.initial_value[name] = v0 = (
param.clone().detach().requires_grad_(False).to(device)
)
# Generate fastfood parameters
DD = np.prod(v0.size())
self.fastfood_params[name] = fastfood_vars(DD, device)
base, localname = module, name
while "." in localname:
prefix, localname = localname.split(".", 1)
base = base.__getattr__(prefix)
self.name_base_localname.append((name, base, localname))
for name, base, localname in self.name_base_localname:
delattr(base, localname)
def forward(self, x):
# Iterate over layers
for name, base, localname in self.name_base_localname:
init_shape = self.initial_value[name].size()
DD = np.prod(init_shape)
# Fastfood transform te replace dence P
ray = fastfood_torched(self.V, DD, self.fastfood_params[name]).view(
init_shape
)
param = self.initial_value[name] + ray
setattr(base, localname, param)
# Pass through the model, by getting hte module from a list self.m
module = self.m[0]
x = module(x)
return x
def fast_walsh_hadamard_torched(x, axis=0, normalize=False):
"""
Performs fast Walsh Hadamard transform
:param x:
:param axis:
:param normalize:
:return:
"""
orig_shape = x.size()
assert axis >= 0 and axis < len(orig_shape), (
"For a vector of shape %s, axis must be in [0, %d] but it is %d"
% (orig_shape, len(orig_shape) - 1, axis)
)
h_dim = orig_shape[axis]
h_dim_exp = int(round(np.log(h_dim) / np.log(2)))
assert h_dim == 2 ** h_dim_exp, (
"hadamard can only be computed over axis with size that is a power of two, but"
" chosen axis %d has size %d" % (axis, h_dim)
)
working_shape_pre = [int(np.prod(orig_shape[:axis]))] # prod of empty array is 1 :)
working_shape_post = [
int(np.prod(orig_shape[axis + 1 :]))
] # prod of empty array is 1 :)
working_shape_mid = [2] * h_dim_exp
working_shape = working_shape_pre + working_shape_mid + working_shape_post
ret = x.view(working_shape)
for ii in range(h_dim_exp):
dim = ii + 1
arrs = torch.chunk(ret, 2, dim=dim)
assert len(arrs) == 2
ret = torch.cat((arrs[0] + arrs[1], arrs[0] - arrs[1]), axis=dim)
if normalize:
ret = ret / torch.sqrt(float(h_dim))
ret = ret.view(orig_shape)
return ret
def fastfood_vars(DD, device=0):
"""
Returns parameters for fast food transform
:param DD: desired dimension
:return:
"""
ll = int(np.ceil(np.log(DD) / np.log(2)))
LL = 2 ** ll
B_seed = torch.seed()
Pi_seed = torch.seed()
G_seed = torch.seed()
# Gaussian scaling matrix, whose elements $G_{i,i} \sim \mathcal{N}(0, 1)$
torch.manual_seed(G_seed)
GG = torch.FloatTensor(LL,).normal_().to(device)
GG.requires_grad = False
divisor = torch.sqrt(LL * torch.sum(torch.pow(GG, 2)))
del GG
return [B_seed, Pi_seed, G_seed, divisor, LL]
def fastfood_torched(x, DD, param_list=None, device=0):
"""
Fastfood transform
:param x: array of dd dimension
:param DD: desired dimension
:return:
"""
dd = x.size(0)
if not param_list:
B_seed, Pi_seed, G_seed, divisor, LL = fastfood_vars(DD, device=device)
else:
B_seed, Pi_seed, G_seed, divisor, LL = param_list
# Binary scaling matrix where $B_{i,i} \in \{\pm 1 \}$ drawn iid
torch.manual_seed(B_seed)
BB = torch.FloatTensor(LL).uniform_(0, 2).type(torch.LongTensor)
BB = (BB * 2 - 1).type(torch.FloatTensor).to(device)
BB.requires_grad = False
# Random permutation matrix
torch.manual_seed(Pi_seed)
Pi = torch.LongTensor(np.random.permutation(LL)).to(device)
Pi.requires_grad = False
# Gaussian scaling matrix, whose elements $G_{i,i} \sim \mathcal{N}(0, 1)$
torch.manual_seed(G_seed)
GG = torch.FloatTensor(LL,).normal_().to(device)
GG.requires_grad = False
# Padd x if needed
dd_pad = F.pad(x, pad=(0, LL - dd), value=0, mode="constant")
# From left to right HGPiH(BX), where H is Walsh-Hadamard matrix
mul_1 = torch.mul(BB, dd_pad)
# HGPi(HBX)
mul_2 = fast_walsh_hadamard_torched(mul_1, 0, normalize=False)
# HG(PiHBX)
mul_3 = mul_2[Pi]
# H(GPiHBX)
mul_4 = torch.mul(mul_3, GG)
# (HGPiHBX)
mul_5 = fast_walsh_hadamard_torched(mul_4, 0, normalize=False)
ret = torch.div(mul_5[:DD], divisor * np.sqrt(float(DD) / LL))
del BB, GG, Pi
return ret
def generate_matrices(param_list):
B_seed, Pi_seed, G_seed, divisor, LL = param_list
# Binary scaling matrix where $B_{i,i} \in \{\pm 1 \}$ drawn iid
torch.manual_seed(B_seed)
BB = torch.FloatTensor(LL).uniform_(0, 2).type(torch.LongTensor)
BB = (BB * 2 - 1).type(torch.FloatTensor).to(device)
BB.requires_grad = False
# Random permutation matrix
torch.manual_seed(Pi_seed)
Pi = torch.LongTensor(np.random.permutation(LL)).to(device)
Pi.requires_grad = False
# Gaussian scaling matrix, whose elements $G_{i,i} \sim \mathcal{N}(0, 1)$
torch.manual_seed(G_seed)
GG = torch.FloatTensor(LL,).normal_().to(device)
GG.requires_grad = False
divisor_tmp = torch.sqrt(LL * torch.sum(torch.pow(GG, 2)))
print(divisor_tmp, divisor)
print(BB)
printf(Pi)
print(GG)
del BB, Pi, GG