-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
286 lines (235 loc) · 9.38 KB
/
Copy pathmodels.py
File metadata and controls
286 lines (235 loc) · 9.38 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import torch
import torch.nn as nn
from scipy.signal import firwin2
from scipy.io import loadmat
class EndFilter(nn.Module):
def __init__(self, n_channels, out_filtration, filter_path):
super(EndFilter, self).__init__()
if out_filtration:
filter_coeff = loadmat(filter_path)["flt_coeff"][0]
filter_coeff = filter_coeff[::-1].copy()
wts = torch.from_numpy(filter_coeff).to(torch.complex64)
wts_expand = wts.unsqueeze(0).unsqueeze(0).expand(n_channels, 1, 255).clone()
self.end_filter = torch.nn.Conv1d(
in_channels=n_channels,
out_channels=n_channels,
kernel_size=255,
padding="same",
groups=n_channels,
bias=False,
)
self.end_filter.weight.data = wts_expand
self.end_filter.weight.requires_grad = False
self.out_filtration = out_filtration
def forward(self, x):
if self.out_filtration:
cmplx_tensor = x[..., 0] + 1j * x[..., 1]
cmplx_tensor = cmplx_tensor.permute(1, 0).unsqueeze(0)
filt_cmplx = self.end_filter(cmplx_tensor)
output = torch.stack((filt_cmplx.real, filt_cmplx.imag), dim=-1)
x = output.squeeze(0).permute(1, 0, 2)
return x.to(torch.float32)
class CoreModel(nn.Module):
def __init__(
self,
n_channels,
input_size,
out_window,
hidden_size,
backbone_type,
batch_size,
out_filtration,
filter_path,
aux_loss_present,
):
super(CoreModel, self).__init__()
self.output_size = 2 # PIM outputs: I & Q
self.input_size = input_size
self.out_window = out_window
self.hidden_size = hidden_size
self.backbone_type = backbone_type
self.batch_size = batch_size
self.n_channels = n_channels
self.batch_first = True # Force batch first
self.bidirectional = False
self.bias = True
self.filter = EndFilter(n_channels, out_filtration, filter_path)
self.out_filtration = out_filtration
self.aux_loss_present = aux_loss_present
if backbone_type == "linear":
from backbones.linear import Linear
self.backbone = Linear(
input_size=self.input_size,
output_size=self.output_size,
batch_size=self.batch_size,
n_channels=n_channels,
)
elif backbone_type == "cond_linear":
from backbones.linear_conductive import LinearConductive
self.backbone = LinearConductive(
in_seq_size=self.input_size,
out_seq_size=self.out_window,
n_channels=n_channels,
)
elif backbone_type == "cond_linear_cx":
from backbones.linear_conductive_cx import LinearConductive
self.backbone = LinearConductive(
in_seq_size=self.input_size,
out_seq_size=self.out_window,
n_channels=n_channels,
)
elif backbone_type == "leak_linear":
from backbones.linear_leakage import LinearLeakage
self.backbone = LinearLeakage(
in_seq_size=self.input_size,
out_seq_size=self.out_window,
n_channels=n_channels,
)
elif backbone_type == "ext_linear":
from backbones.linear_external import LinearExternal
self.backbone = LinearExternal(
in_seq_size=self.input_size,
out_seq_size=self.out_window,
n_channels=n_channels,
)
elif backbone_type == "ext_single":
from backbones.external_single import ExternalSingle
self.backbone = ExternalSingle(
in_seq_size=self.input_size,
out_seq_size=self.out_window,
n_channels=n_channels,
)
elif backbone_type == "cond_leak_linear":
from backbones.linear_cond_leak import LinearCondLeak
self.backbone = LinearCondLeak(
in_seq_size=self.input_size,
out_seq_size=self.out_window,
n_channels=n_channels,
)
elif backbone_type == "linpoly":
from backbones.lin_poly import LinPoly
self.backbone = LinPoly(
input_size=self.input_size,
output_size=self.output_size,
n_channels=n_channels,
batch_size=self.batch_size,
)
elif backbone_type == "ext_linpoly":
from backbones.lin_poly_external import LinPolyExternal
self.backbone = LinPolyExternal(
input_size=self.input_size,
output_size=self.output_size,
n_channels=n_channels,
batch_size=self.batch_size,
out_window=self.out_window,
)
elif backbone_type == "leak_linpoly":
from backbones.lin_poly_leakage import LinPolyLeakage
self.backbone = LinPolyLeakage(
input_size=self.input_size,
output_size=self.output_size,
n_channels=n_channels,
batch_size=self.batch_size,
out_window=self.out_window,
)
elif backbone_type == "int_linpoly":
from backbones.lin_poly_internal import LinPolyInternal
self.backbone = LinPolyInternal(
input_size=self.input_size,
output_size=self.output_size,
n_channels=n_channels,
batch_size=self.batch_size,
out_window=self.out_window,
)
elif backbone_type == "convx":
from backbones.conv import ConvModel
self.backbone = ConvModel(
input_size=self.input_size,
output_size=self.output_size,
batch_size=self.batch_size,
n_channels=n_channels,
)
elif backbone_type == "cnn_gru":
from backbones.cnn_gru import CNN_GRU
self.backbone = CNN_GRU(
hidden_size=self.hidden_size,
output_size=self.output_size,
batch_size=self.batch_size,
batch_first=self.batch_first,
bias=self.bias,
input_size=self.input_size,
n_channels=n_channels,
)
elif backbone_type == "cnn_only":
from backbones.cnn_gru import CNN_ONLY
self.backbone = CNN_ONLY(
hidden_size=self.hidden_size,
output_size=self.output_size,
batch_size=self.batch_size,
batch_first=self.batch_first,
bias=self.bias,
input_size=self.input_size,
n_channels=n_channels,
)
elif backbone_type == "mcp":
from backbones.mcp import MultiChannelMLP
self.backbone = MultiChannelMLP(
in_seq_size=self.input_size,
out_seq_size=self.out_window,
n_channels=self.n_channels,
)
elif backbone_type == "mcp_abs":
from backbones.mcp_abs import McpAbs
self.backbone = McpAbs(
in_seq_size=self.input_size,
out_seq_size=self.out_window,
n_channels=n_channels,
)
elif backbone_type == "mcp_enriched":
from backbones.mcp_enriched import McpEnriched
self.backbone = McpEnriched(
in_seq_size=self.input_size,
out_seq_size=self.out_window,
n_channels=n_channels,
)
elif backbone_type == "mcp_preproc":
from backbones.mcp_preproc import McpPreproc
self.backbone = McpPreproc(
in_seq_size=self.input_size,
out_seq_size=self.out_window,
n_channels=n_channels,
)
elif backbone_type == "moe_enriched":
from backbones.moe_enriched import MoeEnriched
self.backbone = MoeEnriched(
in_seq_size=self.input_size,
out_seq_size=self.out_window,
n_channels=n_channels
)
else:
raise ValueError(
f"The backbone type '{self.backbone_type}' is not supported. Please add your own "
f"backbone under ./backbones and update models.py accordingly."
)
# Initialize backbone parameters
try:
self.backbone.reset_parameters()
print("Backbone Initialized...")
except AttributeError:
pass
def forward(self, x, h_0=None):
device = x.device
batch_size = x.size(0) # NOTE: dim of x must be (batch, time, feat)/(N, T, F)
if h_0 is None: # Create initial hidden states if necessary
h_0 = torch.zeros(1, batch_size, self.hidden_size).to(device)
if self.aux_loss_present:
output, aux_loss = self.backbone(x, h_0)
else:
output = self.backbone(x, h_0)
filtered_output = self.filter(output)
if self.aux_loss_present:
return filtered_output, aux_loss
else:
return filtered_output
def get_aux_loss_state(self):
return self.aux_loss_present