-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcustom_modules.py
164 lines (144 loc) · 6.18 KB
/
custom_modules.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
import math
import torch
import torch.nn as nn
from custom_functions import *
class MIL_max(nn.Module):
"""
Applies a mil_max transformation to the incoming data: :math:`y = max(x)` at each feature map
Shape:
- Input: :math:`(batch_size, channels, in_height, in_width)`
- Output: :math:`(batch_size, channels, 1,1)`
Examples::
>>> input = Variable(torch.rand(100,50,7,7))
>>> output = mil_max(input)
>>> output.size()
"""
def forward(self, input):
return mil_max(input)
def __repr__(self):
return self.__class__.__name__
class MIL_or(nn.Module):
"""
Applies a mil_max transformation to the incoming data: :math:`y = 1-(1-p_{11})...(1-p_{wh})`
Shape:
- Input: :math:`(batch_size, channels, in_height, in_width)`
- Output: :math:`(batch_size, channels, 1,1)`
Examples::
>>> input = Variable(torch.rand(100,50,7,7))
>>> output = mil_or(input)
>>> output.size()
"""
def forward(self, input):
return mil_or(input)
def __repr__(self):
return self.__class__.__name__
class DAG_RNN_se(nn.Module):
"""
Applies a SouthEast RNN transformation to the incoming data
Shape:
- Input: :math:`(batch_size, channels, in_height, in_width)`
- Output: :math:`(batch_size, channels,in_height, in_width)`
Examples::
>>> input = Variable(torch.rand(100,50,7,7))
>>> output = dag_rnn_se(input,weight_hh,weight_yh,bias)
>>> output.size()
"""
def __init__(self, input_size, output_size):
super(DAG_RNN_se, self).__init__()
self.input_size = input_size
self.output_size = output_size
self.weight_hh = nn.Parameter(torch.Tensor(input_size, input_size))
self.weight_yh = nn.Parameter(torch.Tensor(input_size, output_size))
self.bias = nn.Parameter(torch.Tensor(output_size))
# Not a very smart way to initialize weights
#self.weight_hh.data.normal_(0, math.sqrt(2. / n))
#self.weight_hh.data.normal_(0, math.sqrt(2. / n))
#self.weight_hh.data.normal_()*1e-3
self.weight_yh.data.normal_()*1e-3
self.weight_hh.data = torch.eye(input_size, input_size)
self.bias.data.zero_()
def forward(self, input):
return dag_rnn_se(input,self.weight_hh,self.weight_yh,self.bias)
def __repr__(self):
return self.__class__.__name__
class DAG_RNN_sw(nn.Module):
"""
Applies a SouthWest RNN transformation to the incoming data
Shape:
- Input: :math:`(batch_size, channels, in_height, in_width)`
- Output: :math:`(batch_size, channels, in_height, in_width)`
Examples::
>>> input = Variable(torch.rand(100,50,7,7))
>>> output = dag_rnn_sw(input,output_lastweight_hh,weight_yh)
>>> output.size()
"""
def __init__(self, input_size, output_size):
super(DAG_RNN_sw, self).__init__()
self.input_size = input_size
self.output_size = output_size
self.weight_hh = nn.Parameter(torch.Tensor(input_size, input_size))
self.weight_yh = nn.Parameter(torch.Tensor(input_size, output_size))
# Not a very smart way to initialize weights
#self.weight_hh.data.normal_(0, math.sqrt(2. / n))
#self.weight_hh.data.normal_(0, math.sqrt(2. / n))
#self.weight_hh.data.normal_()*1e-3
self.weight_hh.data = torch.eye(input_size, input_size)
self.weight_yh.data.normal_()*1e-3
def forward(self, input, output_last):
return dag_rnn_sw(input,output_last, self.weight_hh,self.weight_yh)
def __repr__(self):
return self.__class__.__name__
class DAG_RNN_nw(nn.Module):
"""
Applies a NorthWest RNN transformation to the incoming data
Shape:
- Input: :math:`(batch_size, channels, in_height, in_width)`
- Output: :math:`(batch_size, channels,in_height, in_width)`
Examples::
>>> input = Variable(torch.rand(100,50,7,7))
>>> output = dag_rnn_nw(input,output_last,weight_hh,weight_yh)
>>> output.size()
"""
def __init__(self, input_size, output_size):
super(DAG_RNN_nw, self).__init__()
self.input_size = input_size
self.output_size = output_size
self.weight_hh = nn.Parameter(torch.Tensor(input_size, input_size))
self.weight_yh = nn.Parameter(torch.Tensor(input_size, output_size))
# Not a very smart way to initialize weights
#self.weight_hh.data.normal_(0, math.sqrt(2. / n))
#self.weight_hh.data.normal_(0, math.sqrt(2. / n))
#self.weight_hh.data.normal_()*1e-3
self.weight_hh.data = torch.eye(input_size, input_size)
self.weight_yh.data.normal_()*1e-3
def forward(self, input, output_last):
return dag_rnn_nw(input,output_last, self.weight_hh,self.weight_yh)
def __repr__(self):
return self.__class__.__name__
class DAG_RNN_ne(nn.Module):
"""
Applies a NorthEast RNN transformation to the incoming data
Shape:
- Input: :math:`(batch_size, channels, in_height, in_width)`
- Output: :math:`(batch_size, channels, in_height, in_width)`
Examples::
>>> input = Variable(torch.rand(100,50,7,7))
>>> output = dag_rnn_ne(input,output_last,weight_hh,weight_yh)
>>> output.size()
"""
def __init__(self, input_size, output_size):
super(DAG_RNN_ne, self).__init__()
self.input_size = input_size
self.output_size = output_size
self.weight_hh = nn.Parameter(torch.Tensor(input_size, input_size))
self.weight_yh = nn.Parameter(torch.Tensor(input_size, output_size))
# Not a very smart way to initialize weights
#self.weight_hh.data.normal_(0, math.sqrt(2. / n))
#self.weight_hh.data.normal_(0, math.sqrt(2. / n))
#self.weight_hh.data.normal_()*1e-3
self.weight_hh.data = torch.eye(input_size, input_size)
self.weight_yh.data.normal_()*1e-3
def forward(self, input, output_last):
return dag_rnn_ne(input,output_last, self.weight_hh,self.weight_yh)
def __repr__(self):
return self.__class__.__name__