-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiMap.py
More file actions
262 lines (209 loc) · 9.15 KB
/
Copy pathBiMap.py
File metadata and controls
262 lines (209 loc) · 9.15 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import torch
from torch.autograd import Function
from torch import nn
class BiMapFunction(Function):
@staticmethod
def forward(ctx, input, weight):
ctx.save_for_backward(input, weight)
output = (weight.double() @ input.double()) @ weight.double().t()
return output
@staticmethod
def backward(ctx, grad_output):
input, weight = ctx.saved_variables
grad_input = grad_weight = None
input = input.double()
if ctx.needs_input_grad[0]:
grad_input = weight.t() @ grad_output @ weight
if ctx.needs_input_grad[1]:
e_grad = 2 * grad_output @ weight @ input
e_grad = e_grad.sum(0)
grad_weight = e_grad - e_grad @ weight.t() @ weight
return grad_input, grad_weight
class BiMap(nn.Module):
def __init__(self, input_features, output_features):
super(BiMap, self).__init__()
self.input_features = input_features
self.output_features = output_features
if output_features < input_features:
self.weight = nn.Parameter(torch.Tensor(
output_features, input_features))
a = torch.rand(input_features, input_features)
u, s, v = torch.svd(a@a.t())
self.weight.data = u[:, :output_features].t()
self.weight.data = torch.tensor(self.weight.data,dtype=torch.float64)
else:
self.weight = nn.Parameter(torch.Tensor(
output_features, input_features))
a = torch.rand(output_features, output_features)
u, s, v = torch.svd(a @ a.t())
self.weight.data = u[:, :input_features]
self.weight.data = torch.tensor(self.weight.data, dtype=torch.float64)
def forward(self, input):
return BiMapFunction.apply(input, self.weight)
class BiMapgpuFunction(Function):
@staticmethod
def forward(ctx, input, weight):
ctx.save_for_backward(input, weight)
output = (weight.double() @ input.double()) @ weight.double().t()
return output
@staticmethod
def backward(ctx, grad_output):
input, weight = ctx.saved_variables
grad_input = grad_weight = None
input = input.double()
if ctx.needs_input_grad[0]:
grad_input = weight.t() @ grad_output @ weight
if ctx.needs_input_grad[1]:
e_grad = 2 * grad_output @ weight @ input
e_grad = e_grad.sum(0)
grad_weight = e_grad - e_grad @ weight.t() @ weight
return grad_input, grad_weight
class BiMapgpu(nn.Module):
def __init__(self, input_features, output_features):
super(BiMapgpu, self).__init__()
self.input_features = input_features
self.output_features = output_features
self.weight = nn.Parameter(torch.Tensor(
output_features, input_features))
a = torch.rand(input_features, input_features)
u, s, v = torch.svd(a@a.t())
self.weight.data = u[:, :output_features].t()
self.weight.data = torch.tensor(self.weight.data,dtype=torch.float64,device ="cuda")
def forward(self, input):
return BiMapgpuFunction.apply(input, self.weight)
class BiMapSFunction(Function):
@staticmethod
def forward(ctx, input, weight):
ctx.save_for_backward(input, weight)
output = (weight.double() @ input.double()) @ weight.double().t()
return output
@staticmethod
def backward(ctx, grad_output):
input, weight = ctx.saved_variables
grad_input = grad_weight = None
input = input.double()
if ctx.needs_input_grad[0]:
grad_input = weight.t() @ grad_output @ weight
if ctx.needs_input_grad[1]:
e_grad = 2 * grad_output @ weight @ input
e_grad = e_grad.sum(0)
grad_weight = e_grad - e_grad @ weight.t() @ weight
return grad_input, grad_weight
class BiMapS(nn.Module):
def __init__(self, input_features, output_features):
super(BiMapS, self).__init__()
self.input_features = input_features
self.output_features = output_features
self.weight = nn.Parameter(torch.Tensor(
output_features, input_features))
a = torch.rand(input_features, input_features)
u, s, v = torch.svd(a@a.t())
self.weight.data = u[:, :output_features].t()
self.weight.data = torch.tensor(self.weight.data,dtype=torch.float64)
def forward(self, input):
return BiMapSFunction.apply(input, self.weight)
class FrMap(nn.Module):
def __init__(self, input_features, output_features):
super(FrMap, self).__init__()
self.input_features = input_features
self.output_features = output_features
self.weight = nn.Parameter(torch.Tensor(
output_features, input_features))
a = torch.rand(input_features, input_features)
u, s, v = torch.svd(a @ a.t())
self.weight.data = u[:, :output_features].t()
self.weight.data = torch.tensor(self.weight.data, dtype=torch.float64)
def forward(self, input):
return FrMapFunction.apply(input, self.weight)
class FrMapFunction(Function):
@staticmethod
def forward(ctx, input, weight):
ctx.save_for_backward(input, weight)
output = weight.double() @ input.double()
return output
@staticmethod
def backward(ctx, grad_output):
input, weight = ctx.saved_variables
grad_input = grad_weight = None
input = input.double()
if ctx.needs_input_grad[0]:
grad_input = weight.t() @ grad_output
grad_weight = grad_output @ torch.permute(input,[0,2,1])
return grad_input, grad_weight
class FrMapmul(nn.Module):
def __init__(self, input_features, output_features,inchannel):
super(FrMapmul, self).__init__()
self.input_features = input_features
self.output_features = output_features
self.inchannel = inchannel
self.weight = nn.Parameter(torch.Tensor(inchannel,
output_features, input_features))
a = torch.rand(inchannel,input_features, input_features)
u, s, v = torch.svd(a @ a.permute(0,2,1))
self.weight.data = u[:, :,:output_features].permute(0,2,1)
self.weight.data = torch.tensor(self.weight.data, dtype=torch.float64)
def forward(self, input):
return FrMapmulFunction.apply(input, self.weight)
class FrMapmulFunction(Function):
@staticmethod
def forward(ctx, input, weight):
ctx.save_for_backward(input, weight)
if len(input.size()) == 3:
output = (weight.unsqueeze(0)@input.unsqueeze(1))
else:
output = (weight.unsqueeze(0)@input)
return output
@staticmethod
def backward(ctx, grad_output):
input, weight = ctx.saved_variables
grad_input = grad_weight = None
input = input.double()
if ctx.needs_input_grad[0]:
grad_input = weight.permute(0,2,1) @ grad_output
grad_weight = grad_output @ input.permute(0,1,3,2)
else:
grad_input = weight.permute(0, 2, 1) @ grad_output
grad_weight = grad_output @ input.unsqueeze(1).permute(0,1,3,2)
grad_weight = grad_weight.mean(0)
# grad_input = grad_input.sum(0)
return grad_input, grad_weight
class BiMapmulFunction(Function):
@staticmethod
def forward(ctx, input, weight):
ctx.save_for_backward(input, weight)
if len(input.size()) == 3:
weight = weight.unsqueeze(0)
output = (weight @ input.unsqueeze(1)) @ weight.permute(0, 1, 3, 2)
else:
output = (weight.double() @ input.double()) @ weight.double().permute(0,2,1)
return output
@staticmethod
def backward(ctx, grad_output):
input, weight = ctx.saved_variables
grad_input = grad_weight = None
input = input.double()
if ctx.needs_input_grad[0]:
grad_input = weight.permute(0,2,1)@grad_output@weight
if ctx.needs_input_grad[1]:
if len(input.size()) == 3:
e_grad = 2 * grad_output @ weight @ input.unsqueeze(1)
e_grad = e_grad.sum(0) # 因为梯度必须有一个,不能有batchsize个
grad_weight = e_grad - e_grad @ weight.permute(0, 2, 1) @ weight
else:
e_grad = 2 * grad_output @ weight @ input
e_grad = e_grad.sum(0)#因为梯度必须有一个,不能有batchsize个
grad_weight = e_grad - e_grad @ weight.permute(0,2,1) @ weight
return grad_input, grad_weight
class BiMapmul(nn.Module):
def __init__(self, input_features, output_features,inchannel):
super(BiMapmul, self).__init__()
self.input_features = input_features
self.output_features = output_features
self.weight = nn.Parameter(torch.Tensor(
output_features, input_features,inchannel))
a = torch.rand(inchannel,input_features, input_features)
u, s, v = torch.svd(a@a.permute(0,2,1))
self.weight.data = u[:, :,:output_features].permute(0,2,1)
self.weight.data = torch.tensor(self.weight.data,dtype=torch.float64)
def forward(self, input):
return BiMapmulFunction.apply(input, self.weight)