-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcomplex_neural_net.py
261 lines (162 loc) · 7.03 KB
/
complex_neural_net.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
"""
Complex Valued Neural Layers From Scratch
Programmed by Mehdi Hosseini Moghadam
* MIT Licence
* 2022-02-15 Last Update
"""
from torch import nn
import torch
##__________________________________Complex Linear Layer __________________________________________
class CLinear(nn.Module):
def __init__(self, in_channels, out_channels, **kwargs):
super(CLinear, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.re_linear = nn.Linear(self.in_channels, self.out_channels, **kwargs)
self.im_linear = nn.Linear(self.in_channels, self.out_channels, **kwargs)
nn.init.xavier_uniform_(self.re_linear.weight)
nn.init.xavier_uniform_(self.im_linear.weight)
def forward(self, x):
x_re = x[..., 0]
x_im = x[..., 1]
out_re = self.re_linear(x_re) - self.im_linear(x_im)
out_im = self.re_linear(x_im) + self.im_linear(x_re)
out = torch.stack([out_re, out_im], -1)
return out
##______________________________________Complex Convolution 2d_____________________________________________
class CConv2d(nn.Module):
def __init__(self, in_channels, out_channels, **kwargs):
super(CConv2d, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.re_conv = nn.Conv2d(self.in_channels, self.out_channels, **kwargs)
self.im_conv = nn.Conv2d(self.in_channels, self.out_channels, **kwargs)
nn.init.xavier_uniform_(self.re_conv.weight)
nn.init.xavier_uniform_(self.im_conv.weight)
def forward(self, x):
x_re = x[..., 0]
x_im = x[..., 1]
out_re = self.re_conv(x_re) - self.im_conv(x_im)
out_im = self.re_conv(x_im) + self.im_conv(x_re)
out = torch.stack([out_re, out_im], -1)
return out
##___________________________________Complex Convolution Transpose 2d_______________________________________________
class CConvTrans2d(nn.Module):
def __init__(self, in_channels, out_channels, **kwargs):
super(CConvTrans2d, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.re_Tconv = nn.ConvTranspose2d(self.in_channels, self.out_channels, **kwargs)
self.im_Tconv = nn.ConvTranspose2d(self.in_channels, self.out_channels, **kwargs)
nn.init.xavier_uniform_(self.re_Tconv.weight)
nn.init.xavier_uniform_(self.im_Tconv.weight)
def forward(self, x):
x_re = x[..., 0]
x_im = x[..., 1]
out_re = self.re_Tconv(x_re) - self.im_Tconv(x_im)
out_im = self.re_Tconv(x_im) + self.im_Tconv(x_re)
out = torch.stack([out_re, out_im], -1)
return out
##___________________________Complex BatchNorm Layer____________________________________
class CBatchnorm(nn.Module):
def __init__(self, in_channels):
super(CBatchnorm, self).__init__()
self.in_channels = in_channels
self.re_batch = nn.BatchNorm2d(in_channels)
self.im_batch = nn.BatchNorm2d(in_channels)
def forward(self, x):
x_re = x[..., 0]
x_im = x[..., 1]
out_re = self.re_batch(x_re)
out_im = self.re_batch(x_im)
out = torch.stack([out_re, out_im], -1)
return out
##_______________________Complex Convolutional Block_______________________________________
class CconvBlock(nn.Module):
def __init__(self, in_channels, out_channels, **kwargs):
super(CconvBlock, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.CConv2d = CConv2d(self.in_channels, self.out_channels, **kwargs)
self.CBatchnorm = CBatchnorm(self.out_channels)
self.leaky_relu = nn.LeakyReLU()
def forward(self, x):
conved = self.CConv2d(x)
normed = self.CBatchnorm(conved)
activated = self.leaky_relu(normed)
return activated
##__________________________________Complex Convolutional Transpose Block________________________________________
class CConvTransBlock(nn.Module):
def __init__(self, in_channels, out_channels, last_layer=False, **kwargs):
super(CConvTransBlock, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.last_layer = last_layer
self.CConvTrans2d = CConvTrans2d(self.in_channels, self.out_channels, **kwargs)
self.CBatchnorm = CBatchnorm(self.out_channels)
self.leaky_relu = nn.LeakyReLU()
def forward(self, x):
conved = self.CConvTrans2d(x)
if not self.last_layer:
normed = self.CBatchnorm(conved)
activated = self.leaky_relu(normed)
return activated
else:
m_phase = conved/(torch.abs(conved)+1e-8)
m_mag = torch.tanh(torch.abs(conved))
out = m_phase * m_mag
return out
##______________________Complex LSTM Layer_________________________________________________
class CLSTM(nn.Module):
def __init__(self, in_channels, hidden_size, num_layers, **kwargs):
super(CLSTM, self).__init__()
self.in_channels = in_channels
self.hidden_size = hidden_size
self.num_layers = num_layers
self.re_LSTM = nn.LSTM(self.in_channels, self.hidden_size, self.num_layers , **kwargs)
self.im_LSTM = nn.LSTM(self.in_channels, self.hidden_size, self.num_layers, **kwargs)
def forward(self, x, h0, c0):
x_re = x[..., 0]
x_im = x[..., 1]
out_re1, (hn_re1, cn_re1) = self.re_LSTM(x_re, (h0[...,0], c0[...,0]))
out_re2, (hn_re2, cn_re2) = self.im_LSTM(x_im, (h0[...,1], c0[...,1]))
out_re = out_re1 - out_re2
hn_re = hn_re1 - hn_re2
cn_re = cn_re1 - cn_re2
out_im1, (hn_im1, cn_im1) = self.re_LSTM(x_re, (h0[...,1], c0[...,1]))
out_im2, (hn_im2, cn_im2) = self.im_LSTM(x_im, (h0[...,0], c0[...,0]))
out_im = out_im1 + out_im2
hn_im = hn_im1 + hn_im2
cn_im = cn_im1 + cn_im2
out = torch.stack([out_re, out_im], -1)
hn = torch.stack([hn_re, hn_im], -1)
cn = torch.stack([cn_re, cn_im], -1)
return out, (hn, cn)
##_______________________________Complex MaxPooling 2d Layer___________________
class CMaxPool2d(nn.Module):
def __init__(self, kernel_size, **kwargs):
super(CMaxPool2d, self).__init__()
self.kernel_size = kernel_size
self.CMax_re = nn.MaxPool2d(self.kernel_size, **kwargs)
self.CMax_im = nn.MaxPool2d(self.kernel_size, **kwargs)
def forward(self, x):
x_re = x[..., 0]
x_im = x[..., 1]
out_re = self.CMax_re(x_re)
out_im = self.CMax_im(x_im)
out = torch.stack([out_re, out_im], -1)
return out
##________________________________Complex Average Pooling 2d Layer_____________________________
class CAvgPool2d(nn.Module):
def __init__(self, kernel_size, **kwargs):
super(CAvgPool2d, self).__init__()
self.kernel_size = kernel_size
self.CMax_re = nn.AvgPool2d(self.kernel_size, **kwargs)
self.CMax_im = nn.AvgPool2d(self.kernel_size, **kwargs)
def forward(self, x):
x_re = x[..., 0]
x_im = x[..., 1]
out_re = self.CMax_re(x_re)
out_im = self.CMax_im(x_im)
out = torch.stack([out_re, out_im], -1)
return out