-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcustom_functions.py
380 lines (331 loc) · 18 KB
/
custom_functions.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import math
import numpy as np
import sys
import torch
import torch.nn as nn
from torch.autograd import Function
import torch.nn.functional as F
from torch.autograd import Variable as Variable
from torch.nn import Module, Parameter
class MIL_MAX(Function):
def forward(self, input):
batch_size, channels, in_height, in_width = input.size()
prob_min = -sys.float_info.max * torch.ones(batch_size,channels,1,1)
input_flatten = input.view(batch_size,channels,in_height*in_width)
input_max_,ind = torch.max(input_flatten,2)
input_max = input_max_.view(batch_size,channels,1,1)
output = torch.max(input_max,prob_min)
self.save_for_backward(input,output)
return output
def backward(self, grad_output):
input, output = self.saved_tensors
batch_size, channels, in_height, in_width = input.size()
grad_input = tmp = None
output_expand = output.expand(batch_size, channels, in_height, in_width)
grad_input = grad_output.clone()
grad_input = torch.mul(grad_input, torch.eq(output_expand,input).type(torch.FloatTensor))
return grad_input
class MIL_OR(Function):
def forward(self, input, ):
#print(input)
batch_size, channels, in_height, in_width = input.size()
prob_min = -sys.float_info.max * torch.ones(batch_size,channels,1,1)
input_flatten = input.view(batch_size,channels,in_height*in_width)
input_max_,ind = torch.max(input_flatten,2)
input_max = input_max_.view(batch_size,channels,1,1)
nor = 1. - input_flatten
nor_ = nor.prod(2)
prod = 1. - nor_.view(batch_size,channels,1,1)
output = torch.max(prod,input_max)
ge_zeros = torch.ge(output,0.0)
if ge_zeros.sum() < batch_size*channels:
raise ValueError("mil_prob not >= 0")
le_ones = torch.le(output,1.0)
if le_ones.sum() < batch_size*channels:
raise ValueError("mil_prob not <= 1")
self.save_for_backward(input,output)
return output
def backward(self, grad_output):
input, output = self.saved_tensors
batch_size, channels, in_height, in_width = input.size()
grad_input = tmp = None
output_expand = output.expand(batch_size, channels, in_height, in_width)
grad_output_expand = grad_output.expand(batch_size, channels, in_height, in_width)
grad_input = grad_output_expand.clone()
#print(input.size())
#print(output.size())
tmp = torch.div(1 - output_expand,1 - input)
#if torch.cuda.is_available():
grad_input = grad_input.mul(torch.clamp(tmp,max=1.))
#else:
# grad_input = grad_input.mul(torch.min(tmp,1.*torch.ones(batch_size, channels, in_height, in_width)))
return grad_input
class DAG_RNN_SE(Function):
def forward(self, input, weight_hh, weight_yh, bias_yh):
# SE plane
#print(input)
#print(weight_hh)
batch_size, channels, in_height, in_width = input.size()
output = input-input
output = output.view(batch_size, channels, in_height * in_width)
hidden = input
#print(hidden)
for b in range(batch_size):
for h in range(in_height):
for w in range(in_width):
if h > 0:
hidden[b,:,h,w] = hidden[b,:,h,w].unsqueeze(1) + torch.mm(weight_hh,hidden[b,:,h-1,w].unsqueeze(1))
if w > 0:
hidden[b,:,h,w] = hidden[b,:,h,w].unsqueeze(1) + torch.mm(weight_hh,hidden[b,:,h,w-1].unsqueeze(1))
#print(hidden[b,:,h,w])
#relu
#hidden[b,:,h,w] = torch.clamp(hidden[b,:,h,w],min=0)
hidden[b,:,h,w] = hidden[b,:,h,w].tanh()
hidden_b = hidden[b,:,:,:]
hidden_b = hidden_b.view(-1,in_height*in_width)
#print(bias_yh.size())
output_b = torch.mm(weight_yh, hidden_b) + bias_yh.unsqueeze(1).expand_as(hidden_b)
output[b,:,:] = output_b
#print(output.size())
self.save_for_backward(hidden, weight_hh, weight_yh, bias_yh)
return output
def backward(self, grad_output):
hidden, weight_hh, weight_yh, bias_yh = self.saved_tensors
batch_size, channels, in_height, in_width = hidden.size()
grad_output_ = grad_output.view(batch_size, -1, in_height * in_width)
grad_output = grad_output.view(batch_size, -1, in_height, in_width)
hidden_ = hidden.view(batch_size, channels, in_height * in_width)
grad_weight_yh = weight_yh - weight_yh
grad_bias_yh = bias_yh - bias_yh
grad_weight_hh = weight_hh - weight_hh
grad_hidden = hidden - hidden
for b in range(batch_size):
grad_weight_yh = grad_weight_yh + grad_output_[b,:,:].mm(hidden_[b,:,:].t())
grad_bias_yh = grad_bias_yh + grad_output_[b,:,:].sum(1)
for h in range(in_height-1,-1,-1):
for w in range(in_width-1,-1,-1):
grad_hidden[b,:,h,w] = grad_hidden[b,:,h,w].unsqueeze(1) + weight_yh.t().mm(grad_output[b,:,h,w].unsqueeze(1))
#grad_hf = grad_hidden[b,:,h,w].mul(grelu(hidden[b,:,h,w]))
grad_hf = grad_hidden[b,:,h,w].mul(gtanh(hidden[b,:,h,w]))
grad_hh = weight_hh.t().mm(grad_hf.unsqueeze(1))
if h > 0:
grad_hidden[b,:,h-1,w] = grad_hidden[b,:,h-1,w] + grad_hh
grad_weight_hh = grad_weight_hh + grad_hf.unsqueeze(1).mm(grad_hidden[b,:,h-1,w].unsqueeze(1).t())
if w > 0:
grad_hidden[b,:,h,w-1] = grad_hidden[b,:,h,w-1] + grad_hh
grad_weight_hh = grad_weight_hh + grad_hf.unsqueeze(1).mm(grad_hidden[b,:,h,w-1].unsqueeze(1).t())
grad_weight_hh_norm = torch.norm(grad_weight_hh.view(-1))
grad_weight_yh_norm = torch.norm(grad_weight_yh.view(-1))
grad_bias_yh_norm = torch.norm(grad_bias_yh.view(-1))
if grad_weight_hh_norm > 2000.0:
grad_weight_hh = grad_weight_hh / grad_weight_hh_norm * 2000
if grad_weight_yh_norm > 2000.0:
grad_weight_yh = grad_weight_yh / grad_weight_yh_norm * 2000
if grad_bias_yh_norm > 2000.0:
grad_bias_yh = grad_bias_yh / grad_bias_yh_norm * 2000
#print(grad_weight_hh_norm)
#print(grad_weight_yh_norm)
#print(grad_bias_yh_norm)
return grad_hidden, grad_weight_hh, grad_weight_yh, grad_bias_yh
class DAG_RNN_SW(Function):
def forward(self, input, output_last, weight_hh, weight_yh):
# SW Plane
batch_size, channels, in_height, in_width = input.size()
output = (input - input).view(batch_size, channels, in_height*in_width)
hidden = input
for b in range(batch_size):
for h in range(in_height-1,-1,-1):
for w in range(in_width):
if h < in_height-1:
hidden[b,:,h,w] = hidden[b,:,h,w].unsqueeze(1) + torch.mm(weight_hh,hidden[b,:,h+1,w].unsqueeze(1))
if w > 0:
hidden[b,:,h,w] = hidden[b,:,h,w].unsqueeze(1) + torch.mm(weight_hh,hidden[b,:,h,w-1].unsqueeze(1))
#hidden[b,:,h,w] = torch.clamp(hidden[b,:,h,w],min=0)
hidden[b,:,h,w] = hidden[b,:,h,w].tanh()
hidden_b = hidden[b,:,:,:]
hidden_b = hidden_b.view(-1,in_height*in_width)
output_b = torch.mm(weight_yh, hidden_b)
#print(hidden_b)
output[b,:,:] = torch.mm(weight_yh, hidden_b)
output[b,:,:] = output_b
output = output + output_last
self.save_for_backward(hidden, weight_hh, weight_yh)
return output
def backward(self, grad_output):
hidden, weight_hh, weight_yh = self.saved_tensors
batch_size, channels, in_height, in_width = hidden.size()
grad_output_ = grad_output.view(batch_size, -1, in_height * in_width)
grad_output = grad_output.view(batch_size, -1, in_height, in_width)
hidden_ = hidden.view(batch_size, channels, in_height * in_width)
grad_weight_yh = weight_yh - weight_yh
grad_weight_hh = weight_hh - weight_hh
grad_hidden = hidden - hidden
for b in range(batch_size):
grad_weight_yh = grad_weight_yh + grad_output_[b,:,:].mm(hidden_[b,:,:].t())
for h in range(in_height):
for w in range(in_width-1,-1,-1):
grad_hidden[b,:,h,w] = grad_hidden[b,:,h,w].unsqueeze(1) + weight_yh.t().mm(grad_output[b,:,h,w].unsqueeze(1))
#grad_hf = grad_hidden[b,:,h,w].mul(grelu(hidden[b,:,h,w]))
grad_hf = grad_hidden[b,:,h,w].mul(gtanh(hidden[b,:,h,w]))
grad_hh = weight_hh.t().mm(grad_hf.unsqueeze(1))
if h < in_height - 1:
grad_hidden[b,:,h+1,w] = grad_hidden[b,:,h+1,w] + grad_hh
grad_weight_hh = grad_weight_hh + grad_hf.unsqueeze(1).mm(grad_hidden[b,:,h+1,w].unsqueeze(1).t())
if w > 0:
grad_hidden[b,:,h,w-1] = grad_hidden[b,:,h,w-1] + grad_hh
grad_weight_hh = grad_weight_hh + grad_hf.unsqueeze(1).mm(grad_hidden[b,:,h,w-1].unsqueeze(1).t())
grad_weight_hh_norm = torch.norm(grad_weight_hh.view(-1))
grad_weight_yh_norm = torch.norm(grad_weight_yh.view(-1))
if grad_weight_hh_norm > 2000.0:
grad_weight_hh = grad_weight_hh / grad_weight_hh_norm * 2000
if grad_weight_yh_norm > 2000.0:
grad_weight_yh = grad_weight_yh / grad_weight_yh_norm * 2000
return grad_hidden, None,grad_weight_hh, grad_weight_yh
class DAG_RNN_NW(Function):
def forward(self, input, output_last, weight_hh, weight_yh,bias_yh=None):
# NW Plane
batch_size, channels, in_height, in_width = input.size()
output = (input - input).view(batch_size, channels, in_height*in_width)
hidden = input
for b in range(batch_size):
for h in range(in_height-1,-1,-1):
for w in range(in_width-1,-1,-1):
if h < in_height-1:
hidden[b,:,h,w] = hidden[b,:,h,w].unsqueeze(1) + torch.mm(weight_hh,hidden[b,:,h+1,w].unsqueeze(1))
if w < in_width-1:
hidden[b,:,h,w] = hidden[b,:,h,w].unsqueeze(1) + torch.mm(weight_hh,hidden[b,:,h,w+1].unsqueeze(1))
#hidden[b,:,h,w] = torch.clamp(hidden[b,:,h,w],min=0)
hidden[b,:,h,w] = hidden[b,:,h,w].tanh()
hidden_b = hidden[b,:,:,:]
hidden_b = hidden_b.view(-1,in_height*in_width)
output_b = torch.mm(weight_yh, hidden_b)
output[b,:,:] = torch.mm(weight_yh, hidden_b)
output[b,:,:] = output_b
output = output + output_last
self.save_for_backward(hidden, weight_hh, weight_yh)
return output
def backward(self, grad_output):
hidden, weight_hh, weight_yh = self.saved_tensors
batch_size, channels, in_height, in_width = hidden.size()
grad_output_ = grad_output.view(batch_size, -1, in_height * in_width)
grad_output = grad_output.view(batch_size, -1, in_height, in_width)
hidden_ = hidden.view(batch_size, channels, in_height * in_width)
grad_weight_yh = weight_yh - weight_yh
grad_weight_hh = weight_hh - weight_hh
grad_hidden = hidden - hidden
for b in range(batch_size):
grad_weight_yh = grad_weight_yh + grad_output_[b,:,:].mm(hidden_[b,:,:].t())
for h in range(in_height):
for w in range(in_width):
grad_hidden[b,:,h,w] = grad_hidden[b,:,h,w].unsqueeze(1) + weight_yh.t().mm(grad_output[b,:,h,w].unsqueeze(1))
#grad_hf = grad_hidden[b,:,h,w].mul(grelu(hidden[b,:,h,w]))
grad_hf = grad_hidden[b,:,h,w].mul(gtanh(hidden[b,:,h,w]))
grad_hh = weight_hh.t().mm(grad_hf.unsqueeze(1))
if h < in_height - 1:
grad_hidden[b,:,h+1,w] = grad_hidden[b,:,h+1,w] + grad_hh
grad_weight_hh = grad_weight_hh + grad_hf.unsqueeze(1).mm(grad_hidden[b,:,h+1,w].unsqueeze(1).t())
if w < in_width - 1:
grad_hidden[b,:,h,w+1] = grad_hidden[b,:,h,w+1] + grad_hh
grad_weight_hh = grad_weight_hh + grad_hf.unsqueeze(1).mm(grad_hidden[b,:,h,w+1].unsqueeze(1).t())
grad_weight_hh_norm = torch.norm(grad_weight_hh.view(-1))
grad_weight_yh_norm = torch.norm(grad_weight_yh.view(-1))
if grad_weight_hh_norm > 2000.0:
grad_weight_hh = grad_weight_hh / grad_weight_hh_norm * 2000
if grad_weight_yh_norm > 2000.0:
grad_weight_yh = grad_weight_yh / grad_weight_yh_norm * 2000
return grad_hidden, None, grad_weight_hh, grad_weight_yh
class DAG_RNN_NE(Function):
def forward(self, input, output_last, weight_hh, weight_yh):
# NE Plane
batch_size, channels, in_height, in_width = input.size()
output = (input - input).view(batch_size, channels, in_height*in_width)
hidden = input
for b in range(batch_size):
for h in range(in_height):
for w in range(in_width-1,-1,-1):
if h > 0:
hidden[b,:,h,w] = hidden[b,:,h,w].unsqueeze(1) + torch.mm(weight_hh,hidden[b,:,h-1,w].unsqueeze(1))
if w < in_width-1:
hidden[b,:,h,w] = hidden[b,:,h,w].unsqueeze(1) + torch.mm(weight_hh,hidden[b,:,h,w+1].unsqueeze(1))
#hidden[b,:,h,w] = torch.clamp(hidden[b,:,h,w],min=0)
hidden[b,:,h,w] = hidden[b,:,h,w].tanh()
hidden_b = hidden[b,:,:,:]
hidden_b = hidden_b.view(-1,in_height*in_width)
output_b = torch.mm(weight_yh, hidden_b)
output[b,:,:] = torch.mm(weight_yh, hidden_b)
output[b,:,:] = output_b
output = output + output_last
#output = F.softmax(output)
output = output.view(batch_size, channels, in_height, in_width)
self.save_for_backward(hidden, weight_hh, weight_yh)
return output
def backward(self, grad_output):
hidden, weight_hh, weight_yh = self.saved_tensors
batch_size, channels, in_height, in_width = hidden.size()
grad_output_ = grad_output.view(batch_size, -1, in_height * in_width)
grad_output = grad_output.view(batch_size, -1, in_height, in_width)
hidden_ = hidden.view(batch_size, channels, in_height * in_width)
grad_weight_yh = weight_yh - weight_yh
grad_weight_hh = weight_hh - weight_hh
grad_hidden = hidden - hidden
for b in range(batch_size):
grad_weight_yh = grad_weight_yh + grad_output_[b,:,:].mm(hidden_[b,:,:].t())
for h in range(in_height-1,-1,-1):
for w in range(in_width):
grad_hidden[b,:,h,w] = grad_hidden[b,:,h,w].unsqueeze(1) + weight_yh.t().mm(grad_output[b,:,h,w].unsqueeze(1))
#grad_hf = grad_hidden[b,:,h,w].mul(grelu(hidden[b,:,h,w]))
grad_hf = grad_hidden[b,:,h,w].mul(gtanh(hidden[b,:,h,w]))
grad_hh = weight_hh.t().mm(grad_hf.unsqueeze(1))
if h > 0:
grad_hidden[b,:,h-1,w] = grad_hidden[b,:,h-1,w] + grad_hh
grad_weight_hh = grad_weight_hh + grad_hf.unsqueeze(1).mm(grad_hidden[b,:,h-1,w].unsqueeze(1).t())
if w < in_width - 1:
grad_hidden[b,:,h,w+1] = grad_hidden[b,:,h,w+1] + grad_hh
grad_weight_hh = grad_weight_hh + grad_hf.unsqueeze(1).mm(grad_hidden[b,:,h,w+1].unsqueeze(1).t())
grad_weight_hh_norm = torch.norm(grad_weight_hh.view(-1))
grad_weight_yh_norm = torch.norm(grad_weight_yh.view(-1))
if grad_weight_hh_norm > 2000.0:
grad_weight_hh = grad_weight_hh / grad_weight_hh_norm * 2000
if grad_weight_yh_norm > 2000.0:
grad_weight_yh = grad_weight_yh / grad_weight_yh_norm * 2000
return grad_hidden, None, grad_weight_hh, grad_weight_yh
def mil_max(input):
return MIL_MAX()(input)
def mil_or(input):
return MIL_OR()(input)
def grelu(input):
ginput = input.clamp(min=0.0)
ginput = ginput.gt(0).type_as(input)
return ginput
def gtanh(input):
ginput = 1 - input.mul(input)
return ginput
def dag_rnn_se(input,weight_hh, weight_yh, bias_yh):
return DAG_RNN_SE()(input,weight_hh, weight_yh, bias_yh)
def dag_rnn_sw(input,output_last, weight_hh, weight_yh):
return DAG_RNN_SW()(input,output_last, weight_hh, weight_yh)
def dag_rnn_nw(input,output_last, weight_hh, weight_yh):
return DAG_RNN_NW()(input,output_last, weight_hh, weight_yh)
def dag_rnn_ne(input,output_last, weight_hh, weight_yh):
return DAG_RNN_NE()(input, output_last, weight_hh, weight_yh)
# simple test
if __name__ == "__main__":
from torch.autograd import Variable
torch.manual_seed(1111)
a = torch.randn(4,3,2, 3)
a = torch.min(a,1*torch.ones(4,3,2,3))
a = torch.max(a,0.001+torch.zeros(4,3,2,3))
a = torch.sigmoid(a)
print(a)
va = Variable(a, requires_grad=True)
weight_hh = Parameter(torch.rand(3, 3))
weight_yh = Parameter(torch.rand(3, 3))
bias_yh = Parameter(torch.rand(3))
#print(va)
vb = dag_rnn_se(va, weight_hh, weight_yh, bias_yh)
vc = dag_rnn_sw(va,vb, weight_hh, weight_yh)
vd = dag_rnn_nw(va,vc, weight_hh, weight_yh)
ve = dag_rnn_ne(va,vd, weight_hh, weight_yh)
print vb.data,vc.data,vd.data,ve.data
ve.backward(torch.ones(va.size()))
print ve.grad.data